Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

C++ Programming for Games Module II phần 7 pps
Nội dung xem thử
Mô tả chi tiết
193
// Yes, one of the radio buttons in the group was selected,
// so select the new one (stored in LOWORD(wParam)) and
// deselect the other to update the radio button GUI.
// Note: Assumes the radio buttons were created sequentially.
CheckRadioButton(hDlg,
IDC_RADIO_FIGHTER, // First radio button in group
IDC_RADIO_WIZARD, // Last radio button in group
LOWORD(wParam)); // Button to select.
// Save currently selected radio button.
classSelection = LOWORD(wParam);
return true;
Observe that we do not test for each individual radio button selection; rather we test for any selection.
LOWORD(wParam) will give us the actual button pressed, which we pass on to the CheckRadioButton
function, which will handle selecting the correct button and deselecting the others.
Finally, the last key idea of this program is the message box. More specifically, when the user presses
the OK button, we want the program to display a message specific to the character (radio button)
selected. This is easy enough since we have saved the current radio button selected in the
classSelection variable. Thus we could do a simple ‘if’ statement to output an appropriate message
based on the selection. However, instead we add the following variable to the dialog procedure that
contains our messages:
string classNames[4] =
{
"You selected the Fighter.",
"You selected the Cleric.",
"You selected the Thief.",
"You selected the Wizard."
};
We then output the message like so:
case IDOK:
// Now display the class the user selected in a message box.
MessageBox(
0,
classNames[classSelection-IDC_RADIO_FIGHTER].c_str(),
"Message",
MB_OK);
return true;
The interesting line is:
classNames[classSelection-IDC_RADIO_FIGHTER].c_str()
The best way to explain this is to look at the actual numeric values of the resource IDs. If you open
resource.h, you may see something similar to the following:
194
#define IDC_RADIO_FIGHTER 1001
#define IDC_RADIO_CLERIC 1002
#define IDC_RADIO_THIEF 1003
#define IDC_RADIO_WIZARD 1004
Again, the resource identifiers are just unique ways of identifying the resource items.
Our array classNames is a four-element array, so we cannot index into it with values such as 1001,
1002, 1003, or 1004. What we do is subtract the first ID in the group. The first resource in the group is
IDC_RADIO_FIGHTER, which is actually the number 1001. So this would give:
IDC_RADIO_FIGHTER - IDC_RADIO_FIGHTER = 1001 – 1001 = 0
IDC_RADIO_CLERIC - IDC_RADIO_FIGHTER = 1002 – 1001 = 1
IDC_RADIO_THIEF - IDC_RADIO_FIGHTER = 1003 – 1001 = 2
IDC_RADIO_WIZARD - IDC_RADIO_FIGHTER = 1004 – 1001 = 3
The result of these subtractions gives us the corresponding text index for classNames! So for
example, if classSelection == IDC_RADIO_THIEF, we would have:
IDC_RADIO_THIEF - IDC_RADIO_FIGHTER = 1003 – 1001 = 2
and
classNames[2] == "You selected the Thief."
This is the exact string we want to use in this example.
Program 16.3 shows the complete code listing for the radio button program.
Program 16.3:
#include <windows.h>
#include <string>
#include "resource.h"
using namespace std;
// Dialog handle.
HWND ghDlg = 0;
// Dialog window procedure.
BOOL CALLBACK
MsgDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Default to fighter.
static int classSelection = IDC_RADIO_FIGHTER;
string classNames[4] =
{
"You selected the Fighter.",
"You selected the Cleric.",
"You selected the Thief.",
"You selected the Wizard."
};