Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
C++
#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
C++
#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:

C#
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 :\
Posted
Updated 19-Nov-11 17:31pm
v4
Comments
Chuck O'Toole 19-Nov-11 22:20pm    
Sorry but I don't see anything here that instantiates the class, inits, or calls the create function.
FatalCatharsis 19-Nov-11 22:28pm    
void NameListControl::Create() calls the CreateWindowEx. these are just a couple source files that contains the class, i have this massive other application, which i didn't link where i do create an instance of the class globally, call init within winmain, and then create in the WM_CREATE message of the wndproc of my main window class.
Andrew Brock 20-Nov-11 3:17am    
try replacing the 2nd last parameter, GetModuleHandle(NULL), with the hInstance which is passed into your WinMain() function
FatalCatharsis 20-Nov-11 12:14pm    
no dice :\
Chuck O'Toole 20-Nov-11 7:06am    
What does GetLastError() say after CreateWindowEx() returns NULL?

1 solution

just gonna submit a solution to get it off the unanswered list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900