Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know how to focus a winodw of different process.
But I am able to focus only the Main form of the process.

But I want to focus another form in that process.

Here's what I've done.


C#
[DllImport("user32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);



C#
Process currentProcess = Process.GetCurrentProcess();
IntPtr hWnd = currentProcess.MainWindowHandle;
if (hWnd != IntPtr.Zero)
{
    SetForegroundWindow(hWnd);
    ShowWindow(hWnd, int.Parse("9"));
}


The above code is focusing only the Main Form of that Process.
How to focus other form in it.
Posted
Comments
[no name] 1-Sep-14 6:06am    
Form inside another form? You mean like a child window? You would need to get the handle to the child window.
KUMAR619 1-Sep-14 6:10am    
How to get the handle of the child window sir and how to Make it work sir

1 solution

This action is not "focus"; this is "activate".

In the simplest case, you can use combination of SetActiveWindow with SetForegroundWindow. However, it works poorly in one case: the moment of time when the window to be activated opens a modal dialog.

So, the comprehensive solution is this: first, using the main window handle, you have to find the handle of this dialog, if any. This is done using the function GetLastActivePopup. This function will return either the handle of the dialog, or the handle of the main window which you already know. Activate that window.

Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx[^],
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[^].

—SA
 
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