【发布时间】:2019-11-25 14:48:14
【问题描述】:
我正在制作一个程序,用户必须同意才能继续使用该程序。
用户必须选中复选框才能继续,如果用户选中复选框,则显示“运行”按钮,如果他取消选中复选框,则按钮隐藏。
我的程序有两个问题,当用户取消选中复选框时,“运行”按钮没有消失,第二个问题是当用户点击“运行”按钮时,我的程序认为用户单击复选框并选中或取消选中该复选框。
这是我的整个程序,如果你能帮助我,我会很高兴。 如果你愿意,你可以调试这个程序并查看我的问题。
#include <windows.h>
#include <iostream>
#include "resource.h"
using namespace std;
LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam);
const char *title = "Check Box";
HWND agree, button;
int WINAPI WinMain(HINSTANCE currentInstance, HINSTANCE previousInstance, PSTR cmdLine, INT cmdCount)
{
// Register the window class
const char* CLASS_NAME = "myWin32WindowClass";
WNDCLASS wc{};
wc.hInstance = currentInstance;
wc.lpszClassName = CLASS_NAME;
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(20, 20, 20));
wc.lpfnWndProc = WindowProcessMessages;
RegisterClass(&wc);
HWND main = CreateWindow(CLASS_NAME, "WastedBit 1.6.2",
WS_OVERLAPPED | WS_VISIBLE | WS_BORDER | WS_MINIMIZEBOX | WS_SYSMENU, // Window style
CW_USEDEFAULT, CW_USEDEFAULT, // Window initial position
950, 750, // Window size
nullptr, nullptr, nullptr, nullptr);
// TopMost
SetWindowPos(main, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
// Window loop
MSG msg{};
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_CREATE: {
button = CreateWindow("button", 0,
WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
20, 490, 15, 15,
hwnd, (HMENU)1, ((LPCREATESTRUCT)lparam)->hInstance, NULL);
CheckDlgButton(hwnd, 1, BST_UNCHECKED);
}
break;
case WM_COMMAND: {
BOOL checked = IsDlgButtonChecked(hwnd, 1);
if (checked) {
CheckDlgButton(hwnd, 1, BST_UNCHECKED);
}
else if (CheckDlgButton(hwnd, 1, BST_CHECKED) == TRUE) {
CheckDlgButton(hwnd, 1, BST_CHECKED);
agree = CreateWindow("button", "RUN", WS_VISIBLE | WS_CHILD, 750, 525, 150, 150, hwnd, (HMENU)button, 0, 0);
}
else if (CheckDlgButton(hwnd, 1, BST_UNCHECKED) == TRUE) {
ShowWindow(agree, SW_HIDE);
}
}
break;
case WM_DESTROY: {
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
}
【问题讨论】: