Solved

window class

Posted on 2000-05-12
7
197 Views
Last Modified: 2008-03-06
I need an example of how to use the wndclass.cbClsExtra
0
Comment
Question by:liuxiao
  • 4
  • 2
7 Comments
 
LVL 1

Expert Comment

by:yz
ID: 2804581
maybe you can use GetClassInfo to retreive the whole wndclass, and access the extra bytes appended to this class. If you put a pointer to a class or structure, then you can access them, I'm not sure.
0
 
LVL 4

Accepted Solution

by:
captainkirk earned 50 total points
ID: 2805248
Here's some stuff from MSDN that will answer that for you...

The cbClsExtra and cbWndExtra fields in the WNDCLASS structure specify the number of extra bytes Windows will allocate for each class (cbClsExtra) and for each instance of a window from the class (cbWndExtra). These fields must be set to zero if an application does not require extra class or window bytes. If you accidentally use an unreasonably large value for one of these fields, Windows uses that value to allocate extra bytes. I suggest that you use no more than 4 extra bytes per class or window. If you use a higher value, Windows will allocate the extra bytes from User's heap, and will thus run out of system resources quickly. If your application needs more space, you should allocate a block from either the local heap or the global heap, and then store a pointer or handle to the block in the extra bytes. For example, the toolbar sample uses the following code to store the data for each toolbar instance:

#ifdef WIN32
#define SETWINDOWPOINTER(hwnd, name, p)    SetWindowLong(hwnd, 0, (WORD)p)
#define GETWINDOWPOINTER(hwnd, name)      ((name)GetWindowLong(hwnd, 0))
#define ALLOCWINDOWPOINTER(name, size)    ((name)malloc(size))
#define FREEWINDOWPOINTER(p)              free(p)
#else
#define SETWINDOWPOINTER(hwnd, name, p)   SetWindowWord(hwnd, 0, (WORD)p)
#define GETWINDOWPOINTER(hwnd, name)      ((name)GetWindowWord(hwnd, 0))
#define ALLOCWINDOWPOINTER(name, size)    ((name)LocalAlloc(LPTR, size))
#define FREEWINDOWPOINTER(p)              LocalFree((HLOCAL)p)
#endif

LRESULT CALLBACK ToolbarWndProc(HWND hWnd, UINT wMsg, WPARAM wParam,
                                LPARAM lParam)
{
    BOOL fSameButton;
    PTBBUTTON ptbButton;
    PTBHDR pTBHdr;
    int iPos;
    BYTE fsState;

    pTBHdr = GETWINDOWPOINTER(hWnd, PTBHDR);

    switch (wMsg) {
    case WM_CREATE:

         .
         .
         .
          // Create the state data for this toolbar.

        pTBHdr = ALLOCWINDOWPOINTER(PTBHDR, sizeof(TBHDR)-sizeof(TBBUTTON));
        if (!pTBHdr)
          return -1;

        // The structure is initialized to all NULL when created.

        pTBHdr->hwndCommand = lpcs->hwndParent;

        SETWINDOWPOINTER(hWnd, PTBHDR, pTBHdr);
   break;

    case WM_DESTROY:
        if (pTBHdr) {
            .
            .
            .
            FREEWINDOWPOINTER(pTBHdr);
            SETWINDOWPOINTER(hWnd, PTBHDR, 0);
          }
            .
            .
            .
   break;

   case WM_PAINT:
        ToolbarPaint(hWnd, pTBHdr);
   break;
..
..
..
}

0
 

Author Comment

by:liuxiao
ID: 2806649
Hi captainkirk:
I can find the definition in help file. My question is what information we have to store it in extra memory for the class,I don't care it's larger or less than 4 bytes. Since there is no example in help file, My question is why we need it (I got a good example for cbWndExtra)  

in your example I hope more comments why toolBarPaint need the extra bytes and what's inside, also give me part of the class difinition (part of the WinMain, wndclass.cbClsExtra and wndclass.cbWndExtra )
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 4

Expert Comment

by:captainkirk
ID: 2811195
These values are for the programmer's use and for any purpose you desire... it's for storing extra data along with the class, kind of like using the 32 bit value that comes with certain controls...
0
 

Author Comment

by:liuxiao
ID: 2812796
I just need an example: about the programmer's desire for any purpose they used extra memory
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2817816
How about, for example, you want a structure of data to be accessible to the class:

typedef struct _YOURSTRUCT
{
    int nSomeData;
    BOOL bSomeFlag;
    long lSomeLongData;
} YOURSTRUCT; // end struct def

....

YOURSTRUCT* ps = (YOURSTRUCT*)malloc(sizeof(YOURSTRUCT));

WNDCLASS wc;

wc.lpszClassName = "YourAppClass";
wc.lpfnWndProc = ::DefWndProc;
wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDR_MAINFRAME);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.cbClsExtra = ps;
wc.cbWndExtra = 0;

AfxRegisterClass(&wc);
   

NOTE: if you are using MFC, the extra bytes are not used and are set to zero...
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2822009
thanks....
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question