码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi 托盘/热键《LceMeaning》

时间:2015-02-11 10:47:41      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

以下代码本人在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.

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

代码结束.

Delphi 托盘/热键《LceMeaning》

标签:

原文地址:http://www.cnblogs.com/LceMeaning/p/4285368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!