- Mixing C WINAPI code with classes. Need a pointer back to my class from CreateWindow
- Posted by Angus Comber on May 16th, 2004
Hello
In a class I am calling Createwindow etc to create a window. Then I have a
callback function which handles user actions.
I want to pass the this pointer from the class somehow when I create the
window so that the callback function can then call a class member function.
I see in WNDCLASSEX there is a value cbClsExtra and cbWndExtra. Can I store
the pointer in there? What is the best way to do this? Also how do I then
extract the pointer in my callback function?
Angus Comber
angus@NOSPAMiteloffice.com
- Posted by Klueless on May 17th, 2004
"Angus Comber" <angus@NOiteloffice.com.PLEASENOSPAM> wrote in message news:40a7e90e$0$6324$65c69314@mercury.nildram.net. ..
The last argument of CreateWindow(Ex) is lpParam. You can pass the
"this" pointer here. In your WndProc for this window, handle the
WM_NCCREATE message. The LPARAM of the WM_NCCREATE
message points to a CREATESTRUCT structure. The lpCreateParams
member of this structure is the last argument that was passed to CreateWindow(Ex).
In your handler for WM_NCCREATE you want to associate the
data which was passed into CreateWindow(Ex) with the window somehow
so that when you encounter the window again in the future, you find it
again easily. Use SetWindowLongPtr and GetWindowLongPtr for this.
Specify an appropriate non-zero value for the cbWndExtra member of the
WNDCLASSEX structure used with the RegisterClassEx function.
- Posted by Derek Baker on May 17th, 2004
Klueless wrote:
Basically what Klueless says, though I do it slightly differently:
Here's the this pointer passed in:
hwnd = CreateWindowEx (
0,
szClassName,
szClassName,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
rcWindow.left,
rcWindow.top,
rcWindow.right,
rcWindow.bottom,
HWND_DESKTOP,
0,
hInst,
(void*) this
);
I get it out in WndProc:
if(message == WM_CREATE)
{
SetWindowLong(hwnd, GWL_USERDATA,
(DWORD)((LPCREATESTRUCT)lParam)->lpCreateParams);
}
App* mythis = (App*)GetWindowLong(hwnd, GWL_USERDATA);
switch (message) /* handle the messages */
{
--
Derek
- Link between setup classes and interface classes. (Drivers) by Guillaume
- C++: Pointer to a string in a class (Programming) by Thomas
- DialogBox function and C++ classes in WinAPI (Development Resources) by Nick
- turning the mouse pointer back on (Drivers) by Martin
- Bringing in open-source classes to VC++ 6.0 with or without Class Wizard (Development Resources) by Gary

