Click here to Skip to main content
15,904,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use CreateWindow to create a window, say window 'A', and window 'A' creates window 'B'. when 'B' is closed, focus is given to 'A', which is reasonable. buf if before closing 'B' I click on the window of another application and then close 'B', then the systems transfers focus to that application, not 'A'. I want to change this behaviour and try setfocus to window 'A' directly, buf I was not successfull. I tried these codes
C++
SetFocus( hWndA );
SendMessage( hWndA, WM_SETFOCUS, 0, 0 );
PostMessage( hWndA, WM_SETFOCUS, 0, 0 );
SendMessage( hWndA, WM_APP, 0, 0 );
PostMessage( hWndA, WM_APP, 0, 0 );

where window 'A' responds to WM_APP by setting focus to itself, and none of them workded. SetFocus simply failes, Can any body help me?
Posted
Updated 29-May-15 1:21am
v2

For a Window, you should activate it. A focus is a property of a control. More exactly, this keyboard focus; only one control at a time in the whole system can have it.

You need to use this Windows API:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646311%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539%28v=vs.85%29.aspx[^].

When a window is done foreground and activated, you can focus one of its controls.

—SA
 
Share this answer
 
Hi.
one method, I think.
When 'A' create 'B' window, you can inherit the 'A''s windows handle to 'B' window.
and in windowprocmessage of 'B', you can implement WM_DESTROY as follow.
1.set focus to 'A' window by handles, which is given from 'A' window.
and close 'B'window.
2.use ShowWindow function as parameter with the handles which you got from 'A' window.
 
Share this answer
 
Comments
mr.abzadeh 21-Mar-12 16:20pm    
Thanks for your interest
I couldn't know clearly what you point out. how to "inherit the 'A''s windows handle to 'B' window"? I used both SetFocus and ShowWindow, and none of them worked
You might be better to use ShowWindow(HWND hwnd) function instead of SetFocus() function.
Just do like this.
ShowWindow(hWndA, SW_HIDE);
ShowWindow(hWndA, SW_SHOW);

it will set focus to 'A' window.
 
Share this answer
 
Comments
mr.abzadeh 21-Mar-12 16:27pm    
ShowWindow didn't work, I did a thrick to solve the problem. Instead of
case WM_CLOSE:
SetFocus( hWndA );
DestroyWindow( hWndB );
I used this code
case WM_CLOSE:
SetFocus( hWndA );
PostMessage( hWndB, WM_APP_CLOSE, 0, 0 );
break;
case WM_APP_CLOSE:
DestroyWindow( hWndB );
break;
Indeed, SetFocus works if the message being processed does not destroy the window 'A', and I postponed the window destruction by posting an application defined message.
[no name] 29-Mar-12 22:15pm    
I mean to use both two statement with SW_HIDE and SW_SHOW parameters.
if you set the Owner of B to A, your problem will solve.
 
Share this answer
 
You can try following:
SetForegroundWindow(FindWindowA(NULL,"Name of the window you want to set focus to"));

This worked for me to set focus to particular window.

Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900