• IMPORTANT: Welcome to the re-opening of GameRebels! We are excited to be back and hope everyone has had a great time away. Everyone is welcome!

Moving a Borderless Form in C Sharp

Zahreah

Well-Known Member
Joined
Mar 31, 2012
Messages
2,822
Reaction score
8
Going to make a few tuts for C# Doubt any of you will need/understand this but anyway:

If you want to make a borderless form and be able to move it with the mouse, here's how:

Step #1
Add this to the 'using' list.
Code:
using Microsoft.Win32;

Step#2
Add this to the public frmTTMain() Under InitializeComponent();
Code:
this.MouseDown += new MouseEventHandler(this.frmTTMain_MouseDown);

Step #3
Add this AFTER the private void frmTTMain_Load
Code:
//API functions to move the form
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
 
public void frmTTMain_MouseDown(object sender, MouseEventArgs e)
{
    //If the left mouse is pressed, release form for movement
    if (e.Button == MouseButtons.Left)
{
    ReleaseCapture();
    SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
 

Toxique

Well-Known Member
MOTM
Joined
Jan 27, 2012
Messages
3,910
Reaction score
11
Nice tutorial Voi but would it be better if we moved it to Coding Tutorials?
 

bitm0de

Active Member
Joined
May 24, 2015
Messages
13
Reaction score
0
Voi said:
Going to make a few tuts for C# Doubt any of you will need/understand this but anyway:

If you want to make a borderless form and be able to move it with the mouse, here's how:

Step #1
Add this to the 'using' list.
Code:
using Microsoft.Win32;

Step#2
Add this to the public frmTTMain() Under InitializeComponent();
Code:
this.MouseDown += new MouseEventHandler(this.frmTTMain_MouseDown);

Step #3
Add this AFTER the private void frmTTMain_Load
Code:
//API functions to move the form
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
 
public void frmTTMain_MouseDown(object sender, MouseEventArgs e)
{
    //If the left mouse is pressed, release form for movement
    if (e.Button == MouseButtons.Left)
{
    ReleaseCapture();
    SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);

This is the wrong way to do it IMO... You can create your own form and override WndProc... Then you can handle the hit testing messages to determine what should be recognized as the client area of the form and what shouldn't be.
 

Zahreah

Well-Known Member
Joined
Mar 31, 2012
Messages
2,822
Reaction score
8
bitm0de said:
Voi said:
Going to make a few tuts for C# Doubt any of you will need/understand this but anyway:

If you want to make a borderless form and be able to move it with the mouse, here's how:

Step #1
Add this to the 'using' list.
Code:
using Microsoft.Win32;

Step#2
Add this to the public frmTTMain() Under InitializeComponent();
Code:
this.MouseDown += new MouseEventHandler(this.frmTTMain_MouseDown);

Step #3
Add this AFTER the private void frmTTMain_Load
Code:
//API functions to move the form
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
 
public void frmTTMain_MouseDown(object sender, MouseEventArgs e)
{
    //If the left mouse is pressed, release form for movement
    if (e.Button == MouseButtons.Left)
{
    ReleaseCapture();
    SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);

This is the wrong way to do it IMO... You can create your own form and override WndProc... Then you can handle the hit testing messages to determine what should be recognized as the client area of the form and what shouldn't be.

If i remember correctly i ripped this straight from another site about 2 years ago, i probably just changed a few things with whatever knowledge of the Language i knew back then.
 
Top