首页 > 代码库 > dephi XE - 实现主窗口在任务栏上不显示 (转)

dephi XE - 实现主窗口在任务栏上不显示 (转)

方法一

以前的代码不能用。貌似在Delphi2010后就开始不行了,搜索了一下,得到办法: 1 修改工程文件添加一句:Application.MainFormOnTaskbar := False; 2 在主窗体的 OnShow 事件中写下 varStyle: Integer;beginStyle := GetWindowLong(Handle, GWL_EXSTYLE);SetWindowLong(Handle, GWL_EXSTYLE, Style and (not WS_EX_APPWINDOW)); ShowWindow(Application.Handle, SW_HIDE);end;

方法二

作者:兰若笑

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.