1

I've made a circle shaped movable form using the code in the following link Make a borderless form movable?

this Code:

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

Now my form is moving as I wanted but I want to add a click event to that form, but click event isn't firing. Can anyone tell what I'm missing?

Note: I just want to call a function whenever someone clicks my form.

1 Answers1

1

You can use the MouseMove method instead of the MouseDown method:

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

A form does not have to listen to their own events, so I am just overriding the MouseMove and Click procedures in the code above.

LarsTech
  • 80,625
  • 14
  • 153
  • 225