简体   繁体   中英

Win32: Passing lParam to CreateWindow and keeping track of open windows

To start, here is the description from the document I was given. NOTE: all I had to do was change existing code to meet these requirements

Change the code to create 4 windows organized as shown in the demo. When the user closes a window, if there are other windows still open it does NOT kill the program. The user must close ALL of them to stop program,

You will need to keep track of the number of windows opened (and subtract when they are closed). NO GLOBALS ALLOWED. NO STATICS ALLOWED.

In addition your program must initialize the count using the lParam and CREATESTRUCT.

Possible functions and structs:

SetWindowLong()
GetWindowLong()
SetClassLong()
GetClassLong()
CREATESTRUCT

Ok now, here is what I ended with. I could open the 4 required windows, and had it so closing one window (sending the WM_DESTROY Message) did not end the entire program. What I am confused about is the passing of an lParam and using the CREATESTRUCT.

Here is my wndproc.c:

 LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    HDC     hdc;
    PAINTSTRUCT ps;
    RECT    rect;
    int number = GetClassLongPtr(hwnd, 0);

    switch (message){
        case WM_CREATE:
            if(number == 0){
                SetClassLongPtr(hwnd, 0, (LONG)((CREATESTRUCT*)lParam)->lpCreateParams);
            }
            number++;
            return 0;

        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);

            GetClientRect(hwnd, &rect);
            DrawText(hdc, TEXT("Unique yet the same!"), -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER);

            EndPaint(hwnd, &ps);
            return 0;

        case WM_DESTROY:
            number--;
            if(number == 0){
                PostQuitMessage(0);
            }
            return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);

I'm trying to get the variable, which I pass as the lParam of the first window that is created, to be set as the Class Extra. Does that make sense? Here is my main.c:

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCMLine, int iCmdShow){ static TCHAR szAppName[] = TEXT ("HelloApplication"); HWND hwnd; MSG msg; WNDCLASS wndclass; LONG* count;

wndclass.style      = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = HelloWndProc;
wndclass.cbClsExtra = 5;
wndclass.cbWndExtra = 0;
wndclass.hInstance  = hInstance;
wndclass.hIcon      = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor    = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;


if (!RegisterClass (&wndclass)){
    MessageBox (NULL, TEXT ("This program requires Windows 95/98/NT"),
                szAppName, MB_ICONERROR);
    return 0;
}

hwnd = CreateWindow(szAppName,      
                    TEXT("Hello World for Windows"), 
                    WS_OVERLAPPEDWINDOW,    
                    100,        
                    50,     
                    400,        
                    300,        
                    NULL,               
                    NULL,           
                    hInstance,          
                    count = 0);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);

Any help would be appreciated; I've already been quizzed on this info in my class and did not understand it. I'm posting this question for my own understanding only.

Thanks.

As you have to count the number of windows (instances) of the same window class, the counter needed can be seen as a static data member of the window class.

So the goal is to add some extra data (here an integer counter) to the window class. To do so, one needs to tell windows to allocate this extra data. This can be achieved by passing the right values to RegisterClass() . For the counter needed, set the member cbClsExtra of the structure of type WNDCLASS , which's reference is passed to the RegisterClass() , to the size of the integer counter.

To access the windows class' static data (and with it the integer counter) in the message dispatcher's callback method use GetClassLongPtr() .

As I assume this is homework I leave the rest of the game as an exercise ... ;-)

SetClassLongPtr - this function operates on the memory that is associated with the class of your window. This is essentially a static variable. In fact you have simple static counter of opened windows. It is just hidden under a smoke screen.

Technically, from the point of C/C++, your counter is not static/global variable . But from the conceptual stand point - it is.

lParam and CREATESTRUCT are non important details here.

The wrongness, the glasses they do nothing!

  • You can't pass an auto variable to CreateWindowEx , and expect the address of that variable (rather than the value that you're actually passing) to come through
  • You can't assign a value to an auto variable ( int number = GetClassLongPtr(hwnd, 0); ), make changes to that variable, and expect them to be persistent.

In short:

  • Pass a pointer to CreateWindowEx , not a value
  • Dereference that pointer when you want to make changes to the value stored therein

You may need to refresh your knowledge on pointers .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM