我想实现这样的功能:当我按下左键后,鼠标捕捉,然后鼠标移动。左键放开,释放鼠标捕捉。当我按下右键后,鼠标捕捉,然后鼠标移动。右键放开,释放鼠标捕捉。  private void Form1_MouseDown(object sender, MouseEventArgs e)
  {    if (e.Button == MouseButtons.Left)
{         处理}if (e.Button == MouseButtons.Right)
{处理}}   private void Form1_MouseMove(object sender, MouseEventArgs e)
{
 if (e.Button == MouseButtons.Left)//当鼠标按住左键并且移动的时候
            {,,,,,} if (e.Button == MouseButtons.Right)//鼠标右键按下并移动
            {,,,,}}我想问问我这个程序的逻辑有问题么。能不能实现我说的功能!!!

解决方案 »

  1.   

    逻辑有问题!
    设置标志变量int i=0,j=0;
     private void Form1_MouseDown(object sender, MouseEventArgs e) 
      {    if (e.Button == MouseButtons.Left) 
    { i=1;} if (e.Button == MouseButtons.Right) 
    {j=1;} } 
     private void Form1_MouseMove(object sender, MouseEventArgs e) 

    if (e.Button == MouseButtons.Left&&i==1)//当鼠标按住左键并且移动的时候 
                {,,,,,} if (e.Button == MouseButtons.Right&&j==1)//鼠标右键按下并移动 
                {,,,,} }
    然后再有一个MouseUp事件,让标志变量恢复为0.
    就行了。
      

  2.   

    需要一个bool变量确定是否捕捉。
      

  3.   


    不设置变量也可以,这是我的一段代码。捕捉成功。不过请注意MouseDown事件我是放到窗口上,如果你在窗口的其它控件上点击,你是捕捉不到 这个事件的。 private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
                {
                    this.Capture = true;
                }
            }        private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
                {
                    this.Capture = false;
                }
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (this.Capture)
                {
                    pictureBox1.Left = e.X;
                    pictureBox1.Top = e.Y;
                }
            }