switch statements are an elegant way to handle multiple if statements.
Lets take your windows message function as an example, you check to
see if the message is WM_COMMAND like this...
if ( uMsg == WM_COMMAND )
that's great, but what if you are listening for 20 messages, or even more!
That is a heck of a lot of if, else if, else if, etc. The solution? Use a
switch statment. A switch statement takes the parameter you want to check... "uMsg"
and then you have "case" statements for each message we care about. In this
example, we want to check to see if uMsg is WM_COMMAND, WM_CLOSE and WM_INITDIALOG.
To do it as a switch we do...
switch ( uMsg )
{
case WM_INITDIALOG:
{
//put everything that you want to
//happen when the dialog starts up
return TRUE;
}
case WM_COMMAND :
{
//put everything that you want
//for command here
return TRUE;
}
case WM_CLOSE:
{
//put everything that you want to
//happen when the dialog box is closing
return TRUE;
}
}
you have to return or break after each case statement or the code will fall down to the next one. Here is what I mean...
switch ( id )
{
case 1 :
{
//do case 1 stuff
}
case 2 :
{
//do case 2 stuff
}
case 3 :
{
//do case 3 stuff
}
}
Since there were no returns or breaks in the case statement... if id == 1
comes in, it's gonna do the case 1 stuff and fall down and do the case 2 stuff
and fall down and do the case 3 stuff! That is definitley NOT what we want! It should be like this...
switch ( id )
{
case 1 :
{
//do case 1 stuff
break;
}
case 2 :
{
//do case 2 stuff
break;
}
case 3 :
{
//do case 3 stuff
break;
}
}
//do the rest of the stuff here
see, when you put the "break" in, it breaks out of the switch statement.
If you have nothing you want to do after the switch statement, you can use
a "return" statement instead of "break".
__________________________________________________________
#include <stdio.h>
#include <windows.h>
#include "resource.h"
INT_PTR CALLBACK DialogMessages(HWND hwnd,UINT
uMsg,WPARAM wparam,LPARAM lparam)
{
switch ( uMsg )
{
//here
case WM_INITDIALOG:
{
char string [260]= "Type in your first name";
SetDlgItemText( hwnd, IDC_TEXT1, string );
return TRUE;
}
//and here
case WM_COMMAND:
{
if ( HIWORD(wparam) == BN_CLICKED )
{
if (LOWORD(wparam) == IDOK)
{
unsigned int isChecked;
isChecked = IsDlgButtonChecked( hwnd, IDC_CHECK1 );
if ( isChecked == BST_CHECKED )
{
char name [260];
char nameMr [260];
GetDlgItemText( hwnd, IDC_EDIT1, name, 260 );
sprintf(nameMr,"Mr.%s",name);
MessageBox( NULL,nameMr, "messagebox",
MB_OK|MB_ICONEXCLAMATION );
EndDialog( hwnd, 0 );
return TRUE;
}
else if ( isChecked == BST_UNCHECKED )
{
char name [260];
GetDlgItemText( hwnd, IDC_EDIT1, name, 260 );
MessageBox( NULL,name, "messagebox",
MB_OK|MB_ICONEXCLAMATION );
EndDialog( hwnd, 0 );
return TRUE;
}
}
else if (LOWORD(wparam) == IDCANCEL)
{
EndDialog( hwnd, 0 );
return TRUE;
}
}
}
}
return FALSE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE
hPrevInstance, LPSTR pCmdLine, int iCmdShow)
{
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DLOG),
NULL, DialogMessages);
return 0;
}