为什么我不能让CreateWindowEx在这里工作? [英] why can't i get CreateWindowEx to work here?

查看:73
本文介绍了为什么我不能让CreateWindowEx在这里工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些擅长win32的人来说,我很难创建一个窗口.这是我的代码:

NameListControl.h

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




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);
}



尽管我已尽力,但无论我如何或在何处调用NameListControl :: Create,CreateWindowEx始终失败并返回null.有人能看到我在这里想念的任何愚蠢的东西吗?是的,我确实知道我在调用Create之前先调用NameListControl :: Init的事实.

好的,把它带出课堂,并在我的主窗口的WM_CREATE消息中使用它:



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.

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);



并且它仍然返回NULL.我缺少一些非常明显的东西,但我看不到它:\



and it still returns NULL. i''m missing something very obvious but i just can''t see it :\

推荐答案

只是要提交解决方案以将其从未答复的列表中删除.
just gonna submit a solution to get it off the unanswered list.


这篇关于为什么我不能让CreateWindowEx在这里工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