简体   繁体   English

使面板可拖动

[英]Making a panel draggable

I am creating a "cropping tool", and i need to make a panel that contains 2 buttons draggable. 我正在创建“裁剪工具”,并且需要制作一个包含2个可拖动按钮的面板。

Until now i've tried something like this, but the change location event happens only when i click the right button of the mouse... 到目前为止,我已经尝试过类似的操作,但是更改位置事件仅在单击鼠标右键时发生。

this.MouseDown += new MouseEventHandler(onRightClickMouse);
private void onRightClickMouse(object sender, MouseEventArgs e)
{
      if (e.Button == MouseButtons.Right)
      {
            Point localMouseClickPoint = new Point(e.X, e.Y);
            panel1.Location = localMouseClickPoint;
      }
}

My question: How can i make that panel draggable in my form?(I mean click on the panel then drag it to a location). 我的问题:如何使该面板在表单中可拖动?(我的意思是单击面板,然后将其拖动到某个位置)。

Try something like this: 尝试这样的事情:

delegate void updatePanelCallback();
panel1.MouseDown += new MouseEventHandler(onMouseDown);
panel1.MouseUp += new MouseEventHandler(onMouseUp);
System.Timers.Timer runTimer = new System.Timers.Timer(100);
runTimer.Elapsed += new ElapsedEventHandler(onTimerElapsed);
private void onMouseDown(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.Right)
    {
        return;
    }
    runTimer.Enabled = false;
}
private void onMouseUp(object sender, MouseEventArgs e)
{
    runTimer.Enabled = false;
}
public void updatePanelLocation()
{
    if (this.InvokeRequired)
    {
        this.Invoke(new updatePanelCallback(updatePanelLocation), new object[] {});
    }
    else
    {
        Cursor curs = new Cursor(Cursor.Current.Handle);
        panel1.Location = curs.Position;
    }
}
private void onTimerElapsed(object source, ElapsedEventArgs e)
{
    updatePanelLocation();
}

您可以分两步进行尝试,准备对MouseDown事件的操作并在MouseUp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM