c#将click事件添加到无边框可拖动窗体

时间:2017-07-19 19:32:43

标签: c# winforms

我使用以下链接中的代码制作了一个圆形的可移动表格 Make a borderless form movable?

本守则:

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);
    }
}

现在我的表单正在按照我的意愿移动,但我想在该表单中添加一个click事件,但点击事件并未触发。谁能说出我失踪的东西?

注意:我只想在有人点击表单时调用函数。

1 个答案:

答案 0 :(得分:1)

您可以使用MouseMove方法而不是MouseDown方法:

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");
}

表单不必监听自己的事件,所以我只是覆盖上面代码中的MouseMove和Click过程。