-
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!
-
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
);
-
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?
-
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
-
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.
-
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.
-
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"?
:confused:
-
Wow! Quick.
CheckDlgButton(hWnd, IDC_RADIOBUTTON_1, BST_UNCHECKED);//turn off
BST_CHECKED for on
-
Got everything.
Thanks alot!
-
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
-
Need to use the
BS_AUTORADIOBUTTON
so only one in the group can be ON.