I'm attempting to make border-less forms that pop out of a tool bar. I want the user to be able to grab at the bottom-right corner (a "resize handle") and be able to resize the form, but not be able to resize or reposition the form in any other way.
I've heard that I can intercept the WM_NCHITTEST
message sent to the form and set its result to HTBOTTOMRIGHT
which will let the operating system handle the re-sizing of the form, just as if it had a sizable frame. The idea I had was to detect if the mouse pointer had entered a box I defined in the corner and if it did then return the HTBOTTOMRIGHT
result.
This doesn't quite work as I expected it to. I'm able to intercept the message, but it seems the message is only sent when the user positions the mouse cursor on the 1px thick border of the form. That means it works how I want to, if you very precisely position your cursor on the bottom-right edges.
Here is my WndProc
override:
protected override void WndProc(ref Message m)
{
const UInt32 WM_NCHITTEST = 0x0084;
const UInt32 HTBOTTOMRIGHT = 17;
const int RESIZE_HANDLE_SIZE = 40;
bool handled = false;
if (m.Msg == WM_NCHITTEST)
{
Size formSize = this.Size;
Point screenPoint = new Point(m.LParam.ToInt32());
Point clientPoint = this.PointToClient(screenPoint);
Rectangle hitBox = new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, formSize.Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE);
if (hitBox.Contains(clientPoint))
{
m.Result = (IntPtr)HTBOTTOMRIGHT;
handled = true;
}
}
if (!handled)
base.WndProc(ref m);
}
Am I doing something wrong or is there a better way to do what I'm trying to do?
Much thanks.
I was looking for something similar and Anton's code was a great base. This is what I ended up to have resize work from all sides. I'm unsure a
Dictionary
was optimal way to store the hitboxes, but I guess it doesn't matter all that much.And since my form is filled with controls using Fill as Dock parameters, I just had to add a 5px padding to the
Form
for it to work nicely.Based on Charles P. solution made some modifications to it, hope it helps others too :) Small checks and improvements to not declare extra variables every time the windows message is called. Also checks not painting the grip anchor when windows state is maximized. I wanted to create a custom control out of it, but i ended up filling the form with this code unfortunately.
Constructor or in the designer file:
Overriding windows functions:
Anton Semenov, i didn't understand your code.
Anyway, i had a problem with the first code of Charles P,
when i maximize the window and then try to change its size - it is being resized.
after that i couldn't fix it again to its normal size, nor maximize it again with the normal max button.
what i did to fix this problem was adding condition inside the 'foreach' loop at the bottom:
just little modification to your code. I've added
WM_MOUSEMOVE
message handling:by the way, you can draw system specific window size grip with
ControlPaint.DrawSizeGrip Method
http://msdn.microsoft.com/en-us/library/2e1yx2sa.aspx