Set ClientRectangle in custom form in C #

In C #, I have a custom None frame style form that overrides the onPaint event and draws a custom background using the transparency key. I want to set my own value for the client rectangle (so the content will be placed inside my user border), but unfortunately the Form ClientRectangle property is read-only. I found network advice to override the WndProc method (where it sets the client size), but unfortunately I found very little information about this. This especially requires filling in the data indicated by lParam and wParam, and I really don't know how to do this in C #.

Any help?

+2
source share
2 answers

There are several things in your question that concern me ... first you want to draw your own border, and then adjust the client rectangle. This is really not possible because the client rectangle is determined when the window is moved. After defining a completely different paint message, he is responsible for drawing all non-client content. That way you can do what you offer; however, it will violate your current border color.

It would be a FAR eaiser to move all your controls from your form to a new Panel control and place it on the form. Now you can place this panel as if you were setting up the client area.

If you must begin your initial thought in order to change the client area of ​​the window, you must do the following:

private void AdjustClientRect(ref RECT rcClient) { rcClient.Left += 10; rcClient.Top += 10; rcClient.Right -= 10; rcClient.Bottom -= 10; } struct RECT { public int Left, Top, Right, Bottom; } struct NCCALCSIZE_PARAMS { public RECT rcNewWindow; public RECT rcOldWindow; public RECT rcClient; IntPtr lppos; } protected override void WndProc(ref Message m) { base.WndProc(ref m); const int WM_NCCALCSIZE = 0x0083; if (m.Msg == WM_NCCALCSIZE) { if (m.WParam != IntPtr.Zero) { NCCALCSIZE_PARAMS rcsize = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); AdjustClientRect(ref rcsize.rcNewWindow); Marshal.StructureToPtr(rcsize, m.LParam, false); } else { RECT rcsize = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); AdjustClientRect(ref rcsize); Marshal.StructureToPtr(rcsize, m.LParam, false); } m.Result = new IntPtr(1); return; } } 
+3
source

Since you are responsible for painting the entire form, it is probably easiest to define your own Rectangle content, which is positioned, say, 10 pixels from the top / left shape and has a width / height of 20 pixels less than the width / height of the form.

Then, in the Paint control, first draw the border area as usual, then call Graphics.Translate (10,10), and then draw the actual content.

0
source

Source: https://habr.com/ru/post/1344964/


All Articles