WNDCLASSEX

In this structure why does it need the size of itself?

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633577(v=vs.85).aspx

What purpose does the size serve?

Also about cbClsExtra and cbWndExtra, what use it to reserve data for your own use? Wont that automatically happen?

For HCursor what does it mean you have to explicitly set the mouse cursor shape? Dont you have to do that anyway? Or does this "resource file" contain everything?
closed account (G309216C)
Hi,

This is a perfect question because you want to understand why and how.

So WNDCLASSEX structure is a Standard Structure assigned to each Windows except from MessageBox and other information based windows.

Basically cdClsExtra can be used for other purposes such as Skinning Windows and other Custom Style. This is majority of the time set to NULL which is equal to 0.

cdWndExtra again is used for other styling and for even adding extra buttons on the Window Caption such as what Visual Studio has.

HCursor does NOT need explicit mouse cursor shape. In fact it you can use many of the Windows Default Cursor Shapes. Well You can create a Cursor resource file then call it via resource.h then extract it via HCursor of course there is no need to drop it directly to the Hard Drive because HCursor will simply do it by getting the embedded binary for the Cursor from the EXE.

Some of the values in the structure should be left to NULL , 0 , keeping in mind that some do has special circumstances in which they are needed to be set to a different value.


WNDCLASSEX is one of the crème de la crème structure of Native GUI programming.

Hope This Helps.
But how would all that work?

With cdClsExtra and cdWndExtra? Like what do you mean skinning windows and custom styles?

Also what purpose does the SIZE of it serve?
Also my book says:

"The members hIcon, hCursor, and hbrBackground are handles that in turn reference objects that represent:

➤ The application when minimized
➤ The cursor the window uses
➤ The background color of the client area of the window"


What represents the application when minimized out of those three? o.O
Also this lol, sorry if I am asking too many questions, just tell me.

from my book:

---------------------------------------------------------

RegisterClassEx(&WindowClass);

Easy, isn’t it? The address of the struct is passed to the function, and Windows extracts and squirrels away all the values that you have set in the structure members. This process is called registering the window class. Just to remind you, the term class here is used in the sense of classification and is not the same as the idea of a class in C++, so don’t confuse the two. Each instance of the application must make sure that it registers the window classes that it needs."

---------------------------------------------------------

What does it mean in a sense of classification?
Last edited on
closed account (G309216C)
Yes, It says right Cursor Window uses and such which is styling okay , then background colour which is again styling.

The size of these members are that it tells windows to create what type of windows and stuff. Without these values the windows would not know what to make.

Well also if all of it says in the Book read it. LOL
No but what does it mean the application window when minimized?

What do you mean it tells windows to create the type of windows and stuff? How can the size tell it what to do? o.O

Because look at this sample code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <windows.h>
#include <tchar.h>
     
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam);
     
// Listing OFWIN_1
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
  WNDCLASSEX WindowClass;     // Structure to hold our window's attributes
     
  static LPCTSTR szAppName = _T("OFWin");   // Define window class name
  HWND hWnd;                                // Window handle
  MSG msg;                                  // Windows message structure
     
  WindowClass.cbSize = sizeof(WNDCLASSEX);  // Set structure size
     
  // Redraw the window if the size changes
  WindowClass.style   = CS_HREDRAW | CS_VREDRAW;
     
  // Define the message handling function
  WindowClass.lpfnWndProc = WindowProc;
     
  WindowClass.cbClsExtra = 0;     // No extra bytes after the window class
  WindowClass.cbWndExtra = 0;     // structure or the window instance
     
  WindowClass.hInstance = hInstance;        // Application instance handle
     
  // Set default application icon
  WindowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     
  // Set window cursor to be the standard arrow
  WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     
  // Set gray brush for background color
  WindowClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(GRAY_BRUSH));
     
  WindowClass.lpszMenuName = NULL;          // No menu
  WindowClass.lpszClassName = szAppName;    // Set class name
  WindowClass.hIconSm = NULL;               // Default small icon
     
  // Now register our window class
  RegisterClassEx(&WindowClass);
     
  // Now we can create the window
  hWnd = CreateWindow(
          szAppName,                           // the window class name
          _T("A Basic Window the Hard Way"),   // The window title
          WS_OVERLAPPEDWINDOW,                 // Window style as overlapped
          CW_USEDEFAULT,                       // Default screen position of upper left
          CW_USEDEFAULT,                       // corner of our window as x,y.
          CW_USEDEFAULT,                       // Default window size width ...
          CW_USEDEFAULT,                       // ... and height
          NULL,                                // No parent window
          NULL,                                // No menu
          hInstance,                           // Program Instance handle
          NULL                                 // No window creation data
        );
     
  ShowWindow(hWnd, nCmdShow);                  // Display the window
  UpdateWindow(hWnd);                          // Redraw window client area 
     
  // The message loop
  while(GetMessage(&msg, NULL, 0, 0) == TRUE)  // Get any messages
  {
    TranslateMessage(&msg);                    // Translate the message
    DispatchMessage(&msg);                     // Dispatch the message
  }
     
  return static_cast<int>(msg.wParam);         // End, so return to Windows
}
     
// Listing OFWIN_2
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam)
{
     
  switch(message)                              // Process selected messages
  {
    case WM_PAINT:                             // Message is to redraw the window
      HDC hDC;                                 // Display context handle
      PAINTSTRUCT PaintSt;                     // Structure defining area to be drawn
      RECT aRect;                              // A working rectangle
      hDC = BeginPaint(hWnd, &PaintSt);        // Prepare to draw the window
     
      // Get upper left and lower right of client area
      GetClientRect(hWnd, &aRect);
     
      SetBkMode(hDC, TRANSPARENT);             // Set text background mode
     
      // Now draw the text in the window client area
      DrawText(
             hDC,                              // Device context handle
             _T("But, soft! What light through yonder window breaks?"),
             -1,                               // Indicate null terminated string
             &aRect,                           // Rectangle in which text is to be drawn
             DT_SINGLELINE|                    // Text format - single line
             DT_CENTER|                        //             - centered in the line
             DT_VCENTER);                      //             - line centered in aRect
     
      EndPaint(hWnd, &PaintSt);                // Terminate window redraw operation
      return 0;
     
    case WM_DESTROY:                           // Window is being destroyed
      PostQuitMessage(0);
      return 0;
  }
  return DefWindowProc(hWnd, message, wParam, lParam);
}



The size is set before everything else, so doesnt that mean the size would always be the exact same? So why do you have to set it?

And also what does it mean in a sense of classification?
Belatedly...

In this structure why does it need the size of itself?

The cb member is for versioning purposes.

In the future, they might want to add additional members to the end of the struture. In this case, they need to be able to tell if they are being called by an app compiled using an older declaration of the structure or the current one.

Also about cbClsExtra and cbWndExtra, what use it to reserve data for your own use?

Wont that automatically happen?

The space referred to is that which is reserved for you by the system along with the other data it keeps about the registered classes (the stype, the address of the wndproc, etc).
- cbClsExtra asks for storage for data accessible by all instances of the class (accessed using GetClassLongPtr/SetClassLongPtr)
- cbWndExtra asks for space to be allocated for each separate instance (accessed using GetWindowLongPtr/SetWindowLongPtr)

This storage can be used for whatever purpose you want. But the system will only let you use a limited amount of space (40 Bytes, in each case). So if you need bulky data looked after, you must allocate storage for it on the heap and then get the window (using SetWindowLongPtr) or class (using SetClassLongPtr) to look after the pointer to the storage for you.

(btw - cbClsExtra and cbWndExtra should be set to 0 by default; not NULL, as they're sizes not pointers)

For HCursor what does it mean you have to explicitly set the mouse cursor shape? Dont you have to do that anyway? Or does this "resource file" contain everything?

If you provide a cursor when you register, Windows will handle setting the cursor for you automatically. It you use set this member to NULL, then you've got to set the cursor yourself/

(In a general purpose app you should provide the cursor; for, e.g., a drawing app where the curson need to change frequently depending on what the user is doing, then you would use NULL and then set the cursor as required yourself.)

If you want to use a custom cursor you either need to build it into your app's resource and load it from there, or load it from a .cur file. But usually you use a standard cursor which you acquire from the system. (You can also create a cursor on the fly, but that's less usual.)

Andy

About Window Classes
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633574%28v=vs.85%29.aspx
Last edited on
Thanks andywestken that made alot more sense lol. So if you dont specify the cursor when it goes over the menu you have to do it all yourself, rather then letting windows handle it?

Thanks for the help!

Also can you answer just these questions:

My book says:

----------------------------------------------------------------------------------

"The members hIcon, hCursor, and hbrBackground are handles that in turn reference objects that represent:

➤ The application when minimized
➤ The cursor the window uses
➤ The background color of the client area of the window"

----------------------------------------------------------------------------------

What represents the application when minimized out of those three? o.O


Also why does windows need its own data types? Can you tell me the exact problem happening?



MSDN and my book say:

----------------------------------------------------------------------------------

"CW_USEDEFAULT is valid only for overlapped windows; if it is specified for a pop-up or child window, the x and y parameters are set to zero."

----------------------------------------------------------------------------------

Why is it only valid for overlapped windows? By overlapped windows it means windows that their can be two of right or what I am confused.

Thanks the long and detailed reply, you cleared up alot of confusion!
Last edited on
No but what does it mean the application window when minimized?

The application icon (hIcon, when you register the class, or whatever you changed it to) is displayed in the application's button on the task bar.

In the olden days -- of (16 bit) Windows 3.1 and Windows NT 3.5 -- there was no taskbar. What happened when you minised an app was it was shrunk to an icon on the desktop.

What represents the application when minimized out of those three?

The icon (hIcon)

Andy
Last edited on
Also why does windows need its own data types?

Two main reasons, from what I understand.

#1 Windows predates the standard C++ types

#2 Different versions of Windows use different underlying native types for the Windows types (strictly speaking, typedefs, when it comes to DWORD, LPARAM, etc.)

First there was Win16. Then Win32 and Win64. Some of the types are unchanged, but others have changed. For example, WPARAM was 16 bit in Win16 days (a WORD) where it is now 32 bit (for Win32 and Win64). The use of the typedefs meant some code was a bit easier to port as the variable type names did not have to be changed in the code.

Andy

PS That's really too many questions for one thread!!

Porting
http://en.wikipedia.org/wiki/Porting
Last edited on
Oh ok thanks!


Sorry about too many questions, the API is a bit confusing at first. Alot to learn.


Thanks so much!
Last edited on
Should I make a new thread for my other 2 questions that I posted before lol. I already asked you to answer toooo many
Last edited on
Topic archived. No new replies allowed.