   [Win32 C++] Control Confusion
  Hello, Why wont this simple way I made of creating a Custom Control(in Win32 API) work when I want to use the class as so:- I will declare the custom control using CreateWindowEx(0,"updateBox",0,WS_VISIBLE|WS_CHILD,0,0,100,100,hwnd,NULL,hInstance,NULL);  in my WinMain WndProc WM_CREATE: msg.- Will the updateBox object be created? Will the UpdateBoxWndProc() be created? Will the window receive messages? Is this the way to code custom controls?My code, any advice would be really helpful to tell me how to get this to work. I have commented where the errors occur.WinMain.cpp#include <windows.h>#include <cstdlib>#include "updateBox.h"// Global Variablesstatic HINSTANCE gInstance;// FunctionsLRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){    switch(msg)    {   case WM_CREATE:   {   RegisterUpdateboxClass(); // Register custom control HWND updateB = CreateWindowEx(0,"updateBox",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,   0,0,100,100,hwnd,NULL,gInstance,NULL); if (!updateB) {  MessageBox(hwnd,"Failed to create Update Box Control.","Error",MB_OK|MB_ICONERROR);   }   }   break;   case WM_CLOSE:  DestroyWindow(hwnd);   break;   case WM_DESTROY:  PostQuitMessage(0);   break;    }    return DefFrameProc(hwnd, NULL, msg, wParam, lParam); // Error Here I dont know what to put for the MDI (argument2) to pass the messages onto the custom control?}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){    // create dialog...I forgot the code to do it :P but here I open my main dialog}Custom Control Header file#ifndef UPDATEBOX_H#define UPDATEBOX_H#include <windows.h>void RegisterUpdateboxClass();class updateBox {  public: updateBox(); LRESULT CALLBACK UpdateBoxWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);    private: POINT pos;};#endifCustom Control Implementation#include <windows.h>#include "updateBox.h"using namespace std;/// Register Custom Control Class ////// ///void RegisterUpdateboxClass() {   // Post: Register custom class   WNDCLASSEX wc; wc.cbSize   = sizeof(wc);wc.lpszClassName = "updateBox";   wc.hInstance= GetModuleHandle(NULL);   wc.lpfnWndProc   = UpdateBoxWndProc;    // THIS FUNCTION IS A MEMBER OF CLASS updateBox so I get an error here?   wc.hCursor  = LoadCursor(NULL,IDC_ARROW);   wc.hIcon    = NULL;   wc.lpszMenuName  = NULL;   wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);   wc.style    = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;   wc.cbClsExtra    = 0;   wc.cbWndExtra    = sizeof(updateBox*);   wc.hIconSm  = NULL; if(!RegisterClassEx(&wc))    {   MessageBox(NULL, "Update Box Class Registration Failed", "Error",  MB_ICONEXCLAMATION | MB_OK);    }  }/// Update Box Control Class ////// ///updateBox::updateBox(){   // Post: Constructor   }LRESULT CALLBACK updateBox::UpdateBoxWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){   // Post: Receives all UpdateBox window messages updateBox *udb = (updateBox*)GetWindowLong(hwnd,0); switch (msg)    {    case WM_NCCREATE:    {    udb = (updateBox*)malloc(sizeof(updateBox));   if (udb==false) {   MessageBox(NULL,"Failed to create Update Box window","Error",MB_OK|MB_ICONERROR);   return 0;    }   SetWindowLong(hwnd,0,(LONG)udb); // Attach custom class to this window.    }    break;    case WM_PAINT:    {    HDC hdc;    PAINTSTRUCT ps;    hdc = BeginPaint(hwnd,&ps);HRGN foo = CreateRectRgn(0,0,100,100);    FillRgn(...);    EndPaint(hwnd,&ps);    }   break;    case WM_NCDESTROY:    free(udb);    break;    default:    break;   }   return DefWindowProc(hwnd, msg, wParam, lParam);}[Edited by - gretty on April 10, 2010 7:24:47 PM]
  If you want the window procedure to be a class member function, you are going to have to make it a static member function. As it is, updateBox::UpdateBoxWndProc() is implicitly expecting its first argument to be an  updateBox* -- that is, the this pointer -- so its signature isn't a valid WndProc.You could make a handleMessage() routine that isn't static then make updateBox::UpdateBoxWndProc() be static and have it get a pointer to the C++ object to which a message refers by calling GetWindowLong() and then have it call handleMessage() using that pointer.I'm not sure why you're allocating an updateBox sized chunk of memory using malloc() and stuffing a pointer to it in the zeroth slot accessible by SetWindowLong, but unless you have a good reason to be doing it that way you should just use new and delete like normal and you probably meant to call SetWindowLong() using GWL_USERDATA? i.e. I think you might mean SetWindowLong(hwnd, GWL_USERDATA, static_cast<LONG>(new updateBox()));Anyway that's one way of doing it. The other way is to have your custom class be the thing that calls CreateWindow in its constructor and then store the this pointer in the GWL_USERDATA slot.[Edited by - jwezorek on April 9, 2010 8:52:35 PM];Quote:Original post by jwezorekIf you want the window procedure to be a class member function, you are going to have to make it a static member function. As it is, updateBox::UpdateBoxWndProc() is implicitly expecting its first argument to be an  updateBox* -- that is, the this pointer -- so its signature isn't a valid WndProc.You could make a handleMessage() routine that isn't static then make updateBox::UpdateBoxWndProc() be static and have it get a pointer to the C++ object to which a message refers by calling GetWindowLong() and then have it call handleMessage() using that pointer. I'm not sure why you're allocating an updateBox sized chunk of memory using malloc() and stuffing a pointer to it in the zeroth slot accessible by SetWindowLong, but unless you have a good reason to be doing it that way you should just use new and delete like normal and you probably meant to call SetWindowLong() using GWL_USERDATA? i.e. I think you might mean SetWindowLong(hwnd, GWL_USERDATA, static_cast<LONG>(new updateBox()));Anyway that's one way of doing it. The other way is to have your custom class be the thing that calls CreateWindow in its constructor and then store the this pointer in the GWL_USERDATA slot.Thanks for your reply :). The first part of what you said I kindof get but alot went over my head :P. Would you be able to give an example of how/what you mean referring to the handleMessage() routine?I am malloc()'ing really because I was following a tutorial a read concerning making my own custom control but I like using new & delete instead, thanks for the advice :);  I wouldn't recommend using GWLP_USERDATA to store your instance pointer. The user of the control (yourself maybe in months from now when you've forgotten) should be able to use the user data space of a control for something application specific.When you register the window class, set cbWndExtra to something like sizeof(void*) then pop the instance pointer in there withSetWindowLongPtr(Hw,0,reinterpret_cast<LONG_PTR>(ptr));I tend to do this in response to the WM_CREATE message, allocating a structure that holds all the state for the custom control and popping it in, then grabbing it back in response to WM_DESTROY and deleting it.   