How to hide an application from taskbar in Windows 7?

后端 未结 3 1533
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 17:33

I would like to hide an application from the Windows 7 taskbar.

I want to make something like a toolbar on the edge of the screen which does certain things when the

相关标签:
3条回答
  • 2020-12-09 18:32

    your application main form is normally created in the dpr so open the dpr and look for the line that creates the main form.

    // add this line first
    // blank app title will prevent app from showing in the applications list in task manager
    Application.Title := '';
    
    // this line is already in the dpr and creates the main form, the class will differ
    Application.CreateForm(TMainForm, Result);
    
    // make the main form invisible to windows taskbar/task switcher
    i := GetWindowLong(Application.Handle, GWL_EXSTYLE);
    SetWindowLong(Application.Handle, GWL_EXSTYLE, i OR WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW);
    

    i know this works on XP and 7. i'm guessing it's good for 8 as well. this adds the tool window flag and removes the appwindow flag so i guess if you're not interested in the toolwindow flag you can leave out the following part

    i OR WS_EX_TOOLWINDOW
    
    0 讨论(0)
  • 2020-12-09 18:36

    You can override the main form's CreateParam to remove the flag that forces the taskbar button (WS_EX_APPWINDOW) and additionally make the form owned by the application window. That's doing the opposite of the requirement for the shell to place a taskbar button for a window. From "Managing Taskbar Buttons":

    [..] To ensure that the window button is placed on the taskbar, create an unowned window with the WS_EX_APPWINDOW extended style. [..]

    Sample:

    type
      TForm1 = class(TForm)
      protected
        procedure CreateParams(var Params: TCreateParams); override;
      end;
    
    procedure TForm1.CreateParams(var Params: TCreateParams);
    begin
      inherited;
      Params.ExStyle := Params.ExStyle and not WS_EX_APPWINDOW;
      Params.WndParent := Application.Handle;
    end;
    

    Don't change the state of MainFormOnTaskbar property of the 'Application' from its default 'True' if you use this method.

    You can also remove the second line (..WndParent := ..) and instead set PopupMode of the form to pmExplicit in the object inspector to same effect.


    BTW, here's the documentation quote from the same topic for the solution TLama posted:

    To prevent the window button from being placed on the taskbar, [...] As an alternative, you can create a hidden window and make this hidden window the owner of your visible window.

    When you set MainFormOnTaskbar to false, the main form is owned by the application window by VCL design. And if you hide the application window, the requirement is fulfilled.

    0 讨论(0)
  • 2020-12-09 18:39

    Try to use a tricky way described in this article:

    Set the MainFormOnTaskBar to False in your project file. Then try to hide the application window from the main form's OnShow and OnActivate event handlers. So your project might look like follows:

    Project1.dpr:

    program Project1;
    
    uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1};
    
    {$R *.res}
    
    begin
      Application.Initialize;
      Application.MainFormOnTaskbar := False;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    

    Unit1.pas:

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;
    
    type
      TForm1 = class(TForm)
        procedure FormShow(Sender: TObject);
        procedure FormActivate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.FormShow(Sender: TObject);
    begin
      ShowWindow(Application.Handle, SW_HIDE);
    end;
    
    procedure TForm1.FormActivate(Sender: TObject);
    begin
      ShowWindow(Application.Handle, SW_HIDE);
    end;
    
    end.
    
    0 讨论(0)
提交回复
热议问题