Solved

window class

Posted on 2000-05-12
7
195 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now