Thread: help handling checkbox

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    help handling checkbox

    in my switch message i have
    Code:
    case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case IDC_CHECK: MessageBox(NULL,"Ticked","",0); break;
    		}
    	break;
    
    //styles for button
     WS_CHILD  | BS_CHECKBOX | BS_AUTO3STATE | WS_CLIPCHILDREN,
    but when i go to untick the checkbox , it does the messagebox again , so something cant be right

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    How are you creating your check boxes? Are you using CreateWindowEx or are you using a resource script?

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    You've got one argument there; was the control clicked? The answer is always yes. You need to check the state of the checkbox, whether checked or unchecked.

    Code:
    if checked {
     //do this
     }
     else {
    
     //do something else
     }

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    One possible option...


    Code:
    BOOL bIsChecked;
    
    hCheck = CreateWindow ("BUTTON", "Check Box",
        WS_CHILD | BS_AUTO3STATE,
        20, 120, 110, 20,
        hWnd, (HMENU)IDC_CHECK, hInstance, NULL) ;
    
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case IDC_CHECK:
                bIsChecked = SendMessage (hCheck, BM_GETCHECK, 0, 0) ;
                if(bIsChecked)
                    MessageBox (hWnd, "CheckBox Checked", "CheckBox status", MB_OK) ;
                else
                    MessageBox (hWnd, "CheckBox UnChecked", "CheckBox status", MB_OK) ;
    
                break;
        }
    break;

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    the create window is in the winmain code , ive added a second button but that just effects using button one now , heres my switch

    Code:
    #include "stdafx.h"
    #include "ProxyLoaderGui.h"
    #include <windows.h>
    
    
    
    LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
    void RegisterWindowClass(HINSTANCE hInstance);
    void CreateAppWindow(HINSTANCE hInstance);
    void CreateButtonWindow(HINSTANCE hInstance,HWND hWnd);
    void CreateStartWindow(HINSTANCE hInstance,HWND hWnd);
    WPARAM StartMessageLoop();
    
    HWND g_hWnd;
    HWND g_hButton;
    HWND g_hStart;
    int Result;
    
    #define IDC_CHECK 1001
    #define IDC_START 1002
    
    INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,INT)
    {
    	RegisterWindowClass(hInstance);
    	CreateAppWindow(hInstance);
    	CreateButtonWindow(hInstance,g_hWnd);
    	CreateStartWindow(hInstance,g_hWnd);
    
    	ShowWindow(g_hWnd,SW_SHOWDEFAULT);
    	ShowWindow(g_hButton,SW_SHOWDEFAULT);
    	ShowWindow(g_hStart,SW_SHOWDEFAULT);
    
    	UpdateWindow(g_hWnd);
    	UpdateWindow(g_hButton);
    	UpdateWindow(g_hStart);
    	INT result = StartMessageLoop();
    	return result;
    }
    
    LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_CREATE:
    		return 0;
    
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case IDC_CHECK: 
    			Result = SendMessage(g_hButton,BM_GETCHECK,0,0); 
    			if(Result == BST_CHECKED)
    			{
    				MessageBox(NULL,"BST_CHECKED","",0); 
    				break;
    			}
    			if(Result == BST_INDETERMINATE)
    			{
    				MessageBox(NULL,"BST_INDETERMINATE","",0); 
    				break;
    			}
    			if(Result == BST_UNCHECKED)
    			{
    				MessageBox(NULL,"BST_UNCHECKED","",0); 
    				break;
    			}
    		break;
    
    	   // case IDC_START: MessageBox(NULL,"Pressed","",0); break;
    
    			
    		}
    	break;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    
    	case WM_PAINT:
    		ValidateRect(g_hWnd,NULL);
    		ValidateRect(g_hButton,NULL);
    		ValidateRect(g_hStart,NULL);
    
    		RedrawWindow(g_hWnd,NULL,NULL,RDW_INVALIDATE);
    		RedrawWindow(g_hButton,NULL,NULL,RDW_INVALIDATE);
    		RedrawWindow(g_hStart,NULL,NULL,RDW_INVALIDATE);
    		return 0;
    	}
    	return DefWindowProc(hWnd,msg,wParam,lParam);
    }
    
    void RegisterWindowClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX wc;
    	wc.cbSize = sizeof(WNDCLASSEX);
    	wc.style = CS_HREDRAW | CS_HREDRAW | CS_OWNDC;
    	wc.lpfnWndProc = WndProc;
    	wc.cbWndExtra = 0;
    	wc.cbClsExtra = 0;
    	wc.hInstance = hInstance;
    	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    	wc.hCursor = (HCURSOR)LoadCursor(NULL,IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	wc.lpszMenuName = "MainWin";
    	wc.lpszClassName = "WinApp";
    	wc.hIconSm = NULL;
    
    	RegisterClassEx(&wc);
    }
    
    void CreateAppWindow(HINSTANCE hInstance)
    {
    	g_hWnd = CreateWindowEx(WS_EX_TOOLWINDOW,
    		                "WinApp",
    	 "Basic Windows Application",
    	         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
    			 100,
    			 100,
    			 648,
    			 514,
    			 GetDesktopWindow(),
    			 NULL,
    			 hInstance,
    			 NULL);
    }
    
    void CreateButtonWindow(HINSTANCE hInstance,HWND hWnd)
    {
    	g_hButton = CreateWindowEx(NULL,
    		                "BUTTON",
    	               "Button",
    	         WS_CHILD  | BS_CHECKBOX | BS_AUTO3STATE | WS_BORDER,
    			 0,
    			 0,
    			 100,
    			 100,
    			 hWnd,
    			 (HMENU)IDC_CHECK,
    			 hInstance,
    			 NULL);
    }
    
    void CreateStartWindow(HINSTANCE hInstance,HWND hWnd)
    {
    	g_hStart = CreateWindowEx(NULL,
    		                "BUTTON",
    	               "Start",
    	         WS_CHILD | WS_BORDER,
    			 200,
    			 0,
    			 100,
    			 100,
    			 hWnd,
    			 (HMENU)IDC_START,
    			 hInstance,
    			 NULL);
    }
    
    WPARAM StartMessageLoop()
    {
    	MSG msg;
    	while(1)
    	{
    		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    		{
    			if(msg.message == WM_QUIT)
    				break;
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    		else
    		{
    
    		}
    	}
    	return msg.wParam;
    }
    now the first is not working and not saying clicked etc after i add IDC_START
    Last edited by Anddos; 04-03-2009 at 11:01 PM.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hmmm, the message box is showed, but behind the program window
    Try using the main window handle to attach the alert window instead a null handle.

    Niara

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Your problem lies in the WM_PAINT routine. Comment it out as follows and the app will identify the different states of the checkbox.


    Code:
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    #ifdef BAD
    	case WM_PAINT:
    		ValidateRect(g_hWnd,NULL);
    		ValidateRect(g_hButton,NULL);
    		ValidateRect(g_hStart,NULL);
    
    		RedrawWindow(g_hWnd,NULL,NULL,RDW_INVALIDATE);
    		RedrawWindow(g_hButton,NULL,NULL,RDW_INVALIDATE);
    		RedrawWindow(g_hStart,NULL,NULL,RDW_INVALIDATE);
    		return 0;
    #endif
    	}
    	return DefWindowProc(hWnd,msg,wParam,lParam);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Handling critical errors
    By Memloop in forum C Programming
    Replies: 14
    Last Post: 03-26-2009, 06:14 AM
  3. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  4. event handling is serialized in MS Visual Studio C++ 2005 ??
    By mynickmynick in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2008, 04:47 AM
  5. Signal Handling - Are they part of ANSI C?
    By Stanley S in forum C Programming
    Replies: 3
    Last Post: 12-21-2005, 07:49 AM