How to stop the application from appearing on the taskbar?

My application has the ability to run it only in the system tray, and not on the taskbar. This worked great when my application was created by Delphi 6. After switching to Delphi XE2, it no longer works.

I messed up something, and it works for me for Windows 7, but I still have a problem with Windows XP. The application is correctly hidden from the taskbar and displayed on the taskbar. But when I create and show any additional form, the icon appears in Windows XP.

procedure TfrmAppointment.HideWindowFromTaskbar;
var
   TaskbarList: ITaskbarList;
begin
Application.MainFormOnTaskBar := False;

// Windows 7 seems to behave differently.  This seems to fix it.
if (CheckWin32Version(6, 1)) then
    begin
    // We are in Win7, and we requested the tray.
    TaskbarList := CreateComObject(CLSID_TaskbarList) as ITaskbarList;
    TaskbarList.HrInit;
    TaskbarList.DeleteTab(Application.Handle);
    end
else
   begin
   // Previous code from D6 days
   ShowWindow(Application.Handle, SW_HIDE);
   SetWindowLong(Application.Handle, GWL_EXSTYLE, GetWindowLong(Application.Handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW);
   ShowWindow(Application.Handle, SW_SHOWNOACTIVATE);
   end;
end;

, . Windows, . Windows XP, , . Windows 7 .

, ?

, , , , , Delphi 2009), MainFormOnTaskBar, , , .

[EDIT:] , . : .

. . . .

, . , , , TRect "Shell_TrayWnd" / "TrayNotifyWnd" DrawAnimatedRects(), . . . , .

Windows.

, , , , Windows XP . Windows 7 . , Windows XP , , . , . Windows 7 , .

+5
2

Application.MainFormOnTaskBar := True;

.dpr , .

, ,

MainForm.Hide;

,

MainForm.Show;

.

, .


HideWindowFromTaskbar , . MainFormOnTaskBar equals True, - . , . , , .

. , . .

, . , , . , , CreateParams. , . , , .

MSDN :

  • .
  • .

, , :

program MainFormHiding;

uses
  Forms, StdCtrls;

var
  MainForm, OtherForm: TForm;
  Button: TButton;

type
  TEventHandlerClass = class
    class procedure ToggleMainFormVisible(Sender: TObject);
  end;

class procedure TEventHandlerClass.ToggleMainFormVisible(Sender: TObject);
begin
  MainForm.Visible := not MainForm.Visible;
end;

begin
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm, MainForm);
  OtherForm := TForm.Create(Application);
  MainForm.Caption := 'Main Form';

  OtherForm.Visible := True;
  OtherForm.Caption := 'Other Form';
  Button := TButton.Create(OtherForm);
  Button.Caption := 'Toggle';
  Button.Parent := OtherForm;
  Button.OnClick := TEventHandlerClass.ToggleMainFormVisible;

  Application.Run;
end.

, , . MainFormOnTaskBar False. , Application.Handle , . , .

PopupParent . , , .

:

program MainFormHiding;

uses
  Forms, StdCtrls, Windows;

var
  MainForm, OtherForm: TForm;
  Button: TButton;

type
  TEventHandlerClass = class
    class procedure ToggleTaskbarButton(Sender: TObject);
  end;

class procedure TEventHandlerClass.ToggleTaskbarButton(Sender: TObject);
begin
  if IsWindowVisible(Application.Handle) then
    ShowWindow(Application.Handle, SW_HIDE)
  else
    ShowWindow(Application.Handle, SW_SHOW);
end;

begin
  Application.MainFormOnTaskbar := False;
  Application.CreateForm(TForm, MainForm);
  OtherForm := TForm.Create(Application);
  OtherForm.PopupParent := MainForm;
  MainForm.Caption := 'Main Form';
  Application.Title := MainForm.Caption;

  OtherForm.Visible := True;
  OtherForm.Caption := 'Other Form';
  Button := TButton.Create(OtherForm);
  Button.Caption := 'Toggle';
  Button.Parent := OtherForm;
  Button.OnClick := TEventHandlerClass.ToggleTaskbarButton;

  Application.Run;
end.

. . . , , . .

- , , , . , . - , Application.Handle. , Title .

, . , , ITaskbarList, ..


, . , , . , .

, . - TApplication.Minimize, , . , , , .

procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;

....

procedure TMainForm.WMSysCommand(var Msg: TWMSysCommand);
begin
  if (Msg.CmdType and $FFF0)=SC_MINIMIZE then 
  begin
    Hide;
    exit;
  end;
  inherited;
end;

OnMinimize TApplication.

class procedure TEventHandlerClass.ApplicationMinimize(Sender: TObject);
begin
  ShowWindow(Application.Handle, SW_HIDE);
end;
+8

. , . , . . :

Application.OnMessage := AppMessage;

:

procedure TfrmAppointment.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
// This first check decides if we are minimizing via the upper right button OR
// The context menu in the upper left hand corner of the window. 
// Minimizing twice restores, so this can be a restore as well.
if ((((Msg.message = WM_NCLBUTTONDOWN) and (Msg.wParam = HTMINBUTTON)) or
     ((Msg.message = WM_SYSCOMMAND) and (Msg.wParam = SC_MINIMIZE))) and
    (Screen.ActiveForm = Self)) then
   begin
   // This function is defined as (bool, bool) where the variables are:
   // Param1: Mimimizing (true), Restoring (false)
   // Param2: Draw animation rectangles for doing this or not
   Handled := MinimizeOrRestore(Self.WindowState <> wsMinimized, True);
   end
else if ((Msg.message = WM_SYSCOMMAND) and 
         (Msg.wParam = SC_RESTORE) and 
         (Screen.ActiveForm = Self)) then
   begin
   // Specifically, restore has been asked for
   Handled := MinimizeOrRestore(False, True); // Minimize with animation
   end
else if ((Msg.message = WM_SYSCOMMAND) and (Msg.wParam = SC_CLOSE)) then
   begin
   // The user just used the system menu to close the application
   ApplicationIsClosing := True; // see below for this
   end
end;

FormCloseQuery , "ApplicationIsClosing" . FALSE, , X, , , . , .

, MinimizeOnRestore TRect , , DrawAnimatedRects. Vista , . . true, . false.

+3

All Articles