如何在windows API程序中使用按钮



我正在尝试制作一个按钮,并在单击它时发生一些事情。我使用了while循环来制作按钮,但我正在尝试在单击它的时候发生一些事情,我使用的是c++控制台应用程序。

#define VK_LBUTTON 0x01
#define VK_RBUTTON 0x02
#include <Windows.h>
#include "tchar.h"
#include <iostream>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
MSG msg;
//if you add WS_CHILD flag,CreateWindow will fail because there is no parent window.
HWND hWnd = CreateWindow(TEXT("button"), TEXT("Easy"), WS_VISIBLE | WS_POPUP,
500, 500, 80, 25, NULL, NULL, NULL, NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}

BUTTON控件需要一个父窗口,但您没有创建父窗口。该父窗口需要一个窗口过程来处理发送给它的消息。当单击按钮时,它会向其父窗口发送BN_CLICKED通知。你的消息循环将永远看不到它。

最新更新