2008. február 5., kedd

Minimize an application when modal forms are present (2)


Problem/Question/Abstract:

My application is a non-MDI application. We use form.showmodal to call our application forms because we do not want more than one window at a time to be open. Problem: When our users minimize a window opened with showmodal, instead of minimizing the application, the showmodal window is minimized on the desktop. We need to restrict multiple windows from being open simultaneously in this application.

Answer:

Have a look at this unit, this form will do what you want. When minimizing the modal form it will minimize the app, and when restoring from the taskbar it will restore the app and bring the modal window back to front.

unit testunit;

interface

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

type
  TFormTest = class(TForm)
  private
    { Private declarations }
    OldRestore: TNotifyEvent;
    procedure MyMinimize(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
    procedure DoRestore(Sender: TObject);
  public
    { Public declarations }
  end;

implementation

{$R *.DFM}

procedure TFormTest.MyMinimize(var msg: TWMSysCommand);
begin
  if (msg.cmdtype and $FFF0) = SC_MINIMIZE then
  begin
    { the following line only for D5, not for D3, due to a bug(?) in forms.pas }
    EnableWindow(Application.handle, true);
    Application.Minimize;
    OldRestore := Application.OnRestore;
    Application.OnRestore := DoRestore;
    msg.Result := 0;
  end
  else
    inherited;
end;

procedure TFormTest.DoRestore(Sender: TObject);
begin
  Application.OnRestore := OldRestore;
  SetForegroundWindow(Handle);
end;

end.

1 megjegyzés: