WM_NCALCSIZE and Form restore resizing problem

G

Guest

Hi,

I have problem with creating own form. I need to make own header of form -
caption and buttons(minimize,restore,close). So I override WndProc and insert
in the switch own code for message WM_NCCALCSIZE.

Everything looks OK, but when I maximize and restore window, it shorts -
height is about 5px lower and width around 3px. So, when repeatly maximize
and restore window, its height is lower and lower.

When traced code, I noticed, that when I do not use own code for
wm_nccalcsize, I get 1times message WM_NCCALCSIZE, but when I use my code, I
get this message 3times during restoring the window

case (int)WinAPI_WM.WM_NCCALCSIZE:
WinAPI_NCCALCSIZE_PARAMS csp;
csp = (WinAPI_NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam,
typeof(WinAPI_NCCALCSIZE_PARAMS));
csp.rgrc0.Top += 20;
csp.rgrc0.Bottom -= 3;
csp.rgrc0.Left += 3;
csp.rgrc0.Right -= 3;
Marshal.StructureToPtr(csp, m.LParam, false);
m.Result = IntPtr.Zero;
break;


Where can be problem? Thanks for any answer.
 
M

Mick Doherty

Your code works just fine in VS2003. I don't know what changed, but in
VS2005 it seems that you also need to override the SetBoundsCore() method.

Your WM_NCCALCSIZE handling is incorrect. If wParam is false then lParam is
a RECT structure otherwise it is a NCCALCSIZE_PARAMS structure. Your code
works because you only modify the first RECT of the NCCALCSIZE_PARAMS, but
it should be corrected as in the example below.

This example also overrides the SetClientSizeCore() method so that the form
is resized to accomodate the new NonClientArea whilst still keeping the
ClientSize that you set at DesignTime, but if you're going to Inherit this
class and use it at DesignTime, then it will need some more reworking.

\\\
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCCALCSIZE)
{
RECT rc;
if (m.WParam.ToInt32() == 0)
{
rc = (RECT)m.GetLParam(typeof(RECT));
OnNonClientCalcSize(ref rc);
Marshal.StructureToPtr(rc, m.LParam, true);
}
else
{
NCCALCSIZE_PARAMS nccs =
(NCCALCSIZE_PARAMS)m.GetLParam(typeof(NCCALCSIZE_PARAMS));
rc = nccs.rgrc0;
OnNonClientCalcSize(ref rc);
nccs.rgrc0 = rc;
Marshal.StructureToPtr(nccs, m.LParam, true);
}
m.Result = IntPtr.Zero;
}
base.WndProc(ref m);
}

private void OnNonClientCalcSize(ref RECT rc)
{
rc.Top += 20;
rc.Bottom -= 3;
rc.Left += 3;
rc.Right -= 3;
}

protected override void SetClientSizeCore(int x, int y)
{
base.SetClientSizeCore(x + 6, y + 23);
}

protected override void SetBoundsCore(int x, int y, int width, int height,
BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width + 6, height + 23, specified);
}
///
 
G

Guest

Thanks a lot. Yes, there is some problem in VS at design time. I think, I
will managed it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top