2005. július 18., hétfő

Add Taskbar-Button's for SubForms and manage them correctly


Problem/Question/Abstract:

I would like to add some Windows' Taskbar-Buttons for dynamic created forms w/o loosing the ability to 're-focus' the MainForm by clicking it's own button.

Answer:

The following example uses a Button (Button1)to dynamic create forms at runtime. Each form will be accessable via a corresponding Button placed on the Taskbar. You had to include the WMSysCommand method to enable the real "look&feel" of minimizing the MainForm. Otherwise, the MainForm will be minimized to the lower left side of the Screen or will be hidden in the background, so it's not possible to restore it correctly.

If you want to minimize (or hide) all subforms when minimizing the MainForm, you had to iterate through all registered subforms and hide them manualy. I don't know a better way right now, but if you've found any solution...:-)

MainForm Unit

unit MainForm;

interface

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

type
  TMainForm = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure WMSysCommand(var Msg: TMessage); message WM_SYSCOMMAND;
  public
    { Public declarations }
    procedure CreateParams(var Params:
      TCreateParams); override;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

uses
  SubForm;

var
  SubForm: TSubForm;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  SetWindowLong(Application.Handle, GWL_EXSTYLE,
    GetWindowLong(Application.Handle, GWL_EXSTYLE) or
    WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);
end;

procedure TMainForm.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW and not WS_EX_TOOLWINDOW;
end;

procedure TMainForm.WMSysCommand(var Msg: TMessage);
begin
  DefaultHandler(Msg);
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin
  SubForm := TSubForm.Create(Application);
  SubForm.Show;
end;

end.

SubForm Unit

unit SubForm;

interface

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

type
  TSubForm = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure CreateParams(var Params: TCreateParams); override;
  end;

implementation

{$R *.DFM}

procedure TSubForm.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
end;

procedure TSubForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése