WinAPI create window + child windows,处理按钮按下?
WinAPI create window + child windows, process a button press?
我是 WinApi 的新手,我希望在我的程序中创建一个简单的 window,其中包含一个空白 parent window 和两个较小的子按钮 "button1" & "button2"。我希望通过这个按钮将 bool 值从 false 更改为 true,反之亦然,但是我看到的几乎所有示例都很难理解,看来您必须 return 和 MSG 某种我不知道如何处理的值。
我在下面有一个 伪代码 我正在尝试做的事情,我留下了评论来解释我每时每刻想做的事情,我做对了吗方式?:
#include <windows.h>
static int buildwindow(){
MSG msg;
//create parent window
HWND hWnd = CreateWindow(TEXT("scrollbar"), TEXT("Parent"), WS_VISIBLE | WS_POPUP,
10, 10, 800, 500, NULL, NULL, NULL, NULL);
//create child window
HWND hWnd1 = CreateWindow(TEXT("button"), TEXT("button1"), WS_CHILD|WS_VISIBLE | WS_POPUP,
10, 10, 80, 25, hWnd, NULL, NULL, NULL);
//create child window2
HWND hWnd2 = CreateWindow(TEXT("button"), TEXT("button2"), WS_CHILD|WS_VISIBLE | WS_POPUP,
100, 100, 80, 25, hWnd, NULL, NULL, NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
ShowWindow(hWnd1, SW_SHOW);
UpdateWindow(hWnd1);
ShowWindow(hWnd2, SW_SHOW);
UpdateWindow(hWnd2);
//wait for buttonpress
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//return the buttonpress
return (int) msg.wParam;
}
int main(void)
{
//create window inside the buttonpress method
int buttonpress = buildwindow();
//check which button was pressed
if(buttonpress = button1){
//do something
}
elseif(buttonpress = button2){
//do something else
}
//finish
return(0);
}
消息循环 (GetMessage) 在 WM_QUIT 消息到达之前不会结束。
您需要实现按钮点击事件的回调函数。
我建议在此处阅读有关按钮消息的更多信息:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb775941(v=vs.85).aspx
我是 WinApi 的新手,我希望在我的程序中创建一个简单的 window,其中包含一个空白 parent window 和两个较小的子按钮 "button1" & "button2"。我希望通过这个按钮将 bool 值从 false 更改为 true,反之亦然,但是我看到的几乎所有示例都很难理解,看来您必须 return 和 MSG 某种我不知道如何处理的值。
我在下面有一个 伪代码 我正在尝试做的事情,我留下了评论来解释我每时每刻想做的事情,我做对了吗方式?:
#include <windows.h>
static int buildwindow(){
MSG msg;
//create parent window
HWND hWnd = CreateWindow(TEXT("scrollbar"), TEXT("Parent"), WS_VISIBLE | WS_POPUP,
10, 10, 800, 500, NULL, NULL, NULL, NULL);
//create child window
HWND hWnd1 = CreateWindow(TEXT("button"), TEXT("button1"), WS_CHILD|WS_VISIBLE | WS_POPUP,
10, 10, 80, 25, hWnd, NULL, NULL, NULL);
//create child window2
HWND hWnd2 = CreateWindow(TEXT("button"), TEXT("button2"), WS_CHILD|WS_VISIBLE | WS_POPUP,
100, 100, 80, 25, hWnd, NULL, NULL, NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
ShowWindow(hWnd1, SW_SHOW);
UpdateWindow(hWnd1);
ShowWindow(hWnd2, SW_SHOW);
UpdateWindow(hWnd2);
//wait for buttonpress
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//return the buttonpress
return (int) msg.wParam;
}
int main(void)
{
//create window inside the buttonpress method
int buttonpress = buildwindow();
//check which button was pressed
if(buttonpress = button1){
//do something
}
elseif(buttonpress = button2){
//do something else
}
//finish
return(0);
}
消息循环 (GetMessage) 在 WM_QUIT 消息到达之前不会结束。
您需要实现按钮点击事件的回调函数。
我建议在此处阅读有关按钮消息的更多信息:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb775941(v=vs.85).aspx