• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Focus wnd while rest of app is minimized

How do I activate a window and let the rest of my program be minimized?

It's like this:
I have an app which minimizes and run an OLE-embedd on Word. Then when users press certain buttons in Word I want a to fire up a dialog from my program on top of Word but I do not want the rest of my program to be on top of Word it should stay minimized. Can I accomplish this without having to use DLL's?
0
dr_gonzo
Asked:
dr_gonzo
  • 2
1 Solution
 
ZifNabCommented:
Sure it's possible, just make sure that the parent of the popup form isn't the main form of your program, but something like the program manager, here is some code (I learned it from the example at Delphi Cornor of Brendan Delumpa :

Making Secondary Forms Independent of the Main Form

In my application, I want to be able to iconize the main form and still leave the secondary forms displaying on the desktop. Likewise, I want to be able to select
secondary forms without the main form popping up. How can I do this?

Recently a user asked me about this, and I had to do a bit of experimentation before I finally figured it out. And the solution to this problem is actually so simple, you'll
scream (actually, I did all the screaming myself). But it's not something that's necessarily easy to find out nor intuitive (maybe it is for some, but it wasn't for me). But
before I give you the solution, let's discuss the concept that's behind it.

All windowed controls have a parent of some sort; that is, some control that maintains visual control (ie. display) over it. Main forms of an application all point to the
Application as their parent. Likewise, by default, secondary forms point to the main form of the application for parentage. But the neat thing about creating windowed
objects in Delphi (though you need to be careful with some controls) is that you can change the parentage of a control to isolate its visual control, essentially giving it
independence from its default parent. Okay, so how do you do it? You might think that you can reset parentage at FormCreate, but that's not the right place to do it.
The only way to do this is before the window gets created in the first place, and that place is in the CreateParams procedure.

I've discussed CreateParams in previous articles, so I won't go into details about it, though I will brush over what it does. CreateParams is an inherited procedure that
wraps the WinAPI functions CreateWindow and CreateWindowEx that are responsible for a window's initial appearance. It's a convenient way to set display
parameters. With it, we can change the a variable parameter called Params that is a TCreateParams structure (you should look this structure up in the online help) to
affect a number of different things about a form. One of the fields in the TCreateParams structure is WndParent. This parameter specifies the handle of the window that
controls the display of the window being created. By changing this parameter to point another window handle (hint, hint), we can change the default parentage.

So now it's a matter of deciding what window is going to be the secondary form's new parent. In this case, whenever we want to make a secondary form independent of
the main form, we're essentially turning it into its own mini-application without creating a new EXE. So it's best to choose a parent that's at the highest order in the
system. That window is Windows' Desktop Window. Fortunately we have a way of getting its handle by using the WinAPI call GetDesktopWindow, which returns the
handle of the Desktop.

Okay, we've covered all the bases. Now you're going to kill me for belaboring the point. Here's the code:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm2 = class(TForm)
  private
    { Private declarations }
    //override the CreateParams procedure for any child forms you want to
    //make independent of the main form
    procedure CreateParams(var Params : TCreateParams); override;
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

//Here's the implementation of CreateParams
procedure TForm2.CreateParams(var Params : TCreateParams);
begin
  inherited CreateParams(Params); //Don't ever forget to do this!!!
  Params.WndParent := GetDesktopWindow;
end;

end.

Insanely simple, huh? Sorry I took so long to lead up to it, but while the solution was simple, I just couldn't get away from explaining at least a bit of background
information to help those who aren't familiar with the internal workings of the WinAPI. In any case, HAVE AT IT!!!

Greetings, Zif.
0
 
dr_gonzoAuthor Commented:
Yepp, simple should have thought about it. But there's one thing left. This trick creates a task bar icon and dialog's really don't do that. How can I get rid of it?
0
 
ZifNabCommented:
;-) Look at the answer pages of Brendan delumpa. He's got an example for that too.

http://wwW.delumpa.com/dc_main.html 
 -> tips/articles -> hiding an application from windows

Greetings Zif.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now