Solved

changing focus

Posted on 1997-05-08
8
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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