Solved

shortcut settings: start minimized

Posted on 1997-04-30
7
861 Views
Last Modified: 2010-05-18
I'm trying to create Delphi 2.0 application which responds properly when started
with icons with Run: "Minimized" specified. And I just can't do it.

Borland's TI 2778 says about it this:

A: Delphi's Application object creates a hidden "application
   window," and it is that window, rather than your main form,
   that is being sent the command to show minimized. To fix this,
   make your main form's OnCreate event handler look like this:

      procedure TForm1.FormCreate(Sender: TObject);
      {$IFDEF WIN32}           { Delphi 2.0 (32 bit) }
      var
        MyInfo: TStartUpInfo;
      {$ENDIF}
      begin
      {$IFDEF WIN32}           { Delphi 2.0 (32 bit) }
        GetStartUpInfo(MyInfo);
        ShowWindow(Handle, MyInfo.wShowWindow);
      {$ENDIF}
      {$IFDEF WINDOWS}         { Delphi 1.0 (16 bit) }
        ShowWindow(Handle, cmdShow);
      {$ENDIF}
      end;


If I do this, TApplication and main form get out of sync. Application (i.e. the
icon in the toolbar) think the application is running restored (it has minimize
menu item enabled while restore is disabled). Main form is even worse. It is
"minimize-like" in the lower left corner of desktop, just over the toolbar, as
if it was MDI child of the desktop. I tried to put Application.Minimize call in
FormCreate or FormShow, but it doesn't make matters better.

I have an ugly "fix": at the end of FormCreate to post the message (minimize me)
if minimize flag is on in the shortcut. On response to the message I then
minimize the application. Result is that app starts normal and immediately after
minimizes. Looks ugly.

Does anyone know how to do it properly, or Borland guys just forgot about that
feature ?

Thank you in advance
0
Comment
Question by:lkovac
  • 4
  • 2
7 Comments
 
LVL 3

Expert Comment

by:sperling
ID: 1335798
You can't do it "properly"... Don't know if Borland forgot it or just didn't bother...

Try a simple fix.... Set your FormStyle to wsMinimized, and then in OnCreate write something like this:

GetStartupInfo...
case MyInfo.wShowWindow of
  SW_SHOW,
  SW_SHOWNORMAL : Application.Restore;
  SW_MAXMIMIZE : PostMessage(Application.Handle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
  SW_MINIMIZE : ; // Already minimized
end


You'll probably have to add some more of the warious SW_XXX constants in the case statement, but this would at least let you mimic proper startup behaviour.

Regards,

Erik.



0
 

Author Comment

by:lkovac
ID: 1335799
No, Eric, this doesn't work. I didn't try with Maximized, but that wsMinimized makes main form to create that ugly minimized window in lower left corner just over the taskbar (as if desktop was MDI parent for it) (at least on NT 4.0 it is doing that). My solution with posting user defined message is much better, I even improved it with Hiding (see following code), but there is still that ugly animation going first up then down... However, I still hope somebody has better solution (maybe even the hack to VCL source code) because with this problem how can one create commercial product with Delphi ?

Here is my code:

const
  WM_MINIMIZE = WM_USER + 1;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    { Private declarations }
    bMinimize: boolean;
    procedure Minimize(var msg: TMessage); message WM_MINIMIZE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  si: TStartupInfo;
begin
  si.cb := SizeOf(TStartupInfo);
  GetStartupInfo(si);
  if ((SW_MINIMIZE = si.wShowWindow) or (SW_SHOWMINIMIZED = si.wShowWindow) or (SW_SHOWMINNOACTIVE = si.wShowWindow)) then
  begin
    bMinimize := True;
    PostMessage(Handle, WM_MINIMIZE, 0, 0);
  end
  else
    bMinimize := False;
end;

procedure TForm1.Minimize(var msg: TMessage);
begin
  Application.Minimize;
  bMinimize := False;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  if bMinimize then
    Hide
  else
    if not Visible then
      Show;
end;




0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335800
This is how I did it successfully in Delphi 1.0. I haven't tried it in Delphi 2 yet, but I see no reason for it not to work

   procedure TForm1.FormCreate(Sender: TObject);
   begin
      ShowWindow(Handle, CmdShow);
      {the rest of your program}
   end;

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:lkovac
ID: 1335801
No, that doesn't work in Delphi 2.0 . Delphi 2.0 is completely different in this problem.
0
 

Author Comment

by:lkovac
ID: 1335802
Adjusted points to 200
0
 
LVL 2

Accepted Solution

by:
gysbert1 earned 200 total points
ID: 1335803
It seems this is definately a bug in delphi2. I tested this and it does work in delphi1 !!

There is a hack that will solve your problem, although it is pretty crude ;^)

In oncreate set the position of your form according to the windowstate passed to the application as above. If it should be minimized call showwindow(handle,SW_ShowNoActivate); as well as setting the position to something ridiculous like left:=10000. In your onactivate you can now set the left property to what you want it. Even save it if you want to be thorough ...

The code in the borland TI's as well as the help files suggests your original code should work but on my system it also creates a minimized child some invisible MDI parent !?



0
 

Author Comment

by:lkovac
ID: 1335804
Great reply. Using your advice I was able to make it work. However, it
needed little bit more tweaking (regarding the animation when minimizing on
NT and making application aware of state (right click on icon on taskbar
should enable restore or minimize properly).

This is the solution for application which can be "normal" or minimized
(dialog main form). If it could be also maximized, solution would be much
more complicated. Here is the full solution:

unit MainForm;

interface

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

const
  WM_MINIMIZE = WM_USER + 1;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    bMinimize: boolean;
    l, t: integer;
    procedure Minimize(var msg: TMessage); message WM_MINIMIZE;
    procedure WMMove(var Message: TWMMove); message WM_MOVE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  si: TStartupInfo;
begin
  l := Left;
  t := Top;
  si.cb := SizeOf(TStartupInfo);
  GetStartupInfo(si);
  if ((SW_MINIMIZE = si.wShowWindow) or (SW_SHOWMINIMIZED = si.wShowWindow) or (SW_SHOWMINNOACTIVE = si.wShowWindow)) then
  begin
    bMinimize := True;
    MoveWindow(Handle, 3000, 3000, Width, Height, False);
    PostMessage(Handle, WM_MINIMIZE, 0, 0);
  end
  else
    bMinimize := False;
end;

procedure TForm1.Minimize(var msg: TMessage);
begin
  if bMinimize then
    ShowWindow(Application.Handle, SW_HIDE);
  Application.Minimize;
  if bMinimize then
  begin
    bMinimize := False;
    Left := l;
    Top := t;
    ShowWindow(Application.Handle, SW_SHOWNA);
  end;
end;

procedure TForm1.WMMove(var Message: TWMMove);
begin
  if not bMinimize then
    MoveWindow(Application.Handle, Left, Top, Width, 0, False);
end;

end.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

22 Experts available now in Live!

Get 1:1 Help Now