Skip to content

Instantly share code, notes, and snippets.

@AliAlmasi
Last active June 23, 2024 17:46
Show Gist options
  • Save AliAlmasi/f9f417c2afe362f7c4b0ed081af3b242 to your computer and use it in GitHub Desktop.
Save AliAlmasi/f9f417c2afe362f7c4b0ed081af3b242 to your computer and use it in GitHub Desktop.
Makes a Windows Form Application program movable when user drags any part of the form.
namespace WindowsFormApplication1 {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
private void FormMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
this.Cursor = Cursors.SizeAll;
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
this.Cursor = Cursors.Arrow;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment