Solved

changing focus

Posted on 1997-05-08
8
263 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

12 Experts available now in Live!

Get 1:1 Help Now