【问题标题】:Windows 32 Api: 2 Checkboxes acting as one?Windows 32 Api:2个复选框作为一个?
【发布时间】:2017-06-01 04:59:38
【问题描述】:

好的,自从我在这里提出问题以来已经有一段时间了,从那时起我学到了很多东西......但是我的最新项目需要大量的复选框,并且由于某种原因我不能只得到其中的 2 个行为正确(可以检查,但不能取消检查并且彼此独立{像单独的复选框一样})!

所以...快速运行... Ini 解析器读取 ini 文件,单独的 .cpp 文件中的逻辑填充变量以选中/取消选中框。主显示框,用户可以随意启用或禁用它们。然后 Ini writer(也在单独的 .cpp 文件中)写出更改。故事的结尾……我是这么想的!

这是一个 sn-p,它概述了我迄今为止为 main 编写的内容...

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static char *title = TEXT("Check Box");

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
    {
MSG  msg;
WNDCLASS wc = { 0 };
wc.lpszClassName = TEXT("Check Box");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);


RegisterClass(&wc);
CreateWindow(wc.lpszClassName, title,
    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    150, 150, 230, 150, 0, 0, hInstance, 0);

while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

switch (msg)
{
case WM_CREATE:
{
    HINSTANCE hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
    CreateWindow(TEXT("button"), TEXT("+ developer 1"),
        WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
        20, 20, 185, 35,
        hwnd, (HMENU)IDB_DEV, hInstance, NULL);
    CheckDlgButton(hwnd, IDB_DEV, BST_UNCHECKED);
    CreateWindow(TEXT("button"), TEXT("+ logfile 1"),
        WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
        20, 50, 185, 35,
        hwnd, (HMENU)IDB_LOG, hInstance, NULL);
    CheckDlgButton(hwnd, IDB_LOG, BST_UNCHECKED);
    break;
    }

    case WM_COMMAND:
    {
    BOOL dev = IsDlgButtonChecked(hwnd, IDB_DEV);
    BOOL log = IsDlgButtonChecked(hwnd, IDB_LOG);

    if (dev == TRUE) {
        CheckDlgButton(hwnd, IDB_DEV, BST_CHECKED);
        SetWindowText(hwnd, TEXT("DEV"));
    }

    if (log == TRUE) {
        CheckDlgButton(hwnd, IDB_LOG, BST_CHECKED);
        SetWindowText(hwnd, TEXT("LOG")); //could us magic L here, but for 
    }                                     //readabilitly...

    else {
        CheckDlgButton(hwnd, IDB_DEV, BST_UNCHECKED);
        CheckDlgButton(hwnd, IDB_LOG, BST_UNCHECKED);
        SetWindowText(hwnd, title);
    }

    break;
    }

    case WM_DESTROY:
    {
    PostQuitMessage(0);
    break;
    }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

在我看来,问题出在逻辑区域:case WM_COMMAND,但如果我删除其中一个 if 语句,另一个 if 功能正常,但按钮仍然无法取消选中...如果我离开它们按原样...无论单击哪个按钮,两者都是正确的。我试过“else if”,但没有达到预期的效果。我知道我在这里遗漏了一些东西......

附:我没有通过实践学习的正式培训。有些人可能会称我为黑客,但我更喜欢这种方式!

【问题讨论】:

    标签: c++ windows user-interface


    【解决方案1】:

    if (dev = TRUE) 应该是==

    正式培训会警告您这一点。但即使你不喜欢这样,实验也会告诉你会发生什么。但是在不理解的情况下编写代码,并且不仔细研究发生的情况,这是不可行的。

    【讨论】:

    • 您的鄙视没有必要,但感谢您的回答。
    • 这只是解决了部分问题顺便说一句,复选框仍然无法取消选中,一旦选中另一个就无法选中。
    • @Spilt_Blood:嗯,log = TRUE 也存在同样的错误。由于取消选中是在if(log = TRUE) 的无法到达的else 情况下,这就解释了为什么你不能取消选中它。由于问题仍然存在,我怀疑您只修复了我从字面上指出的一行。
    • 我还怀疑缺少else。如果log==FALSE,那么(不管开发人员)你将取消选中IDB_DEV。 即使你之前只检查了几行。调试器会告诉你这一点。
    • 现在我也无法检查...我在 dev 之后添加了另一个 else...我知道调试器是什么,但我不知道如何设置。 Visual Studio 会为你做,但我觉得它可以提供更有用的输出......有什么想法吗?
    【解决方案2】:

    已修复...

    #include <windows.h>
    #include "resource.h"
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    static char *title = TEXT("Check Box");
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
    {
    MSG  msg;
    WNDCLASS wc = { 0 };
    wc.lpszClassName = TEXT("Check Box");
    wc.hInstance = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(0, IDC_ARROW);
    
    
    RegisterClass(&wc);
    CreateWindow(wc.lpszClassName, title,
    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    150, 150, 230, 150, 0, 0, hInstance, 0);
    
    while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    
    switch (msg)
    {
    case WM_CREATE:
    {
    HINSTANCE hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
    CreateWindow(TEXT("button"), TEXT("+ developer 1"),
        WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
        20, 20, 185, 35,
        hwnd, (HMENU)IDB_DEV, hInstance, NULL);
    CheckDlgButton(hwnd, IDB_DEV, BST_UNCHECKED);
    CreateWindow(TEXT("button"), TEXT("+ logfile 1"),
        WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
        20, 50, 185, 35,
        hwnd, (HMENU)IDB_LOG, hInstance, NULL);
    CheckDlgButton(hwnd, IDB_LOG, BST_UNCHECKED);
    break;
    }
    
    case WM_COMMAND:
    {
    BOOL dev = IsDlgButtonChecked(hwnd, IDB_DEV);
    BOOL log = IsDlgButtonChecked(hwnd, IDB_LOG);
    
    if (dev == TRUE) {
        CheckDlgButton(hwnd, IDB_DEV, BST_CHECKED);
        SetWindowText(hwnd, TEXT("DEV"));
    }
    
    else if (log == TRUE) {
        CheckDlgButton(hwnd, IDB_LOG, BST_CHECKED);
        SetWindowText(hwnd, TEXT("LOG")); //could us magic L here, but for 
    }                                     //readabilitly...
    
    else {
        CheckDlgButton(hwnd, IDB_DEV, BST_UNCHECKED);
        CheckDlgButton(hwnd, IDB_LOG, BST_UNCHECKED);
        SetWindowText(hwnd, title);
    }
    
    break;
    }
    
    case WM_DESTROY:
    {
    PostQuitMessage(0);
    break;
    }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2013-07-20
      • 2015-02-13
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多