Delphi XE2写的程序,希望主窗口在任务栏上不显示,而非主窗口则在任务栏上显示。

 

unit MyForm; { 将在Form1和Form2中被引用 }

interface

uses
Winapi.Windows, Winapi.Messages, Vcl.Controls, Vcl.Forms;

type
TForm = class(Vcl.Forms.TForm)
private
FIsMainForm: Boolean;
FShowInTaskbar: Boolean;
function GetShowInTaskbar(IsMainForm: Boolean): Boolean;
procedure SetShowInTaskbar(IsMainForm: Boolean; const Value: Boolean);
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure WndProc(var Message: TMessage); override;
public
property ShowInTaskbar[IsMainForm: Boolean]: Boolean read GetShowInTaskbar write SetShowInTaskbar;
end;

implementation

{ TForm }

procedure TForm.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
if not FIsMainForm then
begin
if FShowInTaskbar then
Params.WndParent := GetDesktopWindow
else
Params.WndParent := Application.Handle;
end;
end;

function TForm.GetShowInTaskbar(IsMainForm: Boolean): Boolean;
begin
FIsMainForm := IsMainForm;
if FIsMainForm then
Result := Application.MainFormOnTaskBar
else
Result := FShowInTaskbar;
end;

procedure TForm.SetShowInTaskbar(IsMainForm: Boolean;
const Value: Boolean);
begin
FIsMainForm := IsMainForm;
if FIsMainForm then
Application.MainFormOnTaskBar := Value
else
FShowInTaskbar := Value;
Perform(CM_RECREATEWND, 0, 0);
end;

procedure TForm.WndProc(var Message: TMessage);
begin
inherited WndProc(Message);
if FIsMainForm and (not Application.MainFormOnTaskBar) and (Message.Msg = WM_SHOWWINDOW) then
begin
ShowWindow(Application.Handle, SW_HIDE);
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
end;
end;

end.

 

unit Unit1; { 主窗口 }

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, MyForm;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses Unit2;

procedure TForm1.FormCreate(Sender: TObject);
begin
ShowInTaskbar[True] := False; { 主窗口不在任务栏上显示 }
TForm2.Create(Application).Show; { 显示Form2 }
end;

end.

 

unit Unit2;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, MyForm;

type
TForm2 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

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

procedure TForm2.FormCreate(Sender: TObject);
begin
ShowInTaskbar[False] := True; { 本窗口将在任务栏上显示 }
end;

end.

 

本例源代码(Delphi XE2)下载链接如下:

https://files.cnblogs.com/d2012/ex2.zip

posted on 2012-02-25 22:20  兰若笑  阅读(996)  评论(0编辑  收藏  举报