Thread: CheckBox trouble

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    250

    CheckBox trouble

    im trying to get this checkbox to work i am checking to see if the box is checked and if it is run a program but it doesnt seem to work
    Code:
    #include <windows.h>
    #include <ctime>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    
    #define BOX0 500
    #define BUTTON1 501
    #define GBOX3 502
    #define EDIT5 503
    #define BUTTON7 505
    #define EDIT10 506
    #define EDIT11 507
    
    const char g_szClassName[] = "WindowClass";
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
    	case WM_CREATE:
    		CreateWindowEx(0,
    			TEXT("BUTTON"),
    			TEXT("Open Database"),
    			WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,
    			15,
    			25,
    			200,
    			50,
    			hwnd,
    			(HMENU)BOX0,
    			NULL,
    			NULL);
    		CreateWindowEx(0,
    			TEXT("BUTTON"),
    			TEXT("Start"),
    			WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
    			255,
    			25,
    			100,
    			40,
    			hwnd,
    			(HMENU)BUTTON1,
    			NULL,
    			NULL);
    		CreateWindowEx(0,
    			TEXT("BUTTON"),
    			TEXT("CordGen 2.2"),
    			WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
    			5,
    			0,
    			480,
    			100,
    			hwnd,
    			(HMENU)GBOX3,
    			NULL,
    			NULL);
    		CreateWindowEx(0,
    			TEXT("BUTTON"),
    			TEXT("Quit"),
    			WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
    			375,
    			25,
    			100,
    			40,
    			hwnd,
    			(HMENU)BUTTON7,
    			NULL,
    			NULL);
    		return 0;
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case BUTTON1:
    			{
    				srand ((unsigned) time(0));
    				
    
    				for(int gen = 0; gen < 100; gen++)
    				{
    			       	int cordx = rand()%1600;
    			       	int cordy = rand()%1600;
    			     	int cordz = rand()%3000;
    
    					int cordxr = rand()%2;
    					int cordyr = rand()%2;
    					int cordzr = rand()%2;
    
    					if(cordxr == 1)
    					{
    						cordx = cordx - 1600;
    					}
    					if(cordyr == 1)
    					{
    						cordy = cordy - 1600;
    					}
    					if(cordzr == 1)
    					{
    						cordz = cordz - 3000;
    					}
    
    					SetDlgItemInt(hwnd,EDIT5,cordx,0);  // send cords to edit boxes
    					SetDlgItemInt(hwnd,EDIT10,cordy,0);
    					SetDlgItemInt(hwnd,EDIT11,cordz,0);
    
    					ofstream open_cdata("Cords.txt", ios::app);   // send cords to text file
    					open_cdata <<cordx<<" "<<cordy<<" "<<cordz<<endl;
    					open_cdata.close();
    					if(BOX0 == BST_CHECKED)
    					{
    						system("Cords.txt");
    					}
    				}
    			}
    			
    			break;
    		case BUTTON7:
    			SendMessage(hwnd,WM_CLOSE,0,0);
    			return 0;
    			break;
    		}
    		return 0;
    		break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL,IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+0);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "CordGen",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 500,150,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I changed your code so that when the start button is pressed, it tells you the state of the check box. You should be able to use that to do what you want.

    Code:
    #include <windows.h>
    #include <ctime>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    
    #define BOX0 500
    #define BUTTON1 501
    #define GBOX3 502
    #define EDIT5 503
    #define BUTTON7 505
    #define EDIT10 506
    #define EDIT11 507
    
    HWND g_hwndCheck = NULL;
    
    const char g_szClassName[] = "WindowClass";
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
    	case WM_CREATE:
    		g_hwndCheck = CreateWindowEx(0,
    			TEXT("BUTTON"),
    			TEXT("Open Database"),
    			WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,
    			15,
    			25,
    			200,
    			50,
    			hwnd,
    			(HMENU)BOX0,
    			NULL,
    			NULL);
    		CreateWindowEx(0,
    			TEXT("BUTTON"),
    			TEXT("Start"),
    			WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
    			255,
    			25,
    			100,
    			40,
    			hwnd,
    			(HMENU)BUTTON1,
    			NULL,
    			NULL);
    		CreateWindowEx(0,
    			TEXT("BUTTON"),
    			TEXT("CordGen 2.2"),
    			WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
    			5,
    			0,
    			480,
    			100,
    			hwnd,
    			(HMENU)GBOX3,
    			NULL,
    			NULL);
    		CreateWindowEx(0,
    			TEXT("BUTTON"),
    			TEXT("Quit"),
    			WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
    			375,
    			25,
    			100,
    			40,
    			hwnd,
    			(HMENU)BUTTON7,
    			NULL,
    			NULL);
    		return 0;
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case BUTTON1:
    			{
    				if(g_hwndCheck)
    				{
    					if(SendMessage(g_hwndCheck,BM_GETCHECK,0,0) == BST_CHECKED)
    						MessageBox(NULL,"Box is checked"," ",MB_OK);
    					else
    						MessageBox(NULL,"Box is NOT checked"," ",MB_OK);
    
    				}
    
    			}
    			
    			break;
    		case BUTTON7:
    			SendMessage(hwnd,WM_CLOSE,0,0);
    			return 0;
    			break;
    		}
    		return 0;
    		break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL,IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+0);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "CordGen",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 500,150,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>if(g_hwndCheck)

    Not a big fan of global variables, particularly when the data is easy to get. Better to use the HWND in the lParam or GetDlgItem().

    I would use both, some thing like

    Code:
    case BUTTON1:
    if (BN_CLICKED == HIWORD(wParam))// a clicked button must have been created already
    {
             if(SendMessage( GetDlgItem(hWnd,BUTTON1) , BM_GETCHECK, 0, 0) == BST_CHECKED)
    If I may critique your code 'style' a bit cgod....
    These are suggestions only...

    Globals, no comments and non descriptive variable names are not a problem in an app this size but will quickly become one. Think about looking at this in a year or when it a is huge app.
    Also try breaking the code into smaller functions. Callbacks quickly get out of hand and unreadable/debuggable.
    Try calling variables descriptive names
    ie
    BUTTON1 does? Generates random coods so call it something like
    IDC_GEN_COODS (IDC_ = IDentifcation number of a Control)
    Last edited by novacain; 11-29-2004 at 03:04 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    >> I would use both, some thing like
    Your code wouldnt work as it is since it's sending the message to the wrong window. It would need to be:
    Code:
    if(SendMessage( GetDlgItem(hWnd,BOX0) , BM_GETCHECK, 0, 0) == BST_CHECKED)

  5. #5
    Banned
    Join Date
    Oct 2004
    Posts
    250
    thanks for the help, i got the checkbox working but when i use OpenFile(); the program crashes any ideas on how i could fix this?
    Code:
     if(SendMessage( GetDlgItem(hwnd,BOX0) , BM_GETCHECK, 0, 0) == BST_CHECKED);
    				{
    					OpenFile("Cords.txt",NULL,NULL);
    				}

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The OpenFile function is obsolete. Instead use C I/O (fopen), C++ I/O (iostream) or WinAPI I/O (CreateFile). You can perform a search of the board for samples of all of these methods.

    On a style matter, this:
    Code:
    SendMessage( GetDlgItem(hwnd,BOX0) , BM_GETCHECK, 0, 0)
    can be replaced with this:
    Code:
    SendDlgItemMessage(hwnd, BOX0, BM_GETCHECK, 0, 0)

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>if(SendMessage( GetDlgItem(hWnd,BUTTON1)

    opps... see why descriptive names are helpful.

    >>SendDlgItemMessage(hwnd, BOX0, BM_GETCHECK, 0, 0)

    or

    if(SendMessage((HWND)lParam, .....);



    Any others?

    where is that cat I was skinning.......
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. resource.rc CheckBox and Editfield
    By Plasm4 in forum Windows Programming
    Replies: 3
    Last Post: 08-09-2008, 08:24 PM
  2. setting checks on checkbox , setcheck not working
    By hanhao in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 09:38 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. displaying a checkbox to the List view control
    By saif in forum Windows Programming
    Replies: 1
    Last Post: 02-07-2003, 12:21 AM
  5. Missing checkbox...
    By jdinger in forum Windows Programming
    Replies: 4
    Last Post: 03-18-2002, 10:47 PM