Solved

window class

Posted on 2000-05-12
7
198 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 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
Industry Leaders: 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

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!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

733 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