Pelles C forum

C language => Beginner questions => Topic started by: dancho on January 19, 2007, 02:34:15 PM

Title: Problem with SetFocus function in DialogAsMain application
Post by: dancho on January 19, 2007, 02:34:15 PM
Hi all,this is just some small lesson I do to teach myself C.
So like the title said I have main dialog and 2 push buttons on it , and
I would like to set a focus on button number 2 , but something is wrong with my code and I dont get focus on button.

Code: [Select]
#include <windows.h>
#include "hfile.h"

//proto
BOOL CALLBACK Dlg_Func ( HWND , UINT , WPARAM , LPARAM );

int WINAPI WinMain ( HINSTANCE hInst , HINSTANCE hPrevInst , LPSTR lpCmdLine , int iShowCmd ) {

return DialogBox ( hInst , MAKEINTRESOURCE ( DIALOG1 ) , NULL , Dlg_Func ) ;

}

BOOL CALLBACK Dlg_Func ( HWND hWnd , UINT msg , WPARAM wParam , LPARAM lParam ) {

switch ( msg ) {

case WM_INITDIALOG:
SetFocus ( GetDlgItem ( hWnd , PUSHB02 ) );
break;

case WM_COMMAND:
break;

case WM_CLOSE:
EndDialog ( hWnd , 0 );
break;

}

return FALSE;

}


So my first question is why I dont have focus and
second is should I use break statment for separating cases or not ?

thx for help...
Title: Problem with SetFocus function in DialogAsMain application
Post by: frankie on January 19, 2007, 04:58:59 PM
First of all try to read this:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/dialogboxes/dialogboxreference/dialogboxmessages/wm_initdialog.asp

Then use the tabstop editor (in the IDE dialog editor) to set the control where you want focus as the first tabstop.

Also modify code to return true for WM_INITDIALOG, so windows keep the modifications done:
Code: [Select]
     case WM_INITDIALOG:
         SetFocus ( GetDlgItem ( hWnd , PUSHB02 ) );
         return TRUE;


About the use of breaks in switch statements it works this way: when the code finds a break it jumps to the end of the switch statement, if no break is present execution continues with the next case.
Consider:
Code: [Select]
void CountBeeps(int many)
{
  switch (many)
  {
    case 4:
     beep();
    case 3:
     beep();
    case 2:
     beep();
    case 1:
     beep();
  }
}

In this case you'll emit so many beeps as the value in the variable many. I.e. if you pass many=3 then the switch will enter case 3 and will emit a beep, then will execute case 2 and then case 1; total 3 beeps.
Title: Problem with SetFocus function in DialogAsMain application
Post by: dancho on January 19, 2007, 05:58:42 PM
@frankie
thx

one more nooby question:
is this code

Code: [Select]
case WM_INITDIALOG:
         SetFocus ( GetDlgItem ( hWnd , PUSHB02 ) );
         return TRUE;


same for C compiler as this

Code: [Select]
case WM_INITDIALOG:
         SetFocus ( GetDlgItem ( hWnd , PUSHB02 ) );
         return 1;

because as I understand TRUE is nonzero and FALSE is zero ?
Title: Problem with SetFocus function in DialogAsMain application
Post by: frankie on January 19, 2007, 07:28:05 PM
Yes it's the same.
TRUE is defined as 1 in windows.h. The C preprocessor, before compiling code, substitutes defined symbols with their value.
I.e.
Code: [Select]
#define MYVAL 3
.....
int count=MYVAL;

I suggest you to read one of the many C programming tutorials avalable on the net. Anyway if you have any question feel free to ask.