问题描述
我想在我的窗口添加一个按钮..
这是我尝试过的代码..
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]
[]
当我点击它时它甚至没有发送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 !
推荐答案
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.
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按钮问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!