Jump to content
Sign in to follow this  
Lajos Juhász

How to focus a window from another application?

Recommended Posts

In older versions of windows this worked:

h := FindWindow( nil,'Calculator') ;
if h = 0 then 

  WinExec('calc.exe', sw_ShowNormal)
else

  ShowWindow(h, SW_SHOWNORMAL);

 

Unfortunately on Windows 10 this doesn't work anymore. I can see that the application has 2 top-level windows unfortunately after ShowWindow, SetWindowPos or SetForegroundWindow the calculator application get focused on the taskbar and the "calculator" window turns into dark grey and doesn't get the focus. 

 

How should I adjust this code for Windows 10? 

Share this post


Link to post
1 hour ago, Lajos Juhász said:

How should I adjust this code for Windows 10?  

 

This works for me, even switches desktops if you have more that one:

BringWindowToTop(FindWindow( nil,'Calculator'));

 

EDIT: the desktop switching is a bit flaky though..

Edited by FredS
  • Like 1

Share this post


Link to post

On my laptop doesn't work.  Even cannot use directly FindWindow as it returns a window handle from the ApplicationFramHost.exe instead of the Calculator. 

 

Tried on Window handles from the calculator but the reasult is same as with ShowWindow or SetWindowPos.

 

 

Share this post


Link to post

@Lajos Juhász I assume you replied to me.. best to use a tag like I just did or quote some part of the text else a user never gets alerted. Only checked back here because of a 'Like'..

 

Maybe we are missing some info?
All this assumes your calls are made from the Active input Window, and Calculator actually has the 'Calculator' caption.
I did run SpyXX first and my version 20H2 has an actual window 'Calculator' buried within that framework and BringWindowToTop claims, and does on my system, bring that to the Top.

 

 

 

 

Share this post


Link to post

Is it the calculator window that you want to bring to the top, or is this just an example? And what if there are multiple windows so named? 

Share this post


Link to post
9 minutes ago, David Heffernan said:

Is it the calculator window that you want to bring to the top, or is this just an example? And what if there are multiple windows so named? 

The calculator. It's a kiosk type application and the user will have only one instance of calculator open at a time. 

Share this post


Link to post
45 minutes ago, FredS said:

I did run SpyXX first and my version 20H2 has an actual window 'Calculator' buried within that framework and BringWindowToTop claims, and does on my system, bring that to the Top.

 

On My System using reticle (https://chapmanworld.com/2018/10/04/custom-vcl-control-for-getting-window-handles-spy-style-reticle/) I get:

 

Handle: 2952088
Class: Windows.UI.Core.CoreWindow
Caption:Calculator
Ancestor handle: 396212
Ancestor class: ApplicationFrameWindow
Ancestor caption: Calculator

 

I can focus Ancestor handle but it will not get the keyboard focus. 

 

In order to focus I have to do:

   ShowWindow(<Ancestor Handle>, SW_SHOWNORMAL );
   SetForegroundWindow(<Ancestor Handle>);
   ShowWindow(<Handle>, SW_SHOWNORMAL );

 

Tomorrow I am going to try to find a way to get the handle of the Windows.UI.Core.CoreWindow didn't managed to get it using FindWindowEx.

Share this post


Link to post
2 hours ago, Lajos Juhász said:

can focus Ancestor handle but it will not get the keyboard focus.

BringWindowToTop give it keyboard focus here.. of course I'm not dealing with minimized..
FYI it also gets focus if on a different virtual desktop.

 

This works here, suspended or not..

    hFrame := FindWindow('ApplicationFrameWindow', 'Calculator');
    if isIconic(hFrame) then ShowWindow(hFrame, SW_RESTORE)
    else BringWindowToTop(hFrame);

 

Edited by FredS

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×