Thread: Setting focus on DialogBox in DialogBox while both creating

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    Setting focus on DialogBox in DialogBox while both creating

    1st sorry for my english. 2nd - I have a big problem, which I cannot solve. I have this simple
    code:
    Code:
    #include <windows.h>#include <windowsx.h>
    #include <commctrl.h>
    #include "main.h"
    
    
    BOOL CALLBACK DlgProc( HWND, UINT, WPARAM, LPARAM);
    HINSTANCE hInst;
    
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) {
    
    
        hInst = hInstance;
    
    
        INITCOMMONCONTROLSEX icc;
        icc.dwICC = ICC_WIN95_CLASSES;
        icc.dwSize = sizeof( icc );
        InitCommonControlsEx( &icc );
    
    
        WNDCLASSEX wcx;
        wcx.cbSize = sizeof( wcx );
    
    
        if ( !GetClassInfoEx(NULL, MAKEINTRESOURCE(32770), &wcx) ) {
            MessageBox( NULL, "Error creating class.", "Error", MB_OK | MB_ICONERROR );
            return 0;
        }
    
    
        wcx.hInstance = hInstance;
        wcx.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
        wcx.lpszClassName = "VantSnake";
    
    
        if ( !RegisterClassEx(&wcx) ) {
            MessageBox( NULL, "Error creating application.", "Error", MB_OK | MB_ICONERROR );
            return 0;
        }
    
    
        return DialogBox( hInstance, MAKEINTRESOURCE(VNTH_MAIN), NULL, (DLGPROC) DlgProc );
    
    
    }
    
    
    BOOL CALLBACK DlgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
    
    
        switch ( msg ) {
            case WM_COMMAND:
                switch ( GET_WM_COMMAND_ID(wParam, lParam) ) {
                    case IDOK:
                        EndDialog( hWnd, 0 );
                        break;
                }
                break;
            case WM_CLOSE:
                EndDialog( hWnd, 0 );
                break;
            case WM_INITDIALOG:
                if ( !FindWindow(NULL, "Intro") ) {
    
    
                    // creates Intro dialog box
                    CreateDialog( GetModuleHandle(NULL), MAKEINTRESOURCE(VNTH_START), hWnd, (DLGPROC) DlgProc );
                } else {
    
    
                    // here i'm trying to set focus on it, but it doesnt't works
                    SetForegroundWindow( hWnd );
                    SendMessage( hWnd, WM_NEXTDLGCTL, (WPARAM) GetDlgItem(hWnd, VNTH_START), TRUE );
    
    
                }
                return 1;
        }
    
    
        return 0;
    
    
    }
    I create one DialogBox as main application window and second (Intro), which appears at the start as well in WM_INITDIALOG case. The problem is, that the main window, although it's under the Intro window - is focused. I was trying to set focus into Intro window, as I found in other thread (by SetForeground and SendMessage) but it's still not working. I
    m little bit frustrating, because I'm trying to solve it for hours with no effect. Please, if anyone has solution for that, help. The link for Pelles C project: http://www.filolozka.pl/C/VantSnake.zip
    Of course VNTH_MAIN and VNTH_START are resource linkages to Main and Intro windows.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    From MSDN on WM_INITDIALOG

    The dialog box procedure should return TRUE to direct the system to set the keyboard focus to the control specified by wParam. Otherwise, it should return FALSE to prevent the system from setting the default keyboard focus.
    So you may set the focus to the Intro dialog but then you return true and set the focus to the main dialog.

    Also...


    Code:
    SendMessage( hWnd, WM_NEXTDLGCTL, (WPARAM) GetDlgItem(hWnd, VNTH_START), TRUE );
    I do not think this will work, as VNTH_START is a dialog, not a control on the main dialog.

    Code:
    CreateDialog( GetModuleHandle(NULL), MAKEINTRESOURCE(VNTH_START), hWnd, (DLGPROC) DlgProc );
    Create a WNDPROC / callback for each dialog. ie create a new callback for the intro dialog (ie IntroDlgProc).

    Re-using the same callback is a very bad idea.

    Code:
    CASE WM_INITDIALOG:
    	HWND hIntroWnd = FindWindow(NULL, "Intro");
    	//did we find the window?
    	if(hIntroWnd == NULL)
    	{
    		//into window not found so create it
    		CreateDialog( GetModuleHandle(NULL), MAKEINTRESOURCE(VNTH_START), hWnd, (DLGPROC) IntroDlgProc ); 
    	}
    
    	//now set focus on into window
    	SetForegroundWindow( hIntroWnd ); //make sure we use the Intro Windows HWND
    	//do not set the focus
    	return FALSE;
    break;
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    Ok, I did everything according to your suggestions, the code of splited Callback functions (WinMain with no changes):
    Code:
    BOOL CALLBACK DlgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
    
    
        switch ( msg ) {
            case WM_COMMAND:
                break;
            case WM_CLOSE:
                EndDialog( hWnd, 0 );
                break;
            case WM_INITDIALOG:;
                HWND hwnd = FindWindow( NULL, "Intro" );
                if ( hwnd == NULL ) {
                    CreateDialog( GetModuleHandle(NULL), MAKEINTRESOURCE(VNTH_START), hWnd, (DLGPROC) DlgIntroProc );
                }
                SetForegroundWindow( hwnd );
        }
    
    
        return FALSE;
    
    
    }
    
    
    BOOL CALLBACK DlgIntroProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
    
    
        switch ( msg ) {
            case WM_COMMAND:
                switch ( GET_WM_COMMAND_ID(wParam, lParam) ) {
                    case IDOK:
                        EndDialog( hWnd, 0 );
                        break;
                }
                break;
            case WM_CLOSE:
                EndDialog( hWnd, 0 );
                break;
            case WM_INITDIALOG:
                //HWND hwnd = FindWindow( NULL, "Intro" );
                //SetForegroundWindow( hwnd );
                SetForegroundWindow( hWnd );
        }
    
    
        return FALSE;
    
    
    }
    But it's still not working. The main window, which is disabled is still on focus, not the Intro window, which is over it. The interesting thing is, that I must use ";" after CASE : statement, otherwise it throws error (invalid use of type name HWND). When I call any function first, I don't have to do that. Nevermind, it's still not working and Intro window isn't focused, unfortunately any other idea?
    Last edited by vantooh; 01-24-2013 at 03:32 AM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try something like;

    Code:
    BOOL CALLBACK DlgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
    
    //snip
                if ( hwnd == NULL ) {
                    hwnd = CreateDialog( GetModuleHandle(NULL), MAKEINTRESOURCE(VNTH_START), hWnd, (DLGPROC) DlgIntroProc );//make sure hwnd has a non null value
                }
                SetForegroundWindow( hwnd );
                return false;//return false to stop focus change
                break;//not required (if you have a specific return) but included for readablity
    
    BOOL CALLBACK DlgIntroProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
     {    
           //snip
           case WM_INITDIALOG:             
                return true; 
           break;
    }
    If the intro dlg is a splash screen then try using a modal dialog, which will disable the main dlg until it closes. ie use DialogBox() instead of CreateDialog()
    Last edited by novacain; 01-24-2013 at 09:37 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dialogbox from within a DLL
    By JustMax in forum C Programming
    Replies: 33
    Last Post: 02-03-2009, 10:23 AM
  2. DialogBox(...) error
    By Blizzarddog in forum Windows Programming
    Replies: 3
    Last Post: 09-19-2005, 03:59 PM
  3. Setting a Dialogbox Icon
    By Devil Panther in forum Windows Programming
    Replies: 1
    Last Post: 12-08-2004, 02:17 PM
  4. Putting a DialogBox into a DLL?
    By xds4lx in forum Windows Programming
    Replies: 1
    Last Post: 11-17-2002, 12:59 AM
  5. DialogBox's ICON
    By Rizwan Rafique in forum Windows Programming
    Replies: 1
    Last Post: 01-08-2002, 05:47 PM