Thread: create radio buttons

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    50

    create radio buttons

    I'm trying to create a simple windows text game for a friend of mine...

    I'm able to handle text boxes, normal click buttons, etc., but I can't seem to figure out how to create radio buttons.

    What I want is that there is a question the game asks, and gives you a list of say 2, 3 options for the right answer. Next to each possible answer is a radio button. If they click the correct radio button, I want them to be able to continue; if not, I want it to tell them that they're incorrect. I think I can handle figuring out which button they choose, what I can't figure out is how to create the dumb button!

    I've looked on MSDN but it's so damn hard to navigate that I didn't have any luck...

    My guess is it's something in the makeintresource() function right, and you have to make the correct button ID in the resource file. But i don't know what the button ID is so that windows knows to make a radio button.

    Thanks for your help in advance!

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    To make a radio button, use the CreateWindowEx() function:

    Code:
    HWND CreateWindowEx(
        0,
        TEXT("BUTTON"),
        TEXT("THIS IS A RADIO BUTTON!"),
        WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
        int x,
        int y,
        int nWidth,
        int nHeight
        HWND hWndParent,
        HMENU hMenu,
        HINSTANCE hInstance,
        LPVOID lpParam
       );
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    50

    but...

    Doesn't that override the windows defaults for creating the x box and stuff?

    To my knowledge (which granted is exceedingly limited, I'm quite the newbie), here is your typical CreateWindowEx procedure:

    hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szClassName,
    "The title of my window",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
    NULL, NULL, hInstance, NULL);

    Which of course you can customize to change things around...

    Yours has too many parameters and results in the elimination of the window resizing and closing functions.

    Or am I misunderstanding the placement? My above example goes in WinMain(). Does yours go somewhere else, or am I misunderstanding how to use the code?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Both of ours have the same ammount of parameters, you just space yours out differently, AND the one i am telling you, goes in WM_CREATE: in your WndProc() function

    Code:
    YOURS:
    
    hwnd = CreateWindowEx( 
    WS_EX_CLIENTEDGE,  // 1
    g_szClassName,  // 2
    "The title of my window", // 3 
    WS_OVERLAPPEDWINDOW,  // 4
    CW_USEDEFAULT,  // 5
    CW_USEDEFAULT,  // 6
    300,  // 7
    200, // 8
    NULL, // 9
    NULL, //10
    hInstance, // 11 
    NULL); // 12
    Code:
    MINE:
    
     HWND CreateWindowEx(
        0, // 1
        TEXT("BUTTON"), // 2
        TEXT("THIS IS A RADIO BUTTON!"), // 3
        WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON, // 4
        int x, // 5
        int y, // 6
        int nWidth, // 7
        int nHeight, // 8
        HWND hWndParent, // 9
        HMENU hMenu, // 10
        HINSTANCE hInstance,  // 11
        LPVOID lpParam // 12
       );
    See, the same ammount of parameters
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    50
    By any chance are you using MFC?

    I note that your second parameter, lpClassName, you use BUTTON. On MSDN, it says that that is a predefined class (oddly enough, it also explains all of this under Windows CE).

    I'm trying to do this without MFC...

    If I try to use it under WinProc() the way you're saying, I get tons of errors telling me about undeclared identifiers (yes, I'm including windows.h). I'm using MSVC 6.0.

    There is something else I don't understand...

    MSDN says that CreateWindowEx sends the message WM_CREATE to the window, which should be handled by WinProc. But when I put what you gave me under WinProc in the switch statement, it doesn't show up. All I get is my window with a title.

    Also, I think CreateWindowEx() needs to be in WinMain.. if I try a version without it there, all the computer does is turn the cursor into an hourglass for a second and then back to a mouse, but it never creates the window.

    Gr. I apologize for my lack of understanding. I've tried reading some tutorials but they just don't go enough in depth.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You can 'create' a class of your own with a WINCLASS struc and RegisterClass().

    Or you can use one of the predefined classes, of which BUTTON, EDIT are types.
    This is for both WIN32 API and its MS C++ wrapper MFC.

    Do it under WM_CREATE as then you know its parent is up and running.

    To create a contol you need the style WS_CHILD, specify the dialogs HWND as the HWNDPARENT (the one in the callback/winproc)
    and give it an ID#

    #define IDC_RADIO_BUTTON_1 10001

    cast this as the HMENU paramater.

    (HMENU)IDC_RADIO_BUTTON_1


    If your function does not work, find out WHY.

    Use
    iError=GetLastError();
    as soon as you detect a fail from the return value (say HWND==NULL) and put in a MesageBox() or break point.
    Last edited by novacain; 05-13-2002 at 09:49 PM.
    "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

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    50
    Ok cool. I have the radio button up, along with it's text. I can even handle the message of whether the button was checked. Unfortunately, since my knowledge is so limited, I can't figure out how to simply fill in the radio button so it's filled with a black dot.... all I have it doing right now is that if it's clicked, it pops up a message box to let me know it was clicked.

    How do I go about simply filling it in and then checking to see if it was clicked only AFTER someone clicks a subsequent button, say one called "Submit"?


  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Wow! Quick.

    CheckDlgButton(hWnd, IDC_RADIOBUTTON_1, BST_UNCHECKED);//turn off
    BST_CHECKED for on
    Last edited by novacain; 05-13-2002 at 09:53 PM.
    "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

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    50
    Got everything.

    Thanks alot!

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    50
    I have only one last question...

    How do I make sure that the check box I'm checking is the only one checked?

    You know how sometimes you'll pull up a dialog and it will have multiple radio buttons and you can only select one, because if you select another one it unselects the one you just chose?

    How do you do that?

    Thanks

  11. #11
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Need to use the

    BS_AUTORADIOBUTTON

    so only one in the group can be ON.
    "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. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  2. OwnerDrawn Radio Buttons?
    By Devil Panther in forum Windows Programming
    Replies: 0
    Last Post: 02-11-2006, 11:02 AM
  3. Cannot create MDI Client Win
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 10-31-2005, 10:05 PM
  4. Create two buttons that interact with an editbox
    By Arkanos in forum Windows Programming
    Replies: 8
    Last Post: 07-13-2005, 12:56 AM
  5. radio buttons in group box
    By COBOL2C++ in forum Windows Programming
    Replies: 3
    Last Post: 09-15-2003, 05:26 PM