现在的位置: 首页 > 综合 > 正文

C# WinForm 鼠标事件 键盘事件

2013年12月10日 ⁄ 综合 ⁄ 共 1410字 ⁄ 字号 评论关闭

this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_KeyDown);

 

this.pDraw.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pDraw_MouseDown);
this.pDraw.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pDraw_MouseUp);

 

 

        Point mouse = new Point();
        int offset_x = 0;
        int offset_y = 0;

 

        //  改变位置
        private void pDraw_MouseDown(object sender, MouseEventArgs e)
        {
            mouse = e.Location;
        }

        private void pDraw_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                offset_x += e.X - mouse.X;
                offset_y += e.Y - mouse.Y;
                mouse = e.Location;
                pDraw.Invalidate(true);
            }
        }

 

        private void Form_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.Up)     // Ctrl + Up
            {
                offset_y -= 15;
                pDraw.Invalidate(true);
            }
            else if (e.Control && e.KeyCode == Keys.Down)    // Ctrl + Down
            {
                offset_y += 15;
                pDraw.Invalidate(true);
            }
            else if (e.Control && e.KeyCode == Keys.Left)    // Ctrl + Left
            {
                offset_x -= 15;
                pDraw.Invalidate(true);
            }
            else if (e.Control && e.KeyCode == Keys.Right)    // Ctrl + Right
            {
                offset_x += 15;
                pDraw.Invalidate(true);
            }
        }

抱歉!评论已关闭.