2017-07-19 52 views
1

我做了使用的代码下面的链接 Make a borderless form movable?C#添加一个click事件无边界拖动形式

圆圈形状可动的形式验证码

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

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

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
{  
    if (e.Button == MouseButtons.Left) 
    { 
     ReleaseCapture(); 
     SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
    } 
} 

现在我的形式移动正如我想要的,但我想添加一个单击事件到该窗体,但点击事件不会触发。任何人都可以告诉我错过了什么吗?

注意:我只是想在某人点击我的表单时调用一个函数。

+0

请向我们显示您的代码。 –

+0

当我们甚至不知道您有什么时,很难知道您错过了什么...... – maccettura

+0

我更新了问题 –

回答

1

您可以使用鼠标移动方法,而不是鼠标按下方法:

protected override void OnMouseMove(MouseEventArgs e) { 
    base.OnMouseMove(e); 

    if (e.Button == MouseButtons.Left) { 
    ReleaseCapture(); 
    SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
    } 
} 

protected override void OnClick(EventArgs e) { 
    base.OnClick(e); 
    MessageBox.Show("Clicked"); 
} 

表单没有听自己的事件,所以我只是重写鼠标移动,并在代码点击上面的程序。

+0

但即使用户仅尝试重新定位表单,该代码也会执行。不是吗? –

+0

@AshfaqAfzal更新了答案。 – LarsTech