If we don’t want to use the C# default window style, we definitely need to have a window with no borders. But you can’t drag a frameless window, so how do you do that?
With mouseDown, mouseUp, and mouseMove events, it’s easy.
First right click on our window class – view the code:
Then create these three variables in the window class:
/// <summary>
///The x coordinate in the window when the mouse is pressed down
/// </summary>
private int mouseAtX;
/// <summary>
///The y coordinate in the window when the mouse is pressed
/// </summary>
private int mouseAtY;
/// <summary>
///Whether the mouse is pressed over the window
/// </summary>
private bool isMouseDown = false;
Copy the code
Then add the mouseDown event to the form and add the following code to the function:
Cursor = Cursors.SizeAll;
mouseAtX = e.X;
mouseAtY = e.Y;
isMouseDown = true;
Copy the code
Add the mouseMove event and write the following code in the function:
if (isMouseDown)
{
Left = MousePosition.X - mouseAtX;
Top = MousePosition.Y - mouseAtY;
}
Copy the code
Finally add the mouseUp event and add the following code:
Cursor = Cursors.Default;
isMouseDown = false;
Copy the code
In fact, it is the use of the window inside the mouse position and the mouse in the screen position to calculate.
And that’s it: