Skip to content

Instantly share code, notes, and snippets.

@LaraSQP
Last active September 22, 2021 21:31
Show Gist options
  • Save LaraSQP/6d3bf091a6eb28be7651475a004fcb9b to your computer and use it in GitHub Desktop.
Save LaraSQP/6d3bf091a6eb28be7651475a004fcb9b to your computer and use it in GitHub Desktop.
private const int WM_NCHITTEST = 0x84;
private const int HTCAPTION = 2;
private const int HTLEFT = 10,
HTRIGHT = 11,
HTTOP = 12,
HTTOPLEFT = 13,
HTTOPRIGHT = 14,
HTBOTTOM = 15,
HTBOTTOMLEFT = 16,
HTBOTTOMRIGHT = 17;
private const int RESIZE_BORDER = 4;
private const int TITLEBAR_HEIGHT = 32;
protected override void WndProc( ref Message msg )
{
switch( msg.Msg )
{
case WM_NCHITTEST:
{
// Get the cursor position
var cursor = new Point( msg.LParam.ToInt32() );
cursor = PointToClient( cursor );
// Check for border resizing
var result = 0;
if( cursor.X <= RESIZE_BORDER )
{
// Leftmost
if( cursor.Y <= RESIZE_BORDER )
{
result = HTTOPLEFT;
}
else if( cursor.Y >= ClientRectangle.Height - RESIZE_BORDER )
{
result = HTBOTTOMLEFT;
}
else
{
result = HTLEFT;
}
}
else if( cursor.X >= ClientRectangle.Width - RESIZE_BORDER )
{
// Rightmost
if( cursor.Y <= RESIZE_BORDER )
{
result = HTTOPRIGHT;
}
else if( cursor.Y >= ClientRectangle.Height - RESIZE_BORDER )
{
result = HTBOTTOMRIGHT;
}
else
{
result = HTRIGHT;
}
}
else
{
// Top and bottom edges left
if( cursor.Y <= RESIZE_BORDER )
{
result = HTTOP;
}
else if( cursor.Y >= ClientRectangle.Height - RESIZE_BORDER )
{
result = HTBOTTOM;
}
}
// There was a change, return the border hit
if( result != 0 )
{
msg.Result = (IntPtr)result;
return;
}
// If it is on the title bar, let the system know to allow the user to drag the form around
if( cursor.Y < TITLEBAR_HEIGHT )
{
msg.Result = (IntPtr)HTCAPTION;
return;
}
break;
}
}
base.WndProc( ref msg );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment