[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

window class

Posted on 2000-05-12
7
Medium Priority
?
208 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 100 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

831 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