VC Create a client Area window, drag and drop operations between list boxes

Source: Internet
Author: User
Tags valid

Create client Area window

    1. Drag and drop operations between list boxes

How does the system identify a client or non-client area of a window when sending a painting (paint) message? When I use:: CreateWindow to create a window, how to specify the client area rectangle?

When you create a window, you do not have to specify a client area, and the client area is not specified when a wm_nccalcsize message is received. Whenever Windows wants to know the size of the client area of a window, it sends this message. Implement onnccalcsize processing routines in MFC. The handler function has two parameters, which are converted from WPARAM and LPARAM:

void OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp);

The function tells the application whether to "compute a valid rectangle" (which will be mentioned later); The nccalcsize_params structure holds three rectangular arrays, the first one to save the client area of the window. Here is the basic pattern for implementing Onnccalcsize:

// got WM_NCCALCSIZE
void CMainFrame::OnNcCalcSize(...)
{
   // do default thing (important!)
   CFrameWnd::OnNcCalcSize(...);
   CRect& rc = (CRect&)lpncsp->rgrc[0];
   // adjust rc; eg, rc.DeflateRect(...);
}    

I wrote a small program, Nccalc, which shrinks the standard client area by 7 pixels and draws the area into a three-dimensional appearance color (typically light gray). Figure 1 lists the essence of the source code. The important function is the onnccalcsize, which adjusts the client area rectangle size, Onncpaint draw the boundary. The drawing code is simple and straightforward, and I don't go further. Please download the source code for details.

If you overwrite the wm_nccalcsize/onnccalcsize of the main window, be sure to call the default window processing routines for the base class to implement the default processing. This way the program will have the default client rectangle when it runs, and then you can resize it. Similarly, the default processing of the base class should also be called in Onncpaint/wm_ncpaint. Otherwise, Windows does not draw boundaries, scroll bars, or other standard non-client area elements. If you implement your own window class, like customizing a toolbar or palette, where you want to compute the client rectangle and draw the handle, you can do without calling the base class Default window procedure. Either way, when the window receives the Wm_ncpaint message, you are responsible for drawing the entire non-client area.

Some people may want to know what the bcalcvalidrects and other rectangles in nccalcsize_params are for. If you read the document, you will find that the semantics of wm_nccalcsize are quite complex. The document says if Bcalcvalidrects is TRUE: The application should indicate which part of the client area contains valid information. The system copies the valid information to the specified area in the new client area "In this case, the second [rectangle] contains the coordinates of the client area of the window before being moved or resized" although all these descriptions seem clear enough, I am not quite sure. I have never seen that application using these extra rectangles. The apps I've seen ignore bcalcvalidrects and simply modify the first rectangle to set up the client area in Nccalcsize_params.

I mention this because, as the document says, if Wparam/bcalcvalidrects is FALSE, then LPARAM does not point to the nccalcsize_params structure, but to the individual RECT, the client area, but MFC in all cases Aram cast to Nccalcsize_params. This seems like a bug, though if you only modify the first rectangle in the Nccalcsize_params, your program will never break down. I ran some test programs to make sure Windows only once used Bcalcvalidrects=false to send wm_nccalcsize when the window was first created. Then, Windows uses Bcalcvalidrects=true to send wm_nccalcsize regardless of how the window is resized. You must set up the client area for both cases so that you can display your window normally.

Alas, I am afraid that I have not yet clarified what bcalcvalidrects is for. Microsoft's bosses have not given enough documentation to explain this mysterious parameter, only that it was extended from Windows 3.1 in the ancient times. Now as long as you are always dealing with both cases, and only modify the first rectangle, everything will be OK.

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.