TaskTrayApp

最小化するとタスクバーを占有しない、タスクトレイ利用アプリケーションのためのコード。

unit Unit1;

interface

uses
  Windows, Messages, Forms, ShellApi;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    TrayHandle: HWND;
    NotifyIconData: TNotifyIconData;
    procedure AppMinimize(Sender: TObject);
    procedure TrayWndProc(var Msg: TMessage);
  public
  end;

var
  Form1: TForm1;

implementation

const
  WM_TASKTRAY = WM_APP;
  HideStart = False;
  TipText = '';  //**** Tooltip text to display for the icon. ****

{$R *.DFM}

procedure TForm1.AppMinimize(Sender: TObject);
begin
  Hide;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMinimize := AppMinimize;
  TrayHandle := AllocateHWnd(TrayWndProc);
  with NotifyIconData do
  begin
    cbSize := SizeOf(TNotifyIconData);
    Wnd := TrayHandle;
    uID := 1;
    uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
    uCallbackMessage := WM_TASKTRAY;
    hIcon := Application.Icon.Handle;
    szTip := TipText;
  end;
  Shell_NotifyIcon(NIM_ADD, @NotifyIconData);
  if HideStart then Application.ShowMainForm := False;
end;

procedure TForm1.TrayWndProc(var Msg: TMessage);
begin
  with Msg do
  begin
    if Msg = WM_TASKTRAY then
    begin
      if LParam = WM_LBUTTONDOWN then
      begin
        Show;
        Application.Restore;
        SetForegroundWindow(Application.Handle);
      end;
    end
    else DefWindowProc(TrayHandle, Msg, WParam, LParam);
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);
  DeallocateHWnd(TrayHandle);
end;

end.

Return index page