一个小程序,用来显示一个记事本的,支持热键(Ctrl+Alt+A),能够实现右侧菜单栏显示图标。
同时,可以使用第三方控件,不过一般没有必要。
unit Unit_Top;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ExtCtrls,Clipbrd, Menus,ShellApi;
const WM_TrayIcon=WM_User+1;
type
TFrm_Top = class(TForm)
mem_top: TMemo;
pnl_top: TPanel;
bbtn_Copy: TBitBtn;
pm_top: TPopupMenu;
pmi_Exit: TMenuItem;
mmi_Restore: TMenuItem;
procedure mem_topKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormCreate(Sender: TObject);
procedure pmi_ExitClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure mmi_RestoreClick(Sender: TObject);
private
{ Private declarations }
procedure OnNotifyIcon(var Msg:Tmessage);message WM_TrayIcon;
procedure WMHotkeyHandle(var Msg:TMessage);message WM_HOTKEY; //响应敏感键按键消息
procedure WMSysCommand(var Msg:TMessage);message WM_SysCommand;
public
{ Public declarations }
end;
var
Frm_Top: TFrm_Top;
implementation
{$R *.DFM}
var iData: TNotifyIconData;
HotkeyId :Integer;
procedure TFrm_Top.WMSysCommand(var Msg:TMessage);
begin
if Msg.WParam=SC_MINIMIZE then
begin
Application.Minimize;
Frm_Top.Hide;
end;
inherited;
end;
procedure TFrm_Top.WMHotkeyHandle(var Msg:TMessage);
begin
if (Msg.LParamHi=$41) and (msg.lparamLo=MOD_CONTROL or mod_Alt) then
begin
Msg.Result:=1; //该消息已经处理
mmi_RestoreClick(Application);
end;
end;
procedure TFrm_Top.mem_topKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (ssCtrl in Shift) and (Key=13) then
begin
if mem_top.SelText='' then
Clipboard.AsText:=mem_top.Text
else
Clipboard.AsText:=mem_top.SelText;
Key:=0;
end;
end;
procedure TFrm_Top.FormCreate(Sender: TObject);
begin
iData.cbSize:=Sizeof(iData);
iData.Wnd:=Frm_Top.Handle;
iData.uID:=0;
iData.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
iData.uCallbackMessage:=WM_TrayIcon;
iData.hIcon:=Application.Icon.Handle;
iData.szTip:='asd';
if Shell_NotifyIcon(NIM_ADD, @iData)=False then ShowMessage('NO');
HotkeyId:=GlobalAddAtom(Pchar('UserDefineHotKey'))-$C000; //减去$C000是为了保证取值范围的限制
RegisterHotkey(Handle,Hotkeyid,MOD_CONTROL or MOD_Alt, $41);//敏感键为ctrl+Alt+A
end;
procedure TFrm_Top.OnNotifyIcon(var Msg:Tmessage);
var Pos:Tpoint;
begin
case Msg.LParam of
WM_LBUTTONDBLCLK:
mmi_RestoreClick(Application);
WM_RBUTTONDOWN:
begin
GetCursorPos(Pos);
pm_top.Popup (Pos.x,Pos.y);
end;
end;
end;
procedure TFrm_Top.pmi_ExitClick(Sender: TObject);
begin
if Shell_NotifyIcon(NIM_DELETE, @iData)=False then ShowMessage('NO');
UnRegisterhotkey(Handle,HotkeyId);
DeleteAtom(HotKeyID);
Application.Terminate;
end;
procedure TFrm_Top.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
// Application.Minimize;
// ShowWindow(Handle,SW_HIde);
Frm_Top.Hide;
CanClose:=False;
end;
procedure TFrm_Top.mmi_RestoreClick(Sender: TObject);
begin
// ShowWindow(Handle,SW_SHOW);
Frm_Top.Show;
// Application.Restore;
SetFocus;
end;
end.