Simple Windows API question

Status
Not open for further replies.

blazko

Ars Scholae Palatinae
840
Here is my message handling function:

code:<HR>
Code:
BOOL CALLBACK ConfigProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	CAlarmTime alarm(0,0,0);
	int h=0,min=0,ampm=0;<P>	switch(uMsg)
	{<P>	case WM_INITDIALOG:
		CheckRadioButton(hWnd,IDC_RADIO1,IDC_RADIO2,IDC_RADIO1);
		return TRUE;
	
	case WM_COMMAND:
		
		if(HIWORD(wParam)==BN_CLICKED)
		{
			switch(LOWORD(wParam))
			{<P>			case IDC_RADIO1:
				ampm=0;
				CheckRadioButton(hWnd,IDC_RADIO1,IDC_RADIO2,IDC_RADIO1);
				return TRUE;<P>			case IDC_RADIO2:
				ampm=1;
				CheckRadioButton(hWnd,IDC_RADIO1,IDC_RADIO2,IDC_RADIO2);
				return TRUE;
			}
		}<P>		if(wParam==IDOK)
		{
			alarm.SetAlarmTime(h,min,ampm);
			alarm.PrintAlarmTime();
			return TRUE;
		}<P>		if(wParam==IDCANCEL)
		{
			EndDialog(hWnd,0);
			return TRUE;
		}<P>		return TRUE;<P>	case WM_CLOSE:
		EndDialog(hWnd,0);
		return TRUE;<P>	}<P>	return FALSE;
}

I just have this set up to test my radio buttons when I hit the "OK" button, but it seems like when I hit "OK" it isn't processing my SetAlarmTime function again. The ampm variable does change to the proper value after I hit the radio button, but when I hit "OK" the value is always what I initialized the ampm variable as (in this case, 0).

Am I going to have to create separate methods to set the members of the class individually? (one for h, min, and ampm)? I just want to set them all at once at the end...

Thanks for any help
-blazko
 
code:<HR>
Code:
BOOL CALLBACK ConfigProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	CAlarmTime alarm(0,0,0);
	int h=0,min=0,ampm=0; 
//...

Remember, this is your message handling callback. It is going to get called by windows (<-sort of) everytime anything happens. Everything you do is going make this function get called again. And after you process each message, you will exit this function.

The local variables don't stick around between messages. You need global variables or something similar for the program to remember anything.

The initialization code you have at the top of the function runs every time any sort of event happens. So when you click the OK button, the function gets called and ampm gets set to 0, regardless of what you may have set it to when you got a previous message.

Really, you don't need the ampm variable at all. In the wParam==IDOK case statement, just ask the am and pm radio buttons if they are checked or not.

Am I going to have to create separate methods to set the members of the class individually? (one for h, min, and ampm)?

You don't have any class!

no, don't take that personally... I just meant you aren't dealing with any objects here, this is just a good ole' function, no objects no classes, no members. That is why nothing is lasting between messages.
 

blazko

Ars Scholae Palatinae
840
Ah, thanks. That is a much better way.

Am I going to have to create separate methods to set the members of the class individually? (one for h, min, and ampm)?

I meant would I need to set the members of my CAlarm class seperatly (i.e... alarm.SetHour(h)), which after your explanation I see to be obviously wrong. CAlarm really is a class though.

Thanks for the help!
 
Status
Not open for further replies.