PictureBox自定義實現按下效果

描述:鼠標移入控件,顯示有顏色邊框,鼠標按下,邊框加粗顏色改變,鬆開恢復。

兩個顏色可以在屬性裏面進行設置,可選擇背景圖片。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pic
{
    public partial class UserControl1 : UserControl
    {
        private Color clickBordercolr;
        public Color ClickBorderColor
        {
            get
            {
                return this.clickBordercolr;
            }
            set
            {
                this.clickBordercolr = value;
            }
        }

        private Color moveinbordercolor;
        public Color MoveInBorderColor
        {
            get
            {
                return moveinbordercolor;
            }
            set
            {
                this.moveinbordercolor = value;
            }
        }

        public Image BackImage
        {
            get
            {
                return pictureBox1.Image;
            }
            set 
            {
                pictureBox1.Image = value;
            }
        }

        private bool borderenabel;
        public bool BorderEnable
        {
            get { return borderenabel; }
            set { borderenabel = value; }
        }

        public UserControl1()
        {
            InitializeComponent();
            clickBordercolr = this.BackColor;
            moveinbordercolor = this.BackColor;            
            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

        }


        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if(this.BorderEnable)
            {
                this.pictureBox1.Height = 50;
                this.pictureBox1.Width = 50;
                this.BackColor = clickBordercolr;
                pictureBox1.Location = new Point(5, 5);
            } 
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            pictureBox1.Location = new Point(0, 0);
            if (this.BorderEnable)
            {
                //鼠標鬆開彈起,如果沒有在控件內,則不顯示效果
                if ( (e.Location.X < 60) &&(e.Location.Y < 60))
                {
                    //表示鼠標鬆開,在控件內
                    this.pictureBox1.Height = 54;
                    this.pictureBox1.Width = 54;
                    this.BackColor = MoveInBorderColor;
                    pictureBox1.Location = new Point(3, 3);
                }
                else
                {
                    this.pictureBox1.Height = 60;
                    this.pictureBox1.Width = 60;
                    pictureBox1.Location = new Point(0, 0);
                }
            }
           
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e) 
        {
            if (this.BorderEnable)
            {
                this.pictureBox1.Height = 54;
                this.pictureBox1.Width = 54;
                this.BackColor = MoveInBorderColor;
                pictureBox1.Location = new Point(3, 3);
            }
            
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            if (this.BorderEnable)
            {
                this.pictureBox1.Height = 60;
                this.pictureBox1.Width = 60;
                pictureBox1.Location = new Point(0, 0);
            }
           
        }

        private void UserControl1_SizeChanged(object sender, EventArgs e)
        {
            this.Width = this.Height = 60;
        }
    }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章