Modify the client area of a form

Source: Internet
Author: User

Windows window styles have relatively fixed form styles in windows. Of course, these styles may be greatly changed according to the windows theme, just like the skin. However, windows also provides some APIs that allow us to modify the default form style to meet their respective style requirements, such as the C # redrawing Windows form title bar and border example. Windows operating systems of different versions and themes have different styles. Apart from Title fonts, color changes, and form shapes. For example, some are rounded rectangles, some are real-angle rectangles, some form borders are wider, some are narrower, some are higher, some are shorter, and so on. Whether the form is a rounded rectangle or not. I have already written this in setjavaswrgn function application, drawing a polygon form, and used this method to modify the form shape. This article focuses on how to modify the Border thickness of the form and the height of the title bar, and proposes some solutions. Because windows applications generally have different appearances in different versions of windows operating systems and themes, you must understand the shape of the form and how to modify the border and title bar height to ensure consistency. WM_NCCALCSIZE: the message is sent when the size and location of the windows client area need to be recalculated. Therefore, blocking the message can control the size and location of the windows form client area. WM_NCCALCSIZE is defined as: [c ++] # define WM_NCCALCSIZE 0x0083 C # defined as: [c #] const int WM_NCCALCSIZE = 0x0083; when the Message Parameter wParam is TRUE, the lParam parameter is a pointer of the NCCALCSIZE_PARAMS type, which contains information that can be used to calculate the size and location of the customer zone. NCCALCSIZE_PARAMS Signature: [c ++] typedef struct tagNCCALCSIZE_PARAMS {RECT rgrc [3]; PWINDOWPOS lppos;} NCCALCSIZE_PARAMS, * signature; C # statement: [c #] struct NCCALCSIZE_PARAMS {public _ RECT rcNewWindow; public _ RECT rcOldWindow; public _ RECT rcClient; IntPtr lppos;} struct _ RECT {public int left; public int top; public int right; public int bottom;} NCCALCSIZE_PARAMS struct C # declaration of NCCALCSIZE_PARAMS and C The declaration of/C ++ seems a little different. C/C ++ defines a RECT array, while C # converts it into three separate RECT fields, the current effect is the same, but one uses [] for all objects, and the other is for self-access. That is, rgrc [0] of the former corresponds to rcNewWindow of the latter, rgrc [1] of the former corresponds to rcOldWindow, and rgrc [2] of the former corresponds to rcClient. Only C # is defined separately, mainly to better illustrate the meaning of the three RECT. The first RECT or rcNewWindow stores the coordinates of the form after the form is moved or the size is modified. This is the information that the window will be applied immediately, that is, the window will be immediately moved to the coordinates (rcNewWindow. left, rcNewWindow. top), and the size will be adjusted to (rcNewWindow. right-rcNewWindow.left, rcNewWindow. bottom-rcNewWindow.top); while the second RECT or rcOldWindow stores the coordinate information of the form before moving or the size is changed; the third RECT or rcClient stores the coordinate information of the customer zone before moving or modifying the size. If the form is a child form, the coordinates are relative to the parent form. Otherwise, the coordinates are relative to the screen. However, after the message is processed, the first RECT or rcNewWindow is used to store the coordinate information of the customer zone after moving or changing the size, that is, enough results are calculated. It can also be inferred that the calculation process is calculated using the rcNewWindow coordinates and the height of the Form title bar and the thickness of the form border under the current operating system and the topic. Assume that the form height is CaptionHeight and the Border thickness is BorderWidth. Then, the customer region can be calculated using the formula to obtain the coordinates of the customer region: [c #] client. left = rcNewWindow. left + BorderWidth client. top = rcNewWindow. top + BorderWidth + CaptionHeight client. right = rcNewWindow. right-BorderWidth client. bottom = rcNewWindow. bottom-BorderWidth In fact, both the Border thickness and the height of the form can be retrieved from the class System. windows. forms. systemInformation, but you need to select different thickness and height based on the form type and topic. You can intercept the WM_NCCALCSIZE message when creating the window, obtain the NCCALCSIZE_PARAMS parameter, and calculate the Border thickness and Title Bar Height of the Form Based on rcNewWindow and rcClient. The purpose of modifying the text before the customer region is to describe the coordinate information of the customer region, which is calculated based on the new coordinate information of the form, the Border thickness of the form, and the height of the title bar. Therefore, we can intercept the WM_NCCALCSIZE message and manually modify the coordinate information of the form, thus affecting the calculation result of the coordinate information of the customer zone. As we can see from the formula above, the increase or decrease of the new window coordinates is the increase or decrease of the coordinates of the customer zone. Therefore, the following message code is intercepted to achieve a widening effect and increase the effect of the customer zone: [c #] private void AdjustClientRect (ref _ RECT rcClient) {rcClient. left-= 3; rcClient. right + = 3; rcClient. bottom + = 3;} const int WM_NCCALCSIZE = 0x0083; protected override void WndProc (ref Message m) {switch (m. msg) {case 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);} m. result = new IntPtr (1);} break;} base. wndProc (ref m);} because the width and height of the customer zone are increased, but the form size and position are not changed, the running effect is that the thickness of the left and right sides and the bottom border is obviously reduced, the title bar does not change because we have not adjusted the rcClient. top field. Therefore, if you re-paint the form and want the form to behave the same in the windows operating system and the topic of each version, you can ensure that the section thickness of the form is BorderWidth, you can adjust the Code as follows: [c #] int BorerWidth = 1; private void AdjustClientRect (ref _ RECT rcClient) {rcClient. left-= SystemInformation. frameBorderSize. width-BorerWidth; rcClient. right + = SystemInformation. frameBorderSize. width-BorerWidth; rcClient. bottom + = SystemInformation. frameBorderSize. width-BorerWidth;} rcClient is not set here. top, to the top of the form The rcClient. top cannot achieve the expected results. You need to redraw the title bar. For details, see C # redrawing the title bar and border of Windows Forms. The running effect is as follows: Of course, you can also use this method to achieve the effect of a borderless window without setting the FormBorderStyle attribute to None. You can simply modify the code to achieve: [c #] private void AdjustClientRect (ref _ RECT rcClient) {rcClient. left-= SystemInformation. frameBorderSize. width; rcClient. right + = SystemInformation. frameBorderSize. width; rcClient. bottom + = SystemInformation. frameBorderSize. width; rcClient. top-= SystemInformation. frameBorderSize. width + SystemInformation. captionHeight ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.