tags:

views:

43

answers:

3

1.I want to find a window and set focus on it , but window is not taking the focus.

2.If i use HWND_TOP then it does not make the window active and if i use HWND_TOPMOST then it make window always on top.

Can anyone help me ??

HWND hwndAppDlg = ::FindWindowEx(hwndDesktop,NULL,lpszClass,lpszWindow);

    if(hwndAppDlg && IsWindow(hwndAppDlg))
    {
        CRect rcAppDlg;
        if( 0 == ::GetWindowRect(hwndAppDlg,rcAppDlg))
        {
            OutputDebugString(L"\n GetWindowRect failed...");           
            return FALSE;
        }
        if(0 == ::SetWindowPos(hwndAppDlg,HWND_TOPMOST,rcAppDlg.left,rcAppDlg.top,rcAppDlg.Width(),rcAppDlg.Height(),SWP_SHOWWINDOW))
        {
            OutputDebugString(L"\n SetWindowPos failed...");            
            return FALSE;
        }
        if(0 == ::PostMessage(hwndAppDlg,WM_SETFOCUS,0,0))
        {
            OutputDebugString(L"\n WM_SETFOCUS failed");        
            return FALSE;
        }

        return TRUE;
    }
A: 

How about ShowWindow( hwndAppDlg, SW_SHOW );

Timbo
it does not even activate the dialog.
Ashish
A: 

I have used ::SetForegroundWindow(hwndAppDlg) to activate and set focus on the dialog and it works cool.

Ashish
+2  A: 

You're sending WM_SETFOCUS, but that does not set the focus. That message is sent to a control if it gained or lost the focus, but when that message is sent/received, the focus change has already happened.

To actually set the focus (you don't need to send the WM_SETFOCUS message), use SetFocus() if you know which control in the dialog should get the focus, or SetForegroundWindow() to set the focus to the dialog itself and let the dialog determine which subcontrol actually will get the focus. Both of these APIs will send WM_SETFOCUS automatically.

Stefan