Solved

changing focus

Posted on 1997-05-08
8
264 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending Gmail through Delphi 3 108
How to call a form that is in a DLL  from an application? 13 71
Delphi Form ownership 4 89
Find and Replace Stream with 0s 8 62
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 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