【发布时间】:2013-11-29 15:46:44
【问题描述】:
我一直在为我的问题寻找解决方案,但目前我无法获得任何我想做的成功代码。所以,我有一个没有边框的表单,里面有 2 个自定义面板,所以用户无法点击框架,我想,我实现了一个代码,当用户点击面板时,这将调用一个函数在我的表单上,通过参数接收鼠标事件。 这是我的面板的代码(请注意,我在框架中的两个面板都是同一个类,它只是 2 个不同的实例)
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);
}
}
}
【问题讨论】: