按下按钮时如何执行操作
How to do an action when button is pressed
我正在为我的应用程序创建一个登录屏幕,我创建了按钮,但我不知道如何让我的程序做一些事情,当这些按钮之一是 pressed.Here 是我的代码(我不认为它会有用)
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CREATE:
CreateWindow(TEXT("button"), TEXT("CONFIRM"), WS_VISIBLE | WS_CHILD, 355, 400, 95, 35, hwnd, NULL, NULL , NULL);
CreateWindow(TEXT("button"), TEXT("SIGN UP"), WS_VISIBLE | WS_CHILD, 50, 400, 95, 35, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("static"), TEXT("USERNAME:"), WS_VISIBLE | WS_CHILD, 50, 40, 80, 15, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("static"), TEXT("EMAIL:"), WS_VISIBLE | WS_CHILD, 85, 100, 45, 15, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("static"), TEXT("PASSWORD:"), WS_VISIBLE | WS_CHILD, 50, 160, 83, 15, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD, 135, 40, 200, 17, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD, 135, 100, 200, 17, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD, 135, 160, 200, 17, hwnd, NULL, NULL, NULL);
break;
/* Upon destruction, tell the main thread to stop */
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
/* All other messages (a lot of them) are processed using default procedures */
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc; /* A properties struct of our window */
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG msg; /* A temporary location for all messages */
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL,"XBM LOGO.png"); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, "A"); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Verification",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
400, /* x */
75, /* y */
500, /* width */
600, /* height */
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
/*
This is the heart of our program where all input is processed and
sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&msg); /* Translate key codes to chars if present */
DispatchMessage(&msg); /* Send it to WndProc */
}
return msg.wParam;
}
注意:包括 windows.h 和 iostream,但没有任何其他库
谢谢:)
您需要更改的第一件事是为您的按钮 CreateWindow 调用分配一个 ID,这是通过 HMENU 参数完成的。您需要做的下一件事是处理这些 ID 的 WM_COMMAND 和 BN_CLICKED 命令标识符。
例如,将 ID 1 分配给您的确认按钮:
CreateWindow(TEXT("button"), TEXT("CONFIRM"), WS_VISIBLE | WS_CHILD, 355, 400, 95, 35, hwnd, (HMENU)1, NULL, NULL);
然后在点击时处理:
case WM_COMMAND:
{
UINT uID = LOWORD(wParam), uCmd = HIWORD(wParam);
switch(uID)
{
case 1:
if(BN_CLICKED == uCmd)
{
/* Action here */
break;
}
else
return DefWindowProc(hWnd, uMsg, wParam, lParam);
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
我正在为我的应用程序创建一个登录屏幕,我创建了按钮,但我不知道如何让我的程序做一些事情,当这些按钮之一是 pressed.Here 是我的代码(我不认为它会有用)
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CREATE:
CreateWindow(TEXT("button"), TEXT("CONFIRM"), WS_VISIBLE | WS_CHILD, 355, 400, 95, 35, hwnd, NULL, NULL , NULL);
CreateWindow(TEXT("button"), TEXT("SIGN UP"), WS_VISIBLE | WS_CHILD, 50, 400, 95, 35, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("static"), TEXT("USERNAME:"), WS_VISIBLE | WS_CHILD, 50, 40, 80, 15, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("static"), TEXT("EMAIL:"), WS_VISIBLE | WS_CHILD, 85, 100, 45, 15, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("static"), TEXT("PASSWORD:"), WS_VISIBLE | WS_CHILD, 50, 160, 83, 15, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD, 135, 40, 200, 17, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD, 135, 100, 200, 17, hwnd, NULL, NULL, NULL);
CreateWindow(TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD, 135, 160, 200, 17, hwnd, NULL, NULL, NULL);
break;
/* Upon destruction, tell the main thread to stop */
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
/* All other messages (a lot of them) are processed using default procedures */
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc; /* A properties struct of our window */
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG msg; /* A temporary location for all messages */
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL,"XBM LOGO.png"); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, "A"); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Verification",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
400, /* x */
75, /* y */
500, /* width */
600, /* height */
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
/*
This is the heart of our program where all input is processed and
sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&msg); /* Translate key codes to chars if present */
DispatchMessage(&msg); /* Send it to WndProc */
}
return msg.wParam;
} 注意:包括 windows.h 和 iostream,但没有任何其他库 谢谢:)
您需要更改的第一件事是为您的按钮 CreateWindow 调用分配一个 ID,这是通过 HMENU 参数完成的。您需要做的下一件事是处理这些 ID 的 WM_COMMAND 和 BN_CLICKED 命令标识符。
例如,将 ID 1 分配给您的确认按钮:
CreateWindow(TEXT("button"), TEXT("CONFIRM"), WS_VISIBLE | WS_CHILD, 355, 400, 95, 35, hwnd, (HMENU)1, NULL, NULL);
然后在点击时处理:
case WM_COMMAND:
{
UINT uID = LOWORD(wParam), uCmd = HIWORD(wParam);
switch(uID)
{
case 1:
if(BN_CLICKED == uCmd)
{
/* Action here */
break;
}
else
return DefWindowProc(hWnd, uMsg, wParam, lParam);
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}