Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

window class

Posted on 2000-05-12
7
Medium Priority
?
206 Views
Last Modified: 2008-03-06
I need an example of how to use the wndclass.cbClsExtra
0
Comment
Question by:liuxiao
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

636 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