Solved

shortcut settings: start minimized

Posted on 1997-04-30
7
867 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

12 Experts available now in Live!

Get 1:1 Help Now