开机运行就最小化 到托盘中(50分)

  • 主题发起人 主题发起人 mouse_ingrief
  • 开始时间 开始时间
M

mouse_ingrief

Unregistered / Unconfirmed
GUEST, unregistred user!
我想编这样一个程序:开机运行就最小化 到托盘中。<br>最小化到托盘中,我看过一些资料shell_notifyicon函数<br>可以做到,也试过了,但关键是如何让程序启动的时候就到托盘呢?
 
有这样的控件。
 
你把 &nbsp;托盘的那部分代码写到 formshow 里面就行啦!
 
unit TrayIcon;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Menus,shellapi;<br><br>type<br>&nbsp; //----------------------------------------------------------------------<br>&nbsp; PNotifyIconData = ^TNotifyIconDataA;<br>&nbsp; &nbsp; TNotifyIconDataA = record<br>&nbsp; &nbsp; cbSize : DWORD;<br>&nbsp; &nbsp; Wnd : HWND;<br>&nbsp; &nbsp; uID : UINT;<br>&nbsp; &nbsp; uFlags : UINT;<br>&nbsp; &nbsp; uCallbackMessage : UINT;<br>&nbsp; &nbsp; hIcon : HICON;<br>&nbsp; &nbsp; szTip : array [0..63] of AnsiChar;<br>&nbsp; end;<br>&nbsp; //----------------------------------------------------------------------<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; open1: TMenuItem;<br>&nbsp; &nbsp; close1: TMenuItem;<br>&nbsp; &nbsp; N1: TMenuItem;<br>&nbsp; &nbsp; about1: TMenuItem;<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure open1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure close1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormShow(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; //-------------------------------------------------------------<br>&nbsp; &nbsp; IconData: TNotifyIconData;<br>&nbsp; &nbsp; procedure ShowIcon;<br>&nbsp; &nbsp; procedure IconOnClick(var message:TMessage); message WM_USER+1;<br>&nbsp; &nbsp; Procedure WMSysCommand(Var message : TMessage) ; Message WM_SYSCOMMAND ;<br>&nbsp; &nbsp; //-------------------------------------------------------------<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.IconOnClick( var message: Tmessage);<br>var p : TPoint;<br>begin<br>&nbsp;if (message.lParam = WM_LBUTTONDOWN) then<br>&nbsp;begin<br>&nbsp; &nbsp;ShowWindow(Handle, SW_SHOW );<br>&nbsp;end;<br>&nbsp;<br>&nbsp;if (message.lParam = WM_RBUTTONDOWN) then<br>&nbsp;begin<br>&nbsp; &nbsp;GetCursorPos(p);<br>&nbsp; &nbsp;popupmenu1.Popup( p.x ,p.y );<br>&nbsp;end;<br>end;<br><br>Procedure TForm1.WMSysCommand(Var Message : TMessage) ;<br>begin<br><br>&nbsp; if (Message.WParam = SC_MINIMIZE) then<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;ShowIcon;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; &nbsp;Inherited;<br><br>end;<br><br>procedure TForm1.ShowIcon;<br>begin<br>&nbsp; &nbsp; &nbsp; IconData.cbSize := SizeOf( IconData );<br>&nbsp; &nbsp; &nbsp; IconData.Wnd := Handle;<br>&nbsp; &nbsp; &nbsp; IconData.uID := 1;<br>&nbsp; &nbsp; &nbsp; IconData.uFlags := NIF_ICON<br>&nbsp; &nbsp; &nbsp; or NIF_MESSAGE or NIF_TIP;<br>&nbsp; &nbsp; &nbsp; IconData.uCallBackMessage := WM_USER+1;<br>&nbsp; &nbsp; &nbsp; IconData.hIcon := application.Icon.Handle;<br>&nbsp; &nbsp; &nbsp; IconData.szTip := 'LANChat';<br>&nbsp; &nbsp; &nbsp; Shell_NotifyIcon( NIM_ADD, @IconData );<br>&nbsp; &nbsp; &nbsp; ShowWindow(Handle, SW_HIDE);<br>&nbsp; &nbsp; &nbsp; hide;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; &nbsp;Shell_NotifyIcon( NIM_DELETE, @IconData );<br>end;<br><br>procedure TForm1.open1Click(Sender: TObject);<br>begin<br>&nbsp; Form1.Show;<br>end;<br><br>procedure TForm1.close1Click(Sender: TObject);<br>begin<br>&nbsp; Form1.close;<br>end;<br><br>procedure TForm1.FormShow(Sender: TObject);<br>begin<br><br>&nbsp; showwindow(application.handle,sw_hide);<br><br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; showicon;<br>end;<br><br>end.<br>
 
