Skip to content

Instantly share code, notes, and snippets.

@kyrathasoft
Created November 9, 2019 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyrathasoft/9caaf1135b6dc59f291033115bfadd47 to your computer and use it in GitHub Desktop.
Save kyrathasoft/9caaf1135b6dc59f291033115bfadd47 to your computer and use it in GitHub Desktop.
Move a form using MouseDown() even if it doesn't have a titlebar
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
/*
Shows how to move the form without using a Title Bar to drag the form around the screen.
This can be adapted for custom forms (those with a customized picture box image overlaying the form,
and FormBorderstyle set to 'None' by simply changing Form1_MouseDown() to pictureBox1_MouseDown()...
*/
namespace MoveFormWithoutTitlebar {
public partial class Form1 : 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, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
public Form1() {
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment