Delphi 托盤/熱鍵《LceMeaning》

如下代碼本人在Delphi XE2下編譯經過api

==================================================================ide

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Menus;

type
  TForm1 = class(TForm)
    TrayIcon1: TTrayIcon;     //托盤控件
    pm1: TPopupMenu;          //托盤菜單
    N1: TMenuItem;
    N2: TMenuItem;
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure TrayIcon1DblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    aatom : ATOM;
    procedure hotkey(var msg:TMessage);message WM_HOTKEY;
    //定義全局熱鍵消息事件

    procedure WMsyscommand(var msg : Twmsyscommand);message wm_syscommand;
    //托盤消息定義
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//全局執鍵執行過程
procedure TForm1.hotkey(var msg: TMessage);
begin
  if TWMHotKey(msg).HotKey=aatom then
  begin
    TrayIcon1DblClick(Self);
  end;
end;

//刪除全局熱鍵
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UnregisterHotKey(Handle,aatom);
  GlobalDeleteAtom(aatom);
end;

//建立全局執鍵
procedure TForm1.FormCreate(Sender: TObject);
begin
  if FindAtom('hotKey')=0 then
   begin
     aatom:=GlobalAddAtom('hotKey');
   end;
  RegisterHotKey(Handle,aatom,MOD_ALT,$43);   //ALT + C
end;


procedure TForm1.N1Click(Sender: TObject);
begin
  Form1.Show;
  OpenIcon(Form1.Handle);    //激活窗口
end;

procedure TForm1.N2Click(Sender: TObject);
begin
  TrayIcon1.Visible := False;     //刪除托盤圖標
  Application.Terminate;
end;

procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
  //雙擊托盤圖標顯示/隱藏窗口
  if WindowState = wsMinimized then
    begin
      Form1.Show;
      OpenIcon(Form1.Handle);
    end
  else
    begin
      Form1.Hide;
      WindowState := wsMinimized;
    end;
end;

//窗口縮小到托盤執行代碼
procedure Tform1.WMsyscommand(var msg : Twmsyscommand);
begin
  if msg.CmdType = SC_MINIMIZE then
    Form1.Hide;
  inherited;
end;

end.

==================================================================atom

代碼結束.spa

相關文章
相關標籤/搜索