Hi Dave,
Sorry for letting you wait for so long.
After doing a lot of research, I found that we should first handle
WM_NCCALCSIZE to decrease the client area size. Then the non-client area is
getting larger. Then we should handle the WM_NCPAINT message to paint the
menu and other non-client ourselves.
I have writen a little sample for you to show to handle these 2 messages.
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
enum WM_MESSAGE
{
WM_NCCALCSIZE = 131,
WM_NCPAINT = 133,
WM_NCHITTEST = 132,
WM_NCLBUTTONDOWN = 161,
}
[StructLayout(LayoutKind.Sequential)]
struct NCCALCSIZE_PARAMS
{
public RECT rgrc0, rgrc1, rgrc2;
public IntPtr lppos;
}
[StructLayout(LayoutKind.Sequential)]
struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x, y;
public int cx, cy;
public int flags;
}
[DllImport("User32.dll")]
private extern static IntPtr GetWindowDC( IntPtr hWnd );
[DllImport("User32.dll")]
private extern static int ReleaseDC( IntPtr hWnd, IntPtr hDC );
private Rectangle _rcButton = Rectangle.Empty;
protected override void WndProc(ref Message m)
{
switch ( m.Msg )
{
case (int)WM_MESSAGE.WM_NCCALCSIZE :
NCCALCSIZE_PARAMS csp;
csp = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure( m.LParam,
typeof(NCCALCSIZE_PARAMS));
Font myFont = new Font(FontFamily.GenericSerif, 35, FontStyle.Italic,
GraphicsUnit.Pixel);
Graphics g=this.CreateGraphics();
Size mySize = g.MeasureString(this.menuItem1.Text, myFont).ToSize();
Console.WriteLine(mySize.Height.ToString());
Console.WriteLine(csp.rgrc0.Top.ToString());
csp.rgrc0.Top =
csp.rgrc0.Top -SystemInformation.MenuHeight+mySize.Height;
Console.WriteLine(csp.rgrc0.Top.ToString());
Marshal.StructureToPtr( csp, m.LParam, false );
break;
case (int)WM_MESSAGE.WM_NCPAINT :
{
IntPtr hDC = GetWindowDC( m.HWnd );
Graphics g1 = Graphics.FromHdc( hDC );
Font myFont1 = new Font(FontFamily.GenericSerif, 35, FontStyle.Italic,
GraphicsUnit.Pixel);
Size mySize1 = g1.MeasureString(this.menuItem1.Text, myFont1).ToSize();
g1.FillRectangle(new SolidBrush(SystemColors.Control), 0,
SystemInformation.CaptionHeight, this.ClientRectangle.Width,
mySize1.Height+5);
ReleaseDC( m.HWnd, hDC );
}
break;
}
base.WndProc (ref m);
}
This sample is not perfect, which need a lot work to be done. Hope it helps
you.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.