C# 監聽滑鼠移動及keyin文字 圖片拖曳(二)

public partial class Form2 : Form
    {

        bool isPress = false;
        //判斷是否有按下
        int oldX, oldY;
        //記錄按下的位置
        public Form2()
        {
            InitializeComponent();
            
            Image ii = WA1030318.Properties.Resources.Car_GT;
            //要在Properties.Resources放入圖片
            PicLabel.Text = "";
            PicLabel.AutoSize = false;
            //關閉自動調整大小
            PicLabel.Width = ii.Width;
            PicLabel.Height = ii.Height;
            PicLabel.Image = ii;
        }

        private void PicLabel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                isPress = true;
                oldX = e.X;
                oldY = e.Y;
            }
        }

        private void PicLabel_MouseMove(object sender, MouseEventArgs e)
        {
            if (isPress)
            {
                PicLabel.Left = e.X + (PicLabel.Left - oldX);
                PicLabel.Top = e.Y + (PicLabel.Top - oldY);
                //按下的點,可能在圖上的任一點,所以抓出對應的座標並加回去

            }
        }

        private void PicLabel_MouseUp(object sender, MouseEventArgs e)
        {
            isPress = false;
            
        }
    }
說明:本例是實現在PicLabel上按下滑鼠右鍵拖曳的功能

留言

熱門文章