I use a form with border NONE. I need to override WndProc for resize and move form. However, using this code, my app crashes!
static const int WM_NCHITTEST = 0x0084;
static const int HTCLIENT = 1;
static const int HTCAPTION = 2;
protected: virtual void Form1::WndProc(System::Windows::Forms::Message %m) override
{
switch (m.Msg)
{
case WM_NCHITTEST:
if (m.Result == IntPtr(HTCLIENT))
{
m.Result = IntPtr(HTCAPTION);
}
break;
}
Form1::WndProc(m);
}
virtual System::Windows::Forms::CreateParams^ get() override
{
System::Windows::Forms::CreateParams^ cp = __super::CreateParams;
cp->Style |= 0x40000;
return cp;
}
How can I fix my code not to crash but still allow my form to be moved and resized?
You have to call the base class’s
WndProcmethod first, in order form.Resultto be set to the correct value. The code you posted doesn’t call it until the end.Additionally, you’re trying to call the base class with
Form1::WndProc, which actually calls the current class’s implementation of theWndProcmethod. That’s why your application is “crashing” every time you try to run it with aStackOverflowException—your code within theWndProcmethod is calling itself recursively. Directly calling the superclass’s implementation (like you did in theCreateParamsproperty) with the__superkeyword works just fine.So, you need to re-write that method like this: