如何将程序最小化至系统托盘并可通过热键击活? 用控件很好实现rxlib里有;也有专门的托盘控件www.inprises.com 有 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先声明以私有过程private procedure wmhotkey(var msg:tmessage);message wm_hotkey; //以下程序是windows在任何情况下都相应热键procedure tform1.formcreate(sender.....); begin registerhotkey(handle,1002,0,vk_f10);//注册F10为热键 registerhotkey(handle,1003,mod_control,65);//注册ctrl+a registerhotkey(handle,1004,mod_control+mod_alt,ord('a'));//注册ctrl+alt+a registerhotkey(handle,1001,mod_win,65);//注册win+a,win 就是ctrl和alt中间的键 endprocedure tform1.wmhotkey(var msg:tmessage); begin if msg.wparam=1001 then //自己写事件procedure tform1.formdestroy(sender:tobject); begin unregisterhotkey(handle,1001);//释放注册的热键 end; 给你个例子(建立托盘图标):unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Menus,shellapi, AppEvnts;const ICON_ID = 1; // 图标在本应用程序中的编号 MI_ICONEVENT = WM_USER + 1; // 图标上的鼠标事件type TForm1 = class(TForm) Pop1: TPopupMenu; status1: TMenuItem; n1: TMenuItem; close1: TMenuItem; ApplicationEvents1: TApplicationEvents; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure close1Click(Sender: TObject); procedure status1Click(Sender: TObject); procedure n1Click(Sender: TObject); procedure ApplicationEvents1Minimize(Sender: TObject); private { Private declarations } normalicon, disabledicon: TIcon; //需放在状态指示区的图标 status: Boolean; procedure InstallIcon; //在状态指示区上安装图标 procedure ChangeIcon(status:Boolean);//根据参数,显示不同的图标 procedure UnInstallIcon; //卸载图标 procedure IconOnClick(var message: TMessage); message MI_ICONEVENT; //响应图标上的鼠标事件 public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);beginInstallIcon;ChangeIcon(False);Application.Minimize;end;procedure TForm1.InstallIcon;var IconData: TNotifyIconData;beginnormalicon := TIcon.Create;disabledicon := TIcon.Create;normalicon.LoadFromFile( 'normal.ico' );disabledicon.LoadFromFile( 'disable.ico' );IconData.cbSize := SizeOf( IconData );IconData.Wnd := Handle;IconData.uID := ICON_ID;IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;IconData.uCallBackMessage := MI_ICONEVENT;IconData.hIcon := normalicon.Handle;IconData.szTip := '我可以在状态指示区上加图标了!';Shell_NotifyIcon( NIM_ADD, @IconData );end;procedure TForm1.ChangeIcon( status: boolean );var IconData: TNotifyIconData;beginIconData.cbSize := SizeOf(IconData);IconData.wnd := Handle;IconData.uID := ICON_ID;{if IconData.fconectado then} IconData.hIcon :=normalicon.Handle;{ else IconData.hIcon :=disabledIcon.Handle;}IconData.uFlags := NIF_ICON;Shell_NotifyIcon(NIM_MODIFY, @IconData);end;procedure TForm1.UnInstallIcon;var IconData: TNotifyIconData;beginIconData.cbSize := SizeOf( IconData );IconData.Wnd := Handle;IconData.uID := ICON_ID;Shell_NotifyIcon( NIM_DELETE, @IconData );end;procedure TForm1.IconOnClick( var message: Tmessage);var p : TPoint;beginif (message.lParam = WM_LBUTTONDOWN) then ShowWindow( Application.Handle, SW_SHOW );if (message.lParam = WM_RBUTTONDOWN) then begin GetCursorPos( p ); pop1.Popup( p.x ,p.y ); ChangeIcon( status ); end;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);beginUnInstallIcon;normalicon.Free;disabledicon.Free;end;procedure TForm1.close1Click(Sender: TObject);beginClose;end;procedure TForm1.status1Click(Sender: TObject);beginstatus := not status; if status then status1.Caption := 'Enable' else status1.Caption := 'Disable';end;procedure TForm1.n1Click(Sender: TObject);beginApplication.Restore;Application.BringToFront;end;procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);beginShowWindow(Application.Handle, SW_HIDE);end;end. unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Menus, ShellAPI, RXShell, StdCtrls;const CM_TRAYICON = CM_BASE + 100; //定义发送的休息type TForm1 = class(TForm) PopupMenu1: TPopupMenu; RestoreItem: TMenuItem; ExitItem: TMenuItem; ShowBtn: TButton; N1: TMenuItem; HelloItem: TMenuItem; procedure RestoreItemClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); procedure ExitItemClick(Sender: TObject); procedure HelloItemClick(Sender: TObject); private procedure MyMinimize(Sender: TObject); procedure CMTrayIcon(var Message: TMessage); message CM_TRAYICON; { Private declarations } public { Public declarations } end;var Form1: TForm1; IconData: TNotifyIconData; //图标implementation{$R *.DFM}//处理 CM_TRAYICON 的事件procedure TForm1.CMTrayIcon(var Message: TMessage);var P: TPoint;begin try with Message do //如果单击鼠标左键或右键,弹出菜单 if (lParam = WM_LBUTTONUP)or(lParam = WM_RBUTTONUP) then begin GetCursorPos(P); PopupMenu1.Popup(P.X, P.Y); end; except end;end;//系统最小化时调用的事件procedure TForm1.MyMinimize(Sender: TObject);begin Hide;end;procedure TForm1.RestoreItemClick(Sender: TObject);begin Application.MainForm.Show; Application.Restore; //恢复窗口 // 必须先 MainForm.Show,然后 Restore , // 要不再次 Hide Form 时 将会失败!end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begin //删除图标 Shell_NotifyIcon(NIM_DELETE, @IconData); IConData.cbSize := 0; Application.OnMinimize := nil;end;procedure TForm1.FormCreate(Sender: TObject);begin Application.OnMinimize := MyMinimize; //调用最小化过程 with IconData do begin cbSize := SizeOf(IconData); //结构大小 Wnd := Form1.Handle; //接收CM_TRAYICON消息的窗口HWN uID := $DEDB; //TrayIcon的编号 uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP; hIcon := Forms.Application.Icon.Handle; //指定ICON uCallbackMessage := CM_TRAYICON; //要发送的消息 StrCopy(szTip, PChar(Caption)); end; Shell_NotifyIcon(NIM_Add, @IconData); //添加图标 // ShellAPI.pasend;procedure TForm1.ExitItemClick(Sender: TObject);begin Close;end;procedure TForm1.HelloItemClick(Sender: TObject);begin ShowMessage('Hello! 你好吗?');end;end. 老问题了……建立托盘图标——unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, AppEvnts, ShellAPI, Menus;const my_message=wm_user+24; //定义消息type TForm1 = class(TForm) ApplicationEvents1: TApplicationEvents; PopupMenu1: TPopupMenu; AAA1: TMenuItem; BBB1: TMenuItem; N1: TMenuItem; procedure InstallIcon; procedure OnMymssg(var msg : Tmessage);message my_message; procedure ApplicationEvents1Minimize(Sender: TObject); procedure N1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; nid:Tnotifyicondataa;implementation{$R *.DFM}procedure Tform1.InstallIcon;begin nid.cbsize:=sizeof(nid); nid.wnd:=handle; nid.uid:=0; nid.hIcon:=Application.icon.handle; nid.sztip:='这里是你的提示信息!'; nid.uCallBackMessage:=my_message; nid.uflags:=nif_icon or nif_tip or nif_message; shell_notifyIcon(NIM_ADD,@nid);end;procedure Tform1.OnMymssg(var msg : Tmessage);var mou:Tmouse;begin mou:=Tmouse.Create; try if msg.LParam=WM_LBUTTONUP then begin form1.Visible:=true; setforegroundwindow(form1.handle); OpenIcon(Application.Handle); shell_notifyicon(NIM_DELETE,@nid); end else if msg.LParam=WM_RBUTTONUP then PopupMenu1.Popup(mou.CursorPos.x,mou.cursorpos.y); finally mou.Free; end;end;procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);begin installicon; form1.Hide;end;procedure TForm1.N1Click(Sender: TObject);begin Close;end;end.注册系统热键——(要在USES里加上registry)//注册热键(ctrl、alt和A)RegisterHotKey(handle,0,MOD_ALT or MOD_CONTROL,Ord('A'));//当按下定义好的热键时,窗口弹出,并成为当前激活窗口procedure TForm1.WMHOTKEY(var Message:TMessage);begin Form1.Visible:=True; Setforegroundwindow(form1.handle); OpenIcon(Application.Handle); Shell_notifyicon(NIM_DELETE,@nid);end;够详细了吧?给分吧 控件的安装 数据库中如何计算平均值 文件内容读取 如何将格式yyyy.mm.dd转化为yyyymmdd 哪位方便可以给一个Delphi中的“类型库编辑器(TypImp.exe)”? 小妹又有一个问题想请教大哥们 请教使用 delphi 的朋友一个问题 ClientDataSet1的Params不支持中文如何解决,急!!! 请大家捉虫子!!! 一个c/s数据库开发问题 新手简单问题,先来先给分! 关于窗体的创建和释放的问题
private
procedure wmhotkey(var msg:tmessage);message wm_hotkey; //以下程序是windows在任何情况下都相应热键procedure tform1.formcreate(sender.....);
begin
registerhotkey(handle,1002,0,vk_f10);//注册F10为热键
registerhotkey(handle,1003,mod_control,65);//注册ctrl+a
registerhotkey(handle,1004,mod_control+mod_alt,ord('a'));//注册ctrl+alt+a
registerhotkey(handle,1001,mod_win,65);//注册win+a,win 就是ctrl和alt中间的键
endprocedure tform1.wmhotkey(var msg:tmessage);
begin
if msg.wparam=1001 then
//自己写事件procedure tform1.formdestroy(sender:tobject);
begin
unregisterhotkey(handle,1001);//释放注册的热键
end;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus,shellapi, AppEvnts;
const
ICON_ID = 1; // 图标在本应用程序中的编号
MI_ICONEVENT = WM_USER + 1; // 图标上的鼠标事件type
TForm1 = class(TForm)
Pop1: TPopupMenu;
status1: TMenuItem;
n1: TMenuItem;
close1: TMenuItem;
ApplicationEvents1: TApplicationEvents;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure close1Click(Sender: TObject);
procedure status1Click(Sender: TObject);
procedure n1Click(Sender: TObject);
procedure ApplicationEvents1Minimize(Sender: TObject);
private
{ Private declarations }
normalicon, disabledicon: TIcon; //需放在状态指示区的图标
status: Boolean;
procedure InstallIcon; //在状态指示区上安装图标
procedure ChangeIcon(status:Boolean);//根据参数,显示不同的图标
procedure UnInstallIcon; //卸载图标
procedure IconOnClick(var message: TMessage); message MI_ICONEVENT; //响应图标上的鼠标事件
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin
InstallIcon;
ChangeIcon(False);
Application.Minimize;
end;procedure TForm1.InstallIcon;
var IconData: TNotifyIconData;
begin
normalicon := TIcon.Create;
disabledicon := TIcon.Create;
normalicon.LoadFromFile( 'normal.ico' );
disabledicon.LoadFromFile( 'disable.ico' );
IconData.cbSize := SizeOf( IconData );
IconData.Wnd := Handle;
IconData.uID := ICON_ID;
IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
IconData.uCallBackMessage := MI_ICONEVENT;
IconData.hIcon := normalicon.Handle;
IconData.szTip := '我可以在状态指示区上加图标了!';
Shell_NotifyIcon( NIM_ADD, @IconData );
end;procedure TForm1.ChangeIcon( status: boolean );
var IconData: TNotifyIconData;
begin
IconData.cbSize := SizeOf(IconData);
IconData.wnd := Handle;
IconData.uID := ICON_ID;
{if IconData.fconectado then}
IconData.hIcon :=normalicon.Handle;
{ else
IconData.hIcon :=disabledIcon.Handle;}
IconData.uFlags := NIF_ICON;
Shell_NotifyIcon(NIM_MODIFY, @IconData);
end;procedure TForm1.UnInstallIcon;
var IconData: TNotifyIconData;
begin
IconData.cbSize := SizeOf( IconData );
IconData.Wnd := Handle;
IconData.uID := ICON_ID;
Shell_NotifyIcon( NIM_DELETE, @IconData );
end;procedure TForm1.IconOnClick( var message: Tmessage);
var p : TPoint;
begin
if (message.lParam = WM_LBUTTONDOWN) then
ShowWindow( Application.Handle, SW_SHOW );
if (message.lParam = WM_RBUTTONDOWN) then
begin
GetCursorPos( p );
pop1.Popup( p.x ,p.y );
ChangeIcon( status );
end;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnInstallIcon;
normalicon.Free;
disabledicon.Free;
end;procedure TForm1.close1Click(Sender: TObject);
begin
Close;
end;procedure TForm1.status1Click(Sender: TObject);
begin
status := not status;
if status then
status1.Caption := 'Enable'
else
status1.Caption := 'Disable';
end;procedure TForm1.n1Click(Sender: TObject);
begin
Application.Restore;
Application.BringToFront;
end;procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
ShowWindow(Application.Handle, SW_HIDE);
end;end.
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, ShellAPI, RXShell, StdCtrls;const
CM_TRAYICON = CM_BASE + 100; //定义发送的休息type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
RestoreItem: TMenuItem;
ExitItem: TMenuItem;
ShowBtn: TButton;
N1: TMenuItem;
HelloItem: TMenuItem;
procedure RestoreItemClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure ExitItemClick(Sender: TObject);
procedure HelloItemClick(Sender: TObject);
private
procedure MyMinimize(Sender: TObject);
procedure CMTrayIcon(var Message: TMessage); message CM_TRAYICON;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
IconData: TNotifyIconData; //图标implementation{$R *.DFM}//处理 CM_TRAYICON 的事件
procedure TForm1.CMTrayIcon(var Message: TMessage);
var
P: TPoint;
begin
try
with Message do
//如果单击鼠标左键或右键,弹出菜单
if (lParam = WM_LBUTTONUP)or(lParam = WM_RBUTTONUP) then
begin
GetCursorPos(P);
PopupMenu1.Popup(P.X, P.Y);
end;
except end;
end;//系统最小化时调用的事件
procedure TForm1.MyMinimize(Sender: TObject);
begin
Hide;
end;procedure TForm1.RestoreItemClick(Sender: TObject);
begin
Application.MainForm.Show; Application.Restore; //恢复窗口
// 必须先 MainForm.Show,然后 Restore ,
// 要不再次 Hide Form 时 将会失败!
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//删除图标
Shell_NotifyIcon(NIM_DELETE, @IconData);
IConData.cbSize := 0;
Application.OnMinimize := nil;
end;procedure TForm1.FormCreate(Sender: TObject);
begin Application.OnMinimize := MyMinimize; //调用最小化过程 with IconData do
begin
cbSize := SizeOf(IconData); //结构大小
Wnd := Form1.Handle; //接收CM_TRAYICON消息的窗口HWN
uID := $DEDB; //TrayIcon的编号
uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
hIcon := Forms.Application.Icon.Handle; //指定ICON
uCallbackMessage := CM_TRAYICON; //要发送的消息
StrCopy(szTip, PChar(Caption));
end;
Shell_NotifyIcon(NIM_Add, @IconData); //添加图标
// ShellAPI.pas
end;procedure TForm1.ExitItemClick(Sender: TObject);
begin
Close;
end;procedure TForm1.HelloItemClick(Sender: TObject);
begin
ShowMessage('Hello! 你好吗?');
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
AppEvnts, ShellAPI, Menus;const
my_message=wm_user+24; //定义消息type
TForm1 = class(TForm)
ApplicationEvents1: TApplicationEvents;
PopupMenu1: TPopupMenu;
AAA1: TMenuItem;
BBB1: TMenuItem;
N1: TMenuItem;
procedure InstallIcon;
procedure OnMymssg(var msg : Tmessage);message my_message;
procedure ApplicationEvents1Minimize(Sender: TObject);
procedure N1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
nid:Tnotifyicondataa;
implementation{$R *.DFM}procedure Tform1.InstallIcon;
begin
nid.cbsize:=sizeof(nid);
nid.wnd:=handle;
nid.uid:=0;
nid.hIcon:=Application.icon.handle;
nid.sztip:='这里是你的提示信息!';
nid.uCallBackMessage:=my_message;
nid.uflags:=nif_icon or nif_tip or nif_message;
shell_notifyIcon(NIM_ADD,@nid);
end;procedure Tform1.OnMymssg(var msg : Tmessage);
var
mou:Tmouse;
begin
mou:=Tmouse.Create;
try
if msg.LParam=WM_LBUTTONUP then
begin
form1.Visible:=true;
setforegroundwindow(form1.handle);
OpenIcon(Application.Handle);
shell_notifyicon(NIM_DELETE,@nid);
end
else if msg.LParam=WM_RBUTTONUP then
PopupMenu1.Popup(mou.CursorPos.x,mou.cursorpos.y);
finally
mou.Free;
end;
end;procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
installicon;
form1.Hide;
end;procedure TForm1.N1Click(Sender: TObject);
begin
Close;
end;end.
注册系统热键——(要在USES里加上registry)//注册热键(ctrl、alt和A)
RegisterHotKey(handle,0,MOD_ALT or MOD_CONTROL,Ord('A'));//当按下定义好的热键时,窗口弹出,并成为当前激活窗口
procedure TForm1.WMHOTKEY(var Message:TMessage);
begin
Form1.Visible:=True;
Setforegroundwindow(form1.handle);
OpenIcon(Application.Handle);
Shell_notifyicon(NIM_DELETE,@nid);
end;
够详细了吧?给分吧