我也想问!<br>上面的代码好像不行哦,我试了下,<br>关键是运行时不显示窗体。<br>windows XP+Delphi6.<br>
 
如何办?还有就是 运行时不在任务栏显示。<br>和上一条合起来才算搞定!
 
(这人怎么拖拖拉拉的……bt!)<br>再加一句,就是像金山词霸,Flashget那样。
 
我有办法<br><br>你将你的程序的路径放到注册表的Run中<br>然后,在你的程序的启动后马上给自己发最小化消息
 
我刚用过,你看看下面的代码:<br>unit Mainfrm;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls,Registry,ShellAPI, Menus; //加上ShellApi<br>const<br>&nbsp; MYTRAY_MESSAGE=WM_USER+100; //托盘图标返回消息<br>type<br>&nbsp; Tmainform = class(TForm)<br>&nbsp; &nbsp; TrayPopMenu: TPopupMenu;<br>&nbsp; &nbsp; POPSet: TMenuItem;<br>&nbsp; &nbsp; POPEXIT: TMenuItem;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure POPEXITClick(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure FormPaint(Sender: TObject);<br>&nbsp; &nbsp; procedure POPSetClick(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; TrayIconData : TNotifyIconData; //定义托盘图标结构<br>&nbsp; &nbsp; //托盘消息处理事件<br>&nbsp; &nbsp; procedure MyTrayMessage(var message:TMessage);message MyTRAY_MESSAGE;<br><br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; tag:boolean;<br>&nbsp; end;<br><br>var<br>&nbsp; mainform: Tmainform;<br><br>implementation<br><br>uses Setfrm, UData;<br><br>{$R *.DFM}<br><br>//---------------------------------------------------------------------------<br>//---------使系统启动时,程序自动运行<br>//---------------------------------------------------------------------------<br><br>procedure Tmainform.FormCreate(Sender: TObject);<br>Var //使系统启动时,程序自动运行<br>&nbsp; RegF:TRegistry;<br>begin<br>&nbsp; tag:=true;<br>&nbsp; RegF:=TRegistry.Create;<br>&nbsp; RegF.RootKey:=HKEY_LOCAL_MACHINE;<br>&nbsp; Try<br>&nbsp; &nbsp; RegF.OpenKey('SOFTWARE/Microsoft/Windows/CurrentVersion/Run',true);<br>&nbsp; &nbsp; //这一句判断是否为第一次运行<br>&nbsp; &nbsp; if not Regf.valueExists('GDHclient') then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; tag:=false;<br>&nbsp; &nbsp; &nbsp; RegF.WriteString('GDHclient','"' + ParamStr(0) + '"');<br>&nbsp; &nbsp; &nbsp; Application.MessageBox('谢谢您使用本程序,第一次使用请您先进行初始设置!','提示信息',MB_OK+MB_ICONWARNING);<br>&nbsp; &nbsp; end;<br>&nbsp; except<br>&nbsp; End;<br>&nbsp; RegF.CloseKey;<br>&nbsp; RegF.Free;<br>&nbsp; //建立托盘图标<br>&nbsp; TrayIconData.cbSize := sizeof(TrayIconData); //TrayIconData变量的字节数<br>&nbsp; TrayIconData.Wnd := Handle; &nbsp; //主窗口句柄<br>&nbsp; TrayIconData.uID := 1; &nbsp; &nbsp; &nbsp; &nbsp;//内部标识,可设为任意数<br>&nbsp; TrayIconData.hIcon := Icon.Handle;<br>&nbsp; {要加入的图标句柄,可任意指定,此处为在Tform中Icon属性中指定的图标<br>&nbsp; TrayIconData.hIcon := Application.Icon.Handle;<br>&nbsp; 此句使用了程序的图标,和上句程序有区别,下句亦可,但推荐使用上句,这样图标加<br>&nbsp; 入到托盘中时图标不会走样。}<br>&nbsp; TrayIconData.hIcon := LoadIcon(0,IDI_APPLICATION);<br>&nbsp; TrayIconData.szTip := '正在运行-//你的程序';//图标的提示信息,即黄色的Hint<br>&nbsp; TrayIconData.uCallbackMessage := MYTRAY_MESSAGE;<br>&nbsp; TrayIconData.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; //指明哪些字段有效<br>&nbsp; {将程序的窗口样式设为TOOL窗口,可避免在任务条上出现}<br>&nbsp; SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);<br>&nbsp; Shell_NotifyIcon(NIM_ADD, @TrayIconData); //向托盘中添加图标<br>end;<br><br>//---------------------------------------------------------------------------<br>//------------退出程序<br><br>procedure Tmainform.POPEXITClick(Sender: TObject);<br>begin//弹出菜单事件<br>&nbsp; close;<br>end;<br><br>//---------------------------------------------------------------------------<br>//-------------当关闭程序时<br><br>procedure Tmainform.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; //TrayIconData.cbSize := sizeof(TrayIconData); &nbsp; //TrayIconData变量的字节数<br>&nbsp; //TrayIconData.uID := 1; &nbsp; &nbsp; &nbsp; &nbsp;//内部标识,与加入小图标时的数一致<br>&nbsp; //TrayIconData.Wnd := Handle; &nbsp; //主窗口句柄<br>&nbsp; //TrayIconData.uFlags := 0;<br>&nbsp; Shell_NotifyIcon(NIM_DELETE, @TrayIconData); &nbsp; //去掉小图标<br>end;<br><br>//---------------------------------------------------------------------------<br>//------------我也不知这是什么?<br>procedure Tmainform.FormPaint(Sender: TObject);<br>begin<br>&nbsp; Hide;<br>end;<br><br>//---------------------------------------------------------------------------<br>//-------消息处理事件<br>//---------------------------------------------------------------------------<br><br>procedure TMainForm.MyTrayMessage(var message: Tmessage);<br>var //托盘消息处理事件<br>&nbsp; CursorPos : TPoint;<br>begin<br>&nbsp; //在这里处理用户点击托盘图标事件,可以根据WM_MOUSEMOVE消息的不同情况产生不同的回应,例如区别对待单击和双击等等<br>&nbsp; case message.lParam of<br>&nbsp; WM_RBUTTONDOWN :<br>&nbsp; begin<br>&nbsp; &nbsp; GetCursorPos(CursorPos);<br>&nbsp; &nbsp; SetForegroundWindow(handle);<br>&nbsp; &nbsp; //此句作用是当程序失去焦点时,弹出菜单也随之消失。<br>&nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp; //取得光标当前位置<br>&nbsp; &nbsp; TrayPopMenu.Popup( CursorPos.x ,CursorPos.y );<br>&nbsp; &nbsp; PostMessage(Application.MainForm.Handle, WM_NULL, 0, 0);<br>&nbsp; end;<br>&nbsp; end;<br><br>end; <br><br>//---------------------------------------------------------------------------<br>//-------创建并打开设置窗口<br>//---------------------------------------------------------------------------<br><br>procedure Tmainform.POPSetClick(Sender: TObject);<br>begin //创建并打开设置窗口,以便进行操作<br>&nbsp; setform:=Tsetform.create(application);<br>&nbsp; try<br>&nbsp; &nbsp; setform.showmodal;<br>&nbsp; finally<br>&nbsp; &nbsp; setform.free;<br>&nbsp; end; &nbsp; &nbsp; <br>end;<br><br>end.<br>另外,在工程文件中加入下面的:<br>program SysGDH;<br><br>uses<br>&nbsp; Forms,<br>&nbsp; Mainfrm in 'Mainfrm.pas' {mainform},<br>&nbsp; Setfrm in 'Setfrm.pas' {Setform},<br>&nbsp; UData in 'UData.pas' {Moduleform: TDataModule};<br><br>{$R *.RES}<br><br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; Application.CreateForm(Tmainform, mainform);<br>&nbsp; Application.ShowMainForm := False; //使主窗体在面子上不闪动加上这一句<br>&nbsp; Application.Run;<br><br>end.
 
多人接受答案了。
 
后退
顶部