用鼠标在面板上移动形状

时间:2017-07-26 11:32:48

标签: c# mouseevent shapes

我有一个表单,其中包含一个面板,其中的按钮由代码生成(因此它们在开始时不存在于表单中)并放置在面板上。通常约10-20个按钮,但最多可能达到100个。

现在我需要用户能够单独旋转,更改按钮的形状和尺寸,以及通过鼠标移动它们(使用按钮使这部分工作)。 Button类似乎不是那样的,所以我开始阅读有关形状,绘制事件,图片盒等的内容,但我无法找到一种巧妙的方法。

EDIT 好的,所以我已经使用了paint事件,但是当我移动我的测试按钮时,它会闪烁很多。到目前为止,这是我的代码:

public class CustomButton
{
    Panel panel;
    private MyData data;

    private Point mouseClickLocation;
    private bool selected = false;
    private bool moving = false;

    int dX = 0;
    int dY = 0;


    //constructor
    public TableGraphics(MyData newData, Panel newPanel)
    {
        panel = newPanel;
        data = newData;
        panel.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaintHandler);
        panel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDownHandler);

    }

    //paint eventhandler
    public void OnPaintHandler(object sender, PaintEventArgs e)
    {

        Rectangle rect = new Rectangle(data.ButtonLocation, data.ButtonSize);

        using (Pen pen = new Pen(Color.Red, 5))
        {
            e.Graphics.DrawRectangle(pen, rect);
        }
    }

    //mouse down eventhandler
    public void OnMouseDownHandler(object sender, MouseEventArgs e)
    {
        Point buttonLocation = data.ButtonLocation;
        Size buttonSize = data.ButtonSize;

        //do we hit this object?
        if ((e.X < buttonLocation.X + buttonSize.Width) && (e.X > buttonLocation.X) &&
            (e.Y < buttonLocation.Y + buttonSize.Height) && (e.Y > buttonLocation.Y))
        {
            panel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnMouseUpHandler);
            panel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMoveHandler);
            selected = true;
            mouseClickLocation = e.Location;
        }
        else
        {
            //deselect
            selected = false;
        }

    }

    //mouse up eventhandler
    public void OnMouseUpHandler(object sender, MouseEventArgs e)
    {
        if (selected)
        {
            selected = false;
            moving = false;
            panel.MouseUp -= new System.Windows.Forms.MouseEventHandler(this.OnMouseUpHandler);
            panel.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.OnMouseMoveHandler);
        }

    }

    //mouse drag eventhandler
    public void OnMouseMoveHandler(object sender, MouseEventArgs e)
    {
        if(selected && moving)
        {
            data.ButtonLocation = new Point(e.X + dX, e.Y + dY);
            panel.Invalidate();
        }
        else if (selected && !moving)
        {
            dX = e.X - mouseClickLocation.X;
            dY = e.Y - mouseClickLocation.Y;
            int distance = ((dX * dX) + (dY * dY));

            if(distance > 15)
            {
                dX = data.ButtonLocation.X - e.X;
                dY = data.ButtonLocation.Y - e.Y;
                moving = true;
            }
        }
    }
}

CustomButton是从一个表单创建的,并提供一个存储在别处的数据类和一个对表单面板的引用,对于每个存在的数据对象一次(到目前为止只有1个用于测试目的)