Solved

changing focus

Posted on 1997-05-08
8
258 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now