通过面板移动没有边框的表单

时间:2013-11-29 15:46:45

标签: c# forms mousemove

我一直在寻找解决我问题的方法,但是现在我无法获得任何我想做的成功代码。所以,我有一个没有边框的表单,里面装满了2个自定义面板,所以没有办法让用户点击框架,想到这一点,我实现了一个代码,当用户点击面板时,这将调用一个函数在我的表单上,通过参数recive鼠标的事件。 这是我的Panel的代码(请注意,我在框架中的两个面板都是同一个类,它只是两个不同的实例)

 public class MyPanel : System.Windows.Forms.Panel{

                       (...)

     private void MyPanel_MouseDown(object sender, MouseEventArgs e)
     {
         BarraSms.getInstance().mouseDown(e);
     }

     private void MyPanel_MouseMove(object sender, MouseEventArgs e)
     {
         BarraSms.getInstance().mouseMove(e);
     }

}

这是我表格的代码:

 public partial class BarraSms : Form
{
  private Point mousePoint;

             (...)

   public void mouseDown(MouseEventArgs e) {

        mousePoint = new Point(-e.X, -e.Y);

    }

    public void mouseMove(MouseEventArgs e) {

        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Point mousePos = Control.MousePosition;
            mousePos.Offset(mousePoint .X, mousePoint .Y);
            this.Location = mousePos;
        }

    }
}

有什么东西我不见了吗? 提前感谢您的帮助。

工作代码(更新),问题解决方法:x4rf41

MyPanel类

MouseMove += MyPanel_MouseMove; // added in class constructer

BarraSms课程(表格)

public partial class BarraSms : Form
{
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;


    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture(); 

     public void mouseMove(MouseEventArgs e) {

        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_NCLBUTTONDOWN, new IntPtr(HT_CAPTION), IntPtr.Zero);
            Point loc = this.Location;

            writeCoordToBin(loc.X, loc.Y);

        }

    }

}

3 个答案:

答案 0 :(得分:3)

使用windows api函数有更好的解决方案。 您使用它的方式,当您快速移动表单并且鼠标离开面板时,您将遇到问题。我遇到了同样的问题。

试试这个:

using System.Runtime.InteropServices;

并在您的Form类

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();        

public void mouseMove(MouseEventArgs e) 
{
   if (e.Button == System.Windows.Forms.MouseButtons.Left)
   {
      ReleaseCapture();
      SendMessage(this.Handle,  WM_NCLBUTTONDOWN, new IntPtr(HT_CAPTION), IntPtr.Zero);
   }
}

不需要mouseDown事件

答案 1 :(得分:0)

试试这个:

public void mouseMove(MouseEventArgs e) {

    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        Point currentPos = Location;
        currentPos.Offset(e.X + mousePoint.X, e.Y + mousePoint.Y);
        this.Location = currentPos;
    }
    //Or simply use Location.Offset(e.X + mousePoint.X, e.Y + mousePoint.Y);
}

您必须使用Location(表单的当前位置),而不是Control.MousePosition,这是鼠标在屏幕上的位置。

更新:您甚至不知道如何注册事件处理程序,尝试修改您的面板类:

public class MyPanel : System.Windows.Forms.Panel{
  //(...)
 protected override void OnMouseDown(MouseEventArgs e)
 {
     base.OnMouseDown(e);
     BarraSms.getInstance().mouseDown(e);
 }

 protected override void OnMouseMove(MouseEventArgs e)
 {
     base.OnMouseMove(e);
     BarraSms.getInstance().mouseMove(e);
 }

}

答案 2 :(得分:0)

框架无法调用MyPanel中的私有方法。您需要按如下方式声明它们:

protected override void OnMouseDown(MouseEventArgs e)
{
    var parent = this.Parent as BarraSms;
    parent.mouseDown(e);
}

protected override void OnMouseMove(MouseEventArgs e)
{
    var parent = this.Parent as BarraSms;
    parent.mouseMove(e);
}
相关问题