카테고리 없음

[윈도우즈 API] 실행된 Tray Icon 변경하기

쇼핑스크래퍼2 2023. 9. 15. 08:31
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Timer1: TTimer;
    Image1: TImage;
    Image2: TImage;
    procedure Timer1Timer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  MyNotifyStruct: TNotifyIconData;

implementation
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  with MyNotifyStruct do
  begin
    cbSize := SizeOf(MyNotifyStruct);
    Wnd := Form1.handle;
    uID := 1;
    uFlags := NIF_ICON or NIF_TIP;
    hIcon := Image1.Picture.Icon.Handle;
    StrPCopy(szTip, Application.Title);
  end;

  // 아래 주석으로 표시된 부분은 Tray Icon 으로 만들때 폼을 Hide 시켜서
  // Task Bar 에 나타나지 않도록 만들때 사용하면 된다
  // ShowWindow(Form1.Handle, SW_HIDE);
  Shell_NotifyIcon(NIM_ADD, @MyNotifyStruct); // Tray Icon 으로 등록
  // ShowWindow(Application.Handle, SW_HIDE);
  // ShowWindow(Application.Handle, SW_MINIMIZE); // Show중인 다른 폼들도 감추기 위해
  // ShowWindow(Application.Handle, SW_HIDE);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // Tray Icon 정보를 변경한다
  Shell_NotifyIcon(NIM_Delete, @MyNotifyStruct);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  StrPCopy(MyNotifyStruct.szTip, TimetoStr(Time)); // 현재 시간을 Caption으로...
  // 아이콘을 주기적으로 바꾼다
  if (MyNotifyStruct.hIcon = Image1.Picture.Icon.Handle) then
     MyNotifyStruct.hIcon:= Image2.Picture.Icon.Handle
  else
     MyNotifyStruct.hIcon:= Image1.Picture.Icon.Handle;

  Shell_NotifyIcon(NIM_Modify, @MyNotifyStruct);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  // 프로그램 종료시 Tray Icon을 내린다
  Shell_NotifyIcon(NIM_Delete, @MyNotifyStruct);
end;

end.