Created
November 9, 2019 13:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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