Windows 32 Api:2个复选框作为一个?

时间:2017-06-01 04:59:38

标签: c++ windows user-interface

好吧,自从我在这里问了一个问题已经有一段时间了,从那以后我学到了很多东西......但是我最新的项目需要大量的复选框,出于某种原因,我不能只拿到其中的两个行为正常(可以检查,但不能取消检查,彼此独立{充当单独的复选框})!

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

这是一个片段,概述了我迄今为止为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);
}

在我看来,问题将出现在逻辑区域:案例WM_COMMAND,但如果我删除任何一个if语句,另一个如果功能正常,但按钮仍然无法取消选中...如果我离开它们原样......无论点击哪个按钮,两者都是真的。我试过&#34;否则如果&#34;但那没有达到预期的效果。我知道我在这里错过了一些东西......

P.S。我没有接受过正规训练。有些人可能会称我为黑客,但我更喜欢这样!

2 个答案:

答案 0 :(得分:1)

if (dev = TRUE)应为==

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

答案 1 :(得分:0)

固定...

#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);
}