Delphi 托盘/热键《LceMeaning》

以下代码本人在Delphi XE2下编译通过

==================================================================

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.

==================================================================

代码结束.

转载于:https://www.cnblogs.com/LceMeaning/p/4285368.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值