Solved

Activating an application and correctly setting the focus

Posted on 2007-11-14
4
2,697 Views
Last Modified: 2013-11-22
I've a series of applications which can be used concurrently. The user can switch between the applications without alt-tabbing, from within each application.

This works fine, but...

If application A switches to application B, and application B has a modal dialog open, it's the main form which is shown - not the modal dialog. The user then clicks on the main form and of course nothing happens other than a series of beeps. The user has to alt tab to bring the modal dialog into view and then close this to allow access to the application.

How can I achieve the same result as alt-tabbing? This is how I'm doing it at the moment...

     Result := FindWindow(Pchar(ThisWindowClass), nil);
     ShowWindow(Result, SW_RESTORE);
     ShowWindow(Result, SW_SHOWMAXIMIZED);
     SendMessage(Result, WM_ACTIVATEAPP, 1, 0);


Thanks for any help.


Steve
0
Comment
Question by:steve-west
[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
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:developmentguru
ID: 20283097
I found some code that does this, here is the link.  check out the section on how to steal focus.

http://www.codeproject.com/dialog/dlgboxtricks.asp?df=100&forumid=3124&exp=0&select=1718923

If you need me to provide a Delphi example, I will see what I can do.

Let me know.
0
 

Author Comment

by:steve-west
ID: 20287767
Thank you for this, but I don't see any topics within the link which are relevant.

There is a section which relates to setting the focus to a KNOWN modal dialog but this is not what I'm after.

Application A switches to application B.

Application B could have nth number of Modal dialog boxes opened on top of each other. The way I'm currently doing this results in only the main form of application B showing, but with the dialog boxes open, the application is effectively disabled.

I need to do exactly what Alt-tabbing does - i.e. show the topmost dialog. I could send a message to the application telling it to show the topmost dialog, but how do I do this (showing the window that is, not the messagey part).

Regards

Steve
0
 
LVL 21

Accepted Solution

by:
developmentguru earned 500 total points
ID: 20291463
I used code directly off the link in my example.  I created one program that just had a buton that brought up the modal file open dialog.  I called the form TLaunchDialog and the title was LaunchDialog.  I left it running.  In my second application I used the following code and it works as you stated you wanted it to.

procedure TForm2.Button1Click(Sender: TObject);
var
  WindowHandle : THandle;

begin
  WindowHandle := FindWindow('TLaunchDialog', 'LaunchDialog');

  if WindowHandle <> 0 then
    begin
      AttachThreadInput(GetWindowThreadProcessId(WindowHandle),
        GetCurrentThreadID(), true);

      SetForegroundWindow(WindowHandle);

      AttachThreadInput(GetWindowThreadProcessId(WindowHandle),
        GetCurrentThreadID(), false);
    end;
end;

Let me know if you need more.
0
 

Author Comment

by:steve-west
ID: 20297014
I agree, the solution you propose will return focus to an application with an open modal dialog.

The problem with this method, you have to know the class of the form you are bringing to the front. The applications that the user can switch between them hold over 3000 forms so it's not practical to code for each class type.

I've eventually managed to implement this successfully with the following approach.

1. Switch to application B from A by finding application's B main window and showing that.

2. Send application B a user defined message

3. When Application B receives this, it runs through the list of all open screens and for any screen which itself has an open screen, then bring the topmost screen to the front.


Since your solution does work, although not really suitable for my situation, I'll award the points.

Thanks fro your help

Steve







procedure TPRIAMCoreMainF.UMEnsureRestored(var Msg: TMessage);
var
  iCounter : integer;
  ComCounter: Integer;
  HasScreens : boolean;
begin
  //
  // Restore application
  if IsIconic(Application.Handle) then
    Application.Restore;
  if not Visible then
    Visible := True;
  Application.BringToFront;
 
  //
  // Ensure all TOPMOST modal screens are brought to the front
  for iCounter := 0 to Screen.FormCount - 1  do
  begin
    if Screen.Forms[iCounter].Visible and
      (Screen.Forms[iCounter].FormStyle = fsNormal) and
      //
      // ignore embedded forms
      (not (Screen.Forms[iCounter] is TPriamConnF))  then
    begin
        HasScreens := false;
        for ComCounter := 0 to Screen.Forms[iCounter].ComponentCount - 1 do
        begin
          if Screen.Forms[iCounter].Components[ComCounter] is TForm then
          begin
            HasScreens := true;
            TForm(Screen.Forms[iCounter].Components[ComCounter]).BringToFront;
            Break;
          end;
        end;
        if not HasScreens then
          Screen.Forms[iCounter].BringToFront;
    end;
  end;
end;

Open in new window

0

Featured Post

Independent Software Vendors: 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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

726 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