for you people who are good at win32, i'm having some difficulty getting a window to be created. here is my code:
NameListControl.h
#ifndef NAME_LIST_CONTROL_H
#define NAME_LIST_CONTROL_H
#include <Windows.h>
class NameListControl
{
public:
NameListControl();
void Init(HWND parent);
void Create(HWND parent, int top, int left, int bottom, int right);
LRESULT CALLBACK NameListProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
private:
HWND m_hwnd;
};
static LRESULT CALLBACK ControlProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
static NameListControl * ControlHandle = 0;
#endif
NameListControl.cpp
#include "NameListControl.h"
NameListControl::NameListControl()
{
m_hwnd = NULL;
}
void NameListControl::Init(HWND parent)
{
WNDCLASSEX wc;
ControlHandle = this;
wc.cbSize = sizeof(wc);
wc.lpszClassName = TEXT("NameListControl");
wc.hInstance = GetModuleHandle(NULL);
wc.lpfnWndProc = ControlProc;
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hIcon = 0;
wc.lpszMenuName = 0;
wc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255,255,255));
wc.style = 0;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIconSm = 0;
if (RegisterClassEx(&wc) == 0)
MessageBox(parent, TEXT("couldn't register name list control"), TEXT("error"), MB_OK);
}
void NameListControl::Create(HWND parent, int top, int left, int bottom, int right)
{
m_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("NameListControl"), TEXT(""), WS_VISIBLE | WS_CHILD, left, top, right, bottom, parent, NULL, GetModuleHandle(NULL), NULL);
if(m_hwnd == NULL)
MessageBox(parent, TEXT("couldn't create name list control"), TEXT("error"), MB_OK);
}
LRESULT CALLBACK NameListControl::NameListProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT controlSize;
GetClientRect(hwnd, &controlSize);
HDC hdc;
hdc = BeginPaint(m_hwnd, &ps);
DrawText(hdc, TEXT("testname"), -1, &controlSize, 0);
EndPaint(m_hwnd, &ps);
break;
}
default:
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
LRESULT CALLBACK ControlProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
return ControlHandle->NameListProc(hwnd, Message, wParam, lParam);
}
despite my best efforts, no matter how or where i call NameListControl::Create , The CreateWindowEx always fails and returns null. can anyone see anything stupid that i'm missing here? Yes, i do know for a fact that i am calling NameListControl::Init way before i call Create.
EDIT: okay take it out of the class, and use this in the WM_CREATE message of my main window:
WNDCLASS wc;
wc.cbSize = sizeof(wc);
wc.lpszClassName = TEXT("NameListControl");
wc.hInstance = GetModuleHandle(NULL);
wc.lpfnWndProc = ControlProc;
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hIcon = 0;
wc.lpszMenuName = 0;
wc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255,255,255));
wc.style = 0;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIconSm = 0;
if (RegisterClassEx(&wc) == 0)
MessageBox(parent, TEXT("couldn't register name list control"), TEXT("error"), MB_OK);
HWND m_hwnd;
m_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("NameListControl"), TEXT(""), WS_VISIBLE | WS_CHILD, 0,0,100,100, hwnd, NULL, GetModuleHandle(NULL), NULL);
and it still returns NULL. i'm missing something very obvious but i just can't see it :\