Win32按钮问题 [英] Win32 button problem

查看:164
本文介绍了Win32按钮问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的窗口添加一个按钮..

这是我尝试过的代码..



I want to add a button to my window..
And here's the code i've tried..

#include <Windows.h>
#include <tchar.h>

LRESULT CALLBACK winproc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int cmdShow)
{
	TCHAR className[] = _T("button test");

	WNDCLASS winclass;
	winclass.cbClsExtra = 0;
	winclass.cbWndExtra = 0;
	winclass.hbrBackground = static_cast<HBRUSH>(GetStockObject(LTGRAY_BRUSH));
	winclass.hCursor = LoadCursor(NULL, IDC_CROSS);
	winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	winclass.hInstance = hInstance;
	winclass.lpfnWndProc = winproc;
	winclass.lpszClassName = className;
	winclass.lpszMenuName = NULL;
	winclass.style = CS_VREDRAW | CS_HREDRAW;

	RegisterClass(&winclass);

	HWND hWnd = CreateWindow(className, className,
		WS_OVERLAPPEDWINDOW,
		100, 100, 500, 500,
		NULL, NULL, hInstance, NULL);

	ShowWindow(hWnd, cmdShow);

	MSG msg;
	while (GetMessage(&msg, hWnd, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK winproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == WM_CREATE)
	{
		HWND hWndButton = CreateWindow(TEXT("BUTTON"), TEXT("btn"),
			WS_CHILD | WS_VISIBLE | BS_PUSHBOX,
			10, 10, 100, 25,
			hWnd, NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
	}
	if (message == WM_COMMAND)
	{
		MessageBox(hWnd, _T(""), _T(""), MB_OK);
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}





这是我窗口的屏幕截图



[IMG] http://i61.tinypic.com/2gxelxi.png [/ IMG]



http://i61.tinypic.com/2gxelxi.png [ ^ ]



当我点击它时它甚至没有发送WM_COMMAND ..我的代码有什么问题?



提前感谢!



and here's a screen shot of my window

[IMG]http://i61.tinypic.com/2gxelxi.png[/IMG]

http://i61.tinypic.com/2gxelxi.png[^]

it doesn't even send WM_COMMAND when I click on it.. what's the wrong with my code ?

thanks in advance !

推荐答案

您需要将命令ID添加到按钮(按钮看比盒子更漂亮)像:

You need to add the command id to your button (buttons look nicer than boxes) like:
HWND hWndButton = CreateWindow(TEXT("BUTTON"), TEXT("Push Me"),
    WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    10, 10, 100, 25,
    hWnd, (HMENU)ID_MYCOMMAND, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);



其中 ID_MYCOMMAND 是此控件的命令标识符,您可以在命令处理程序采取适当的代码路径。


where ID_MYCOMMAND is the command identifier for this control, which you can use in your command handler to take the appropriate code path.


只是猜测:为了能够发送WM_COMMAND消息,按钮需要一个id。对于基于模板的对话框,此ID通常来自模板。在您的情况下,您必须手动设置,例如:

Just a guess: To be able to send a WM_COMMAND message the button needs an id. For template-based dialogs this id normally comes from the template. In your case you must set manually, for example:
SetWindowLong (m_hWnd, GWL_ID, 100); // sets id 100





[已添加]另一个可能的错误来源是你已经全部大写指定了Window类BUTTON。你应该尝试按钮。



[ADDED] Another possible error source is that you have specified the Window class "BUTTON" in all uppercase. You should try "Button" instead.


这篇关于Win32按钮问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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