none
如何在一个button的MouseMove事件中触发另一个button的MouseUp事件? RRS feed

  • 问题

  • 现在想按下一个button1之后可以拖出一条线来,拖到另一个button2上面释放,这条线就连接这两个button,在其他地方释放则不绘制这条线。

    但是现在拖到button2上面释放时触发的是button1的MouseUp事件,如何触发button2的MouseUp事件?

    代码如下:

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public bool isDrawing = false;
            public Point startPoint;
            public Point endPoint;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_MouseDown(object sender, MouseEventArgs e)
            {
                isDrawing = true;
                startPoint = new Point(button1.Location.X, button1.Location.Y);
                endPoint = startPoint;
            }
    
            private void button1_MouseMove(object sender, MouseEventArgs e)
            {
                if (isDrawing)
                {
                    Graphics g = this.CreateGraphics();  
    
                    if (endPoint != startPoint)
                    {
                        Pen erase = new Pen(Color.White, 2);
                        g.DrawLine(erase, startPoint, endPoint);
                    }
    
                    int iMouseX = e.X;
                    int iMouseY = e.Y;
                    Pen pen = new Pen(Color.Black, 2);
                    endPoint = new Point(startPoint.X + iMouseX, startPoint.Y + iMouseY);
                    g.DrawLine(pen, startPoint, endPoint);
                }
            }
    
            private void button1_MouseUp(object sender, MouseEventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen erase = new Pen(Color.White, 2);
                g.DrawLine(erase, startPoint, endPoint);
            }
    
            private void button2_MouseUp(object sender, MouseEventArgs e)
            {
                if (isDrawing)
                {
                    isDrawing = false;
                    endPoint = startPoint;
                }
            }
    
    
        }
    }
    
    


     


    • 已编辑 laodouya 2012年1月31日 7:20
    2012年1月31日 7:20

答案

  • Hi laodouya,

    欢迎来到MSDN论坛!

    请您看下下面的代码:

            public bool isDrawing = false;
            public Point startPoint;
            public Point endPoint;      
    
            private void button1_MouseDown(object sender, MouseEventArgs e)
            {
                isDrawing = true;
                startPoint = new Point(button1.Location.X, button1.Location.Y);
                endPoint = startPoint;
            }
    
            private void button1_MouseMove(object sender, MouseEventArgs e)
            {
                if (isDrawing)
                {
                    Graphics g = this.CreateGraphics();
    
                    if (endPoint != startPoint)
                    {
                        Pen erase = new Pen(System.Drawing.SystemColors.Control, 2);
                        g.DrawLine(erase, startPoint, endPoint);
                    }
    
                    int iMouseX = e.X;
                    int iMouseY = e.Y;
                    Pen pen = new Pen(Color.Black, 2);
                    endPoint = new Point(startPoint.X + iMouseX, startPoint.Y + iMouseY);
                    g.DrawLine(pen, startPoint, endPoint);
                }
            }
    
            private void button1_MouseUp(object sender, MouseEventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen erase = new Pen(System.Drawing.SystemColors.Control, 2);
                g.DrawLine(erase, startPoint, endPoint);
    
                foreach (Control c in this.Controls)
                {
                    if (c is Button && c!=button1)
                    { 
                        if (c.Bounds.Contains(startPoint.X+e.X,startPoint.X+e.Y))
                        {
                            endPoint = new Point(startPoint.X+e.X,startPoint.X+e.Y);
                            Pen pen = new Pen(Color.Black, 2);                        
                            g.DrawLine(pen, startPoint, endPoint);
                        }
                    }
                }
    
                isDrawing = false;        
            }                         
    

     

    这里根据button1 MouseUp时候的位置,判断鼠标释放的点是否在某Button控件边界范围内,来确定终点位置.

    希望对您有所帮助!

    谢谢!

    yoyo


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    • 已建议为答案 buaamstc 2012年2月3日 2:02
    • 已标记为答案 laodouya 2012年2月3日 2:04
    2012年2月2日 3:38
    版主

全部回复

  • private void button1_MouseUp(object sender, MouseEventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen erase = new Pen(Color.White, 2);
                g.DrawLine(erase, startPoint, endPoint);

                //call button2's mouseUp
                button2_MouseUp(sender, e);
            }


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/
    • 已建议为答案 Stone Z 2012年1月31日 13:15
    • 取消建议作为答案 laodouya 2012年2月1日 2:25
    2012年1月31日 9:51
  • 感谢您的回复。 可能是我表述不够全面,描述问题时简化处理了一下。实际情况是有很多button2这样的按钮,您这个只能用于只有一个的情况。
    2012年2月1日 2:27
  • 或許您可以改用Drag & Drop的方式
    1.將Form & Button2的AllowDrop屬性設成true
    2.在button1_mousedown多加入 button1.DoDragDrop((sender as Button).Text, DragDropEffects.All);
    3.把button1_mouseMove code移到Form1_DragOver事件之中
    4.把button1_mouseUp及button2_mouseUp的Code移到button2_DragDrop事件之中

    這樣您就可以在不同的button上執行mouseUp要做的事情了哦!

    完整的Code如下,希望對您有幫助!

     

    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
    	isDrawing = true;
    	startPoint = new Point(button1.Location.X, button1.Location.Y);
    	endPoint = startPoint;
    	button1.DoDragDrop((sender as Button).Text, DragDropEffects.All);
    }
    
    
    private void button2_DragEnter(object sender, DragEventArgs e)
    {
    	e.Effect = DragDropEffects.All;
    }
    
    private void button2_DragDrop(object sender, DragEventArgs e)
    {
    	Graphics g = this.CreateGraphics();
    	Pen erase = new Pen(Color.White, 2);
    	g.DrawLine(erase, startPoint, endPoint);
    	if (isDrawing)
    	{
    		isDrawing = false;
    		endPoint = startPoint;
    	}
    }
    
    
    
    private void Form1_DragOver(object sender, DragEventArgs e)
    {
    	if (isDrawing)
    	{
    		Graphics g = this.CreateGraphics();
    
    		if (endPoint != startPoint)
    		{
    			Pen erase = new Pen(Color.White, 2);
    			g.DrawLine(erase, startPoint, endPoint);
    		}
    		endPoint = ((Control)sender).PointToClient (new Point(e.X, e.Y));
    		int iMouseX = endPoint.X;
    		int iMouseY = endPoint.Y;
    		
    		Pen pen = new Pen(Color.Black, 2);
    		endPoint = new Point(startPoint.X + iMouseX, startPoint.Y + iMouseY);
    		g.DrawLine(pen, startPoint, endPoint);
    	}
    }
    


     


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    2012年2月1日 5:07
  • Hi laodouya,

    欢迎来到MSDN论坛!

    请您看下下面的代码:

            public bool isDrawing = false;
            public Point startPoint;
            public Point endPoint;      
    
            private void button1_MouseDown(object sender, MouseEventArgs e)
            {
                isDrawing = true;
                startPoint = new Point(button1.Location.X, button1.Location.Y);
                endPoint = startPoint;
            }
    
            private void button1_MouseMove(object sender, MouseEventArgs e)
            {
                if (isDrawing)
                {
                    Graphics g = this.CreateGraphics();
    
                    if (endPoint != startPoint)
                    {
                        Pen erase = new Pen(System.Drawing.SystemColors.Control, 2);
                        g.DrawLine(erase, startPoint, endPoint);
                    }
    
                    int iMouseX = e.X;
                    int iMouseY = e.Y;
                    Pen pen = new Pen(Color.Black, 2);
                    endPoint = new Point(startPoint.X + iMouseX, startPoint.Y + iMouseY);
                    g.DrawLine(pen, startPoint, endPoint);
                }
            }
    
            private void button1_MouseUp(object sender, MouseEventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen erase = new Pen(System.Drawing.SystemColors.Control, 2);
                g.DrawLine(erase, startPoint, endPoint);
    
                foreach (Control c in this.Controls)
                {
                    if (c is Button && c!=button1)
                    { 
                        if (c.Bounds.Contains(startPoint.X+e.X,startPoint.X+e.Y))
                        {
                            endPoint = new Point(startPoint.X+e.X,startPoint.X+e.Y);
                            Pen pen = new Pen(Color.Black, 2);                        
                            g.DrawLine(pen, startPoint, endPoint);
                        }
                    }
                }
    
                isDrawing = false;        
            }                         
    

     

    这里根据button1 MouseUp时候的位置,判断鼠标释放的点是否在某Button控件边界范围内,来确定终点位置.

    希望对您有所帮助!

    谢谢!

    yoyo


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    • 已建议为答案 buaamstc 2012年2月3日 2:02
    • 已标记为答案 laodouya 2012年2月3日 2:04
    2012年2月2日 3:38
    版主