Solved

changing focus

Posted on 1997-05-08
8
266 Views
Last Modified: 2010-04-06
I am trying to set the focus to an application other than the calling application. I seem to remember making a call to  SetForegroundWindow in the past yet it doesnt seem to work.  The app is activated by making a call to ShellExecuteEx so i have the Handle. what am i doing wrong. I may be Mistaken and used to call another API in the past. Platform :- win95 Delphi20.
0
Comment
Question by:Mdib
8 Comments
 
LVL 1

Expert Comment

by:olmy
ID: 1336014
You should try this

  ShowWindow(Wnd: HWnd; CmdShow: Integer): Bool;

But you need to retrive to windows Hwnd handle to use it, if you know windows classname or windowname.

  FindWindow(Classname, WindowName: PChar): HWnd;
0
 

Author Comment

by:Mdib
ID: 1336015
I use winsight to get the handle to the window and use the handle, The function returns success but the window is not bought to the forground..
0
 
LVL 3

Expert Comment

by:sperling
ID: 1336016
SetForegroundWindow does work if you pass it the correct handle, but you can't get this handle from ShellExecuteEx, you need to use FindWindow or more likely EnumWindows.

Could you list the window classes and names for the app as reported by WinSight? If so, I'll write you some code.



0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 5

Expert Comment

by:ronit051397
ID: 1336017
1. use SetActiveWindow(handle of the window)
2. in order to find the handle of the window use
FindWindow(windowclass,nil)
3. in order to find the class of the window use the spy program.
0
 

Author Comment

by:Mdib
ID: 1336018
SetActiveWindow will only bring the window to the forground if the window belongs to the application making the call. I am trying to activate a vindow from a different app.

0
 

Author Comment

by:Mdib
ID: 1336019
Comment for Sperling:-

I would appreciate an example of EnumWindows callback. Essentially what i am trying to accomplish is to activate an application. If an instace of that application already exists then simply set the focus to the existing instance. Note that the application is a third party app such as netscape for example so i can not chech for previous instance from within the app. I will know befor hand wich apps i will want to run so i would have no problem making a comparison of the windows class.  If you can think of a better way of doing this I would be glad to hear it. for the purpose of this exercise (EnumWindows) feel free to use any windows class or you can use RegEdit_RegEdit.

thanks
0
 
LVL 3

Accepted Solution

by:
sperling earned 100 total points
ID: 1336020
Here's some code using EnumWindows.

Basically, use it by calling FocusWindow('NameOfClass')

The code will focus the first top-level window where the class name *contains* the string passed to FocusWindow.

Delphi's main window is of class 'TAppBuilder', therefore FocusWindow('builder') should activate Delphi.

You probably should make the code case-sensitive and require exact class names as you know the exact names.

-------------------

var
  GClassToFind : PChar;

function EnumWindowsCallback (hWnd : INTEGER; lParam : INTEGER) : BOOL; stdcall;
var
  a      : ARRAY [0..99] OF CHAR;
begin
  Result := TRUE;
  if (GetClassName (hWnd, a, SizeOf(a)) > 0) then begin
    StrUpper(a);
    if StrPos (a, GClassToFind) <> nil then begin
      SetForegroundWindow(hWnd);
      Result := FALSE;
    end;
  end;
end;

procedure FocusWindow (AClass : STRING);
begin
  GClassToFind := StrAlloc(Length(AClass)+1);
  StrPCopy(GClassToFind, UpperCase(AClass));
  EnumWindows(@EnumWindowsCallback, 0);
  StrDispose(GClassToFind);
end;

-----------


Regards,

Erik.
0
 

Author Comment

by:Mdib
ID: 1336021
Thnaks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question