Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving a Borderless Form in C Sharp
#1
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);
[Image: 7.gif]
#2
Nice tutorial Voi but would it be better if we moved it to Coding Tutorials?
[Image: 4.png]
#3
We have a forum for that?
[Image: 7.gif]
#4
(08-25-2013, 08:49 PM)Voi Wrote: 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.
#5
(05-24-2015, 10:12 PM)bitm0de Wrote:
(08-25-2013, 08:49 PM)Voi Wrote: 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.


Forum Jump:


Users browsing this thread: 2 Guest(s)


Powered by © 2002-2020 MyBB Group.
Theme by CreWix. Fixed by Tomik. Customized for Aeowulf.