Click here to Skip to main content
15,896,502 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I would like to paint the title bar by myself, but my title bar height is not the same as the system's, to increase the height of the title bar, I then respond to WM_NCCALCSIZE
But if i constantly maximize and restore,the form will become increasingly smaller
what happended?
Hope someone can answer, thank you!

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("user32")]
        public static extern int GetSystemMetrics(int nIndex);
        private const int SM_CYCAPTION = 4;
        private const int SM_CXFRAME = 32;
        private const int SM_CYFRAME = 33;
        private const int WM_NCCALCSIZE = 0x83;

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct PWINDOWPOS
        {
            public IntPtr hwnd;
            public IntPtr hwndInsertAfter;
            public int x;
            public int y;
            public int cx;
            public int cy;
            public uint flags;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct NCCALCSIZE_PARAMS
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
            public RECT[] rgrc;
            public PWINDOWPOS lppos;
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCCALCSIZE && m.WParam != IntPtr.Zero)
            {
                NCCALCSIZE_PARAMS Params;
                Params = (NCCALCSIZE_PARAMS)m.GetLParam(typeof(NCCALCSIZE_PARAMS));
                Params.rgrc[0].Top += 40 - (GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)); //caption height
                Marshal.StructureToPtr(Params, m.LParam, false);
            }
            base.WndProc(ref m);
        }
    }
}
Posted

You have to take into account the windows themes when doing your calculations so you can't hard code numbers like 40 into the Top property.

Take a look at the following link for samples on how it's done : CustomBorderForm on codeplex
 
Share this answer
 
Comments
Simon Bang Terkildsen 1-Oct-11 10:11am    
OP wrote:
i am so sorry,but his code is too complex,i just need WM_NCCALCSIZE,can you give me a simple one?
thank you very much!
Mehdi Gholam 1-Oct-11 10:15am    
Thanks Simon, go to lounge and check the joke there :)
Simon Bang Terkildsen 1-Oct-11 10:13am    
My 5
well,maybe i have found the answer?
C#
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
    base.SetBoundsCore(x, y, width, height + 40 - (GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)), specified);
}

but what is SetBoundsCore?
 
Share this answer
 
Comments
Simon Bang Terkildsen 1-Oct-11 10:17am    
what is SetBoundsCore?
Have you read the documentation

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900