From: Jimbo on
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
[i]CreateWindowEx(0,"updateBox",0,WS_VISIBLE|WS_CHILD,
0,0,100,100,hwnd,NULL,hInstance,NULL); [/i] 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
[source lang="cpp"]
#include <windows.h>
#include <cstdlib>

#include "updateBox.h"

// Global Variables
static HINSTANCE gInstance;

// Functions
LRESULT 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, msg, wParam, lParam); // Does this send
a message on to my 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
}


[/source]

Custom Control Header file
[source lang="cpp"]
#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;
};

#endif


[/source]

Custom Control Implementation
[source lang="cpp"]
#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);
}


[/source]