Reputation: 95
Okay, its been quite awhile since i asked a question here, and i have learned a lot since then... however my newest project will require a multitude of check boxes, and for some reason i cannot get just 2 of them to behave properly(can check, but not un-check and be independent of each other{act like separate check boxes})!
So... a quick run down... Ini parser reads ini file, logic within a separate .cpp file fills variables to check/uncheck boxes. Main displays boxes, and user can enable or disable them at will. then Ini writer(also in a separate .cpp file) writes out the changes. end of story... or so i thought!
here is a snippet that outlines what i have written for main so far...
#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);
}
it seems to me that the issue would be in the logic area: case WM_COMMAND, but if i remove either of the if statements, the other if functions properly, but the buttons still cannot be unchecked... if i leave them as is... both are true no matter which button is clicked. I have tried "else if" but that did not have the desired effect. I know i'm missing something here...
P.S. I have no formal training i learn by doing. Some may call me a hack, but i prefer it this way!
Upvotes: 0
Views: 81
Reputation: 95
Fixed...
#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);
}
Upvotes: 0
Reputation: 179819
if (dev = TRUE)
should be ==
Formal training would have warned you about this. But even if you dislike that, experimenting would have shown you what happens. But writing code without understanding, and without closely studying what happens, that's just not viable.
Upvotes: 1