程序图标的问题(50分)

  • 主题发起人 主题发起人 飞沙
  • 开始时间 开始时间

飞沙

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么样使程序运行时,程序图标只出现在右下角,或者是不出现,更或者进程里也看不到,<br>最好开机就自动运行
 
第三控件cooltrayicon<br>自动运行写注册表中的run
 
我要代码
 
unit TrayIcon;<br><br>interface<br><br>uses Windows, SysUtils, Messages, ShellAPI, Classes, Graphics, Forms, Menus,<br>&nbsp; StdCtrls, ExtCtrls;<br><br>type<br>&nbsp; ENotifyIconError = class(Exception);<br><br>&nbsp; TTrayNotifyIcon = class(TComponent)<br>&nbsp; private<br>&nbsp; &nbsp; FDefaultIcon: THandle;<br>&nbsp; &nbsp; FIcon: TIcon;<br>&nbsp; &nbsp; FHideTask: Boolean;<br>&nbsp; &nbsp; FHint: string;<br>&nbsp; &nbsp; FIconVisible: Boolean;<br>&nbsp; &nbsp; FPopupMenu: TPopupMenu;<br>&nbsp; &nbsp; FOnClick: TNotifyEvent;<br>&nbsp; &nbsp; FOnDblClick: TNotifyEvent;<br>&nbsp; &nbsp; FNoShowClick: Boolean;<br>&nbsp; &nbsp; FTimer: TTimer;<br>&nbsp; &nbsp; Tnd: TNotifyIconData;<br>&nbsp; &nbsp; procedure SetIcon(Value: TIcon);<br>&nbsp; &nbsp; procedure SetHideTask(Value: Boolean);<br>&nbsp; &nbsp; procedure SetHint(Value: string);<br>&nbsp; &nbsp; procedure SetIconVisible(Value: Boolean);<br>&nbsp; &nbsp; procedure SetPopupMenu(Value: TPopupMenu);<br>&nbsp; &nbsp; procedure SendTrayMessage(Msg: DWORD; Flags: UINT);<br>&nbsp; &nbsp; function ActiveIconHandle: THandle;<br>&nbsp; &nbsp; procedure OnButtonTimer(Sender: TObject);<br>&nbsp; protected<br>&nbsp; &nbsp; procedure Loaded; override;<br>&nbsp; &nbsp; procedure LoadDefaultIcon; virtual;<br>&nbsp; &nbsp; procedure Notification(AComponent: TComponent;<br>&nbsp; &nbsp; &nbsp; Operation: TOperation); override;<br>&nbsp; public<br>&nbsp; &nbsp; constructor Create(AOwner: TComponent); override;<br>&nbsp; &nbsp; destructor Destroy; override;<br>&nbsp; published<br>&nbsp; &nbsp; property Icon: TIcon read FIcon write SetIcon;<br>&nbsp; &nbsp; property HideTask: Boolean read FHideTask write SetHideTask default False;<br>&nbsp; &nbsp; property Hint: String read FHint write SetHint;<br>&nbsp; &nbsp; property IconVisible: Boolean read FIconVisible write SetIconVisible default False;<br>&nbsp; &nbsp; property PopupMenu: TPopupMenu read FPopupMenu write SetPopupMenu;<br>&nbsp; &nbsp; property OnClick: TNotifyEvent read FOnClick write FOnClick;<br>&nbsp; &nbsp; property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;<br>&nbsp; end;<br><br>procedure Register;<br>implementation<br><br>{ TIconManager }<br>{ 处理"托盘消息" }<br>type<br>&nbsp; TIconManager = class<br>&nbsp; private<br>&nbsp; &nbsp; FHWindow: HWnd;<br>&nbsp; &nbsp; procedure TrayWndProc(var Message: TMessage);<br>&nbsp; public<br>&nbsp; &nbsp; constructor Create;<br>&nbsp; &nbsp; destructor Destroy; override;<br>&nbsp; &nbsp; property HWindow: HWnd read FHWindow write FHWindow;<br>&nbsp; end;<br><br>var<br>&nbsp; IconMgr: TIconManager;<br>&nbsp; DDGM_TRAYICON: Cardinal;<br><br>constructor TIconManager.Create;<br>begin<br>&nbsp; FHWindow := Classes.AllocateHWnd(TrayWndProc);<br>end;<br><br>destructor TIconManager.Destroy;<br>begin<br>&nbsp; if FHWindow &lt;&gt; 0 then Classes.DeallocateHWnd(FHWindow);<br>&nbsp; inherited Destroy;<br>end;<br><br>procedure TIconManager.TrayWndProc(var Message: TMessage);<br>{ 用户自定义消息,用来处理托盘图标与组件之间的消息. }<br>var<br>&nbsp; Pt: TPoint;<br>&nbsp; TheIcon: TTrayNotifyIcon;<br>begin<br>&nbsp; with Message do<br>&nbsp; begin<br>&nbsp; &nbsp; if (Msg = DDGM_TRAYICON) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; TheIcon := TTrayNotifyIcon(WParam);<br>&nbsp; &nbsp; &nbsp; case lParam of<br>&nbsp; &nbsp; &nbsp; &nbsp; WM_LBUTTONDOWN: TheIcon.FTimer.Enabled := True;<br>&nbsp; &nbsp; &nbsp; &nbsp; WM_LBUTTONDBLCLK:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TheIcon.FNoShowClick := True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Assigned(TheIcon.FOnDblClick) then TheIcon.FOnDblClick(Self);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; WM_RBUTTONDOWN:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Assigned(TheIcon.FPopupMenu) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetForegroundWindow(IconMgr.HWindow);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCursorPos(Pt);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TheIcon.FPopupMenu.Popup(Pt.X, Pt.Y);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //TheIcon.FPopupMenu.Popup(LParamLo,LParamHi);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PostMessage(IconMgr.HWindow, WM_USER, 0, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Result := DefWindowProc(FHWindow, Msg, wParam, lParam);<br>&nbsp; end;<br>end;<br><br>{ TTrayNotifyIcon }<br><br>constructor TTrayNotifyIcon.Create(AOwner: TComponent);<br>begin<br>&nbsp; inherited Create(AOwner);<br>&nbsp; FIcon := TIcon.Create;<br>&nbsp; FTimer := TTimer.Create(Self);<br>&nbsp; with FTimer do<br>&nbsp; begin<br>&nbsp; &nbsp; Enabled := False;<br>&nbsp; &nbsp; Interval := GetDoubleClickTime;<br>&nbsp; &nbsp; OnTimer := OnButtonTimer;<br>&nbsp; end;<br>&nbsp; LoadDefaultIcon;<br>end;<br><br>destructor TTrayNotifyIcon.Destroy;<br>begin<br>&nbsp; if FIconVisible then SetIconVisible(False);<br>&nbsp; FIcon.Free;<br>&nbsp; FTimer.Free;<br>&nbsp; inherited Destroy;<br>end;<br><br>function TTrayNotifyIcon.ActiveIconHandle: THandle;<br>begin<br>&nbsp; if (FIcon.Handle &lt;&gt; 0) then<br>&nbsp; &nbsp; Result := FIcon.Handle<br>&nbsp; else<br>&nbsp; &nbsp; Result := FDefaultIcon;<br>end;<br><br>procedure TTrayNotifyIcon.LoadDefaultIcon;<br>begin<br>&nbsp; FDefaultIcon := LoadIcon(0, IDI_WINLOGO);<br>end;<br><br>procedure TTrayNotifyIcon.Loaded;<br>begin<br>&nbsp; inherited Loaded;<br>&nbsp; if FIconVisible then<br>&nbsp; &nbsp; SendTrayMessage(NIM_ADD, NIF_MESSAGE or NIF_ICON or NIF_TIP);<br>end;<br><br>procedure TTrayNotifyIcon.Notification(AComponent: TComponent;<br>&nbsp; Operation: TOperation);<br>begin<br>&nbsp; inherited Notification(AComponent, Operation);<br>&nbsp; if (Operation = opRemove) and (AComponent = PopupMenu) then<br>&nbsp; &nbsp; PopupMenu := nil;<br>end;<br><br>procedure TTrayNotifyIcon.OnButtonTimer(Sender: TObject);<br>begin<br>&nbsp; FTimer.Enabled := False;<br>&nbsp; if (not FNoShowClick) and Assigned(FOnClick) then<br>&nbsp; &nbsp; FOnClick(Self);<br>&nbsp; FNoShowClick := False;<br>end;<br><br>procedure TTrayNotifyIcon.SendTrayMessage(Msg: DWORD; Flags: UINT);<br>begin<br>&nbsp; with Tnd do<br>&nbsp; begin<br>&nbsp; &nbsp; cbSize := SizeOf(Tnd);<br>&nbsp; &nbsp; StrPLCopy(szTip, PChar(FHint), SizeOf(szTip));<br>&nbsp; &nbsp; uFlags := Flags;<br>&nbsp; &nbsp; uID := UINT(Self);<br>&nbsp; &nbsp; Wnd := IconMgr.HWindow;<br>&nbsp; &nbsp; uCallbackMessage := DDGM_TRAYICON;<br>&nbsp; &nbsp; hIcon &nbsp;:= ActiveIconHandle;<br>&nbsp; end;<br>&nbsp; Shell_NotifyIcon(Msg, @Tnd);<br>end;<br><br>procedure TTrayNotifyIcon.SetHideTask(Value: Boolean);<br>const<br>&nbsp; ShowArray: array[Boolean] of integer = (sw_ShowNormal, sw_Hide);<br>begin<br>&nbsp; if FHideTask &lt;&gt; Value then<br>&nbsp; begin<br>&nbsp; &nbsp; FHideTask := Value;<br>&nbsp; &nbsp; if not (csDesigning in ComponentState) then<br>&nbsp; &nbsp; &nbsp; ShowWindow(Application.Handle, ShowArray[FHideTask]);<br>&nbsp; end;<br>end;<br><br>procedure TTrayNotifyIcon.SetHint(Value: string);<br>begin<br>&nbsp; if FHint &lt;&gt; Value then<br>&nbsp; begin<br>&nbsp; &nbsp; FHint := Value;<br>&nbsp; &nbsp; if FIconVisible then<br>&nbsp; &nbsp; &nbsp; SendTrayMessage(NIM_MODIFY, NIF_TIP);<br>&nbsp; end;<br>end;<br><br>procedure TTrayNotifyIcon.SetIcon(Value: TIcon);<br>begin<br>&nbsp; FIcon.Assign(Value);<br>&nbsp; if FIconVisible then SendTrayMessage(NIM_MODIFY, NIF_ICON);<br>end;<br><br>procedure TTrayNotifyIcon.SetIconVisible(Value: Boolean);<br>const<br>&nbsp; MsgArray: array[Boolean] of DWORD = (NIM_DELETE, NIM_ADD);<br>begin<br>&nbsp; if FIconVisible &lt;&gt; Value then<br>&nbsp; begin<br>&nbsp; &nbsp; FIconVisible := Value;<br>&nbsp; &nbsp; SendTrayMessage(MsgArray[Value], NIF_MESSAGE or NIF_ICON or NIF_TIP);<br>&nbsp; end;<br>end;<br><br>procedure TTrayNotifyIcon.SetPopupMenu(Value: TPopupMenu);<br>begin<br>&nbsp; FPopupMenu := Value;<br>&nbsp; if Value &lt;&gt; nil then Value.FreeNotification(Self);<br>end;<br><br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('Extend', [TTrayNotifyIcon]);<br>end;<br><br>const<br>&nbsp; TrayMsgStr = 'DDG.TrayNotifyIconMsg';<br><br>initialization<br>&nbsp; DDGM_TRAYICON := RegisterWindowMessage(TrayMsgStr);<br>&nbsp; IconMgr := TIconManager.Create;<br>finalization<br>&nbsp; IconMgr.Free;<br>end.<br>
 
你可以先定义一个TNotifyIconData的类型ServerStart1,不过先要USES SHELLAPI单元,<br>再用下面的填充结构:<br>&nbsp; serverStart1.cbSize:=Sizeof(TNotifyIconData);<br>&nbsp; serverStart1.Wnd:=self.Handle; <br>&nbsp; serverStart1.uID:=0;<br>&nbsp; ServerStart1.uFlags:=NIF_ICON or NIF_TIP or NIF_MESSAGE;<br>&nbsp; ServerStart1.uCallbackMessage:=Station_message;//这里是自定义消息<br>&nbsp; ServerStart1.hIcon:=icon1.Handle;// &nbsp;这是你要显示图标的句柄<br>&nbsp; ServerStart1.szTip:='服务正在运行;<br>再调用Shell_NotifyIcon(NIM_Add,@ServerStart1);来显示在右下角<br>程序结束时用Shell_NotifyIcon(NIM_Delete,@ServerStart1);来删除<br>不想程序运行时出现在任务栏里可以用下面的API函数<br>ShowWindow(Application.Handle,SW_Hide);就可以了,<br><br><br><br>
 
procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp;if Edit1.Text='' then &nbsp;<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; MessageBox(Handle,'应用程序名称不可以为空。','错误',MB_OK+MB_ICONERROR);<br>&nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;with TRegistry.Create;<br>&nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; RootKey:=HKEY_LOCAL_MACHINE;<br>&nbsp; &nbsp; &nbsp; &nbsp; if OpenKey('Software/Microsoft/Windows/CurrentVersion/Run',False) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WriteString('StartUp1', Edit1.Text); &nbsp; //如果是自己的程序,Edit1.Text改为ParamStr(0)就可以了<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MessageBox(Handle,'打开注册表失败。','错误',MB_OK+MB_ICONERROR);<br>&nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp; CloseKey;<br>&nbsp; &nbsp; &nbsp; &nbsp; Free;<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br><br>详细请看:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=1158387
 
to hongxing_dl<br>写一个路径到注册表里有用吗,我试了,好像没什么作用哦<br>to indexhome<br>&nbsp; ShowWindow(Application.Handle,SW_Hide); 这句用了也不能在任务条中隐藏<br>ServerStart1.uCallbackMessage:=Station_message;//这里是自定义消息<br>上面一句的Station_message应该传什么值才能双击打右下角图标就打开程序啊,谢谢<br>
 
怎么不用wab的?它的可以啊
 
不需要写什么注册表的<br>indexhome说的方法简单可行,只不过是没有说清楚<br>建议查一下:Shell_NotifyIcon 的在线帮助,很简单的
 
自动运行,那就是写注册表了。<br>右下角的图标,当然是写代码或用第三方控件。<br>不出现在任务栏中要用到一个API函数。<br>代码发给你:<br>、//创建图标过程。<br>procedure TmainForm.modifytrayicon(action: dword);<br>var<br>NIData:TNotifyIconData;<br>begin<br>&nbsp; &nbsp;with NIData do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp;cbSize:=SIZEOF(TNotifyIconData);<br>&nbsp; &nbsp;uid:=0;<br>&nbsp; &nbsp;uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br>&nbsp; &nbsp;wnd:=Handle;<br>&nbsp; &nbsp;uCallbackMessage:=wm_trayicon;<br>&nbsp; &nbsp;hIcon:=Application.Icon.Handle;<br>&nbsp; &nbsp;StrPCopy(szTip,Application.Title);<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;Shell_NotifyIcon(Action,@nidata);<br>end;<br>//调用创建图标过程<br>procedure TmainForm.FormCreate(Sender: TObject);<br>begin<br>modifytrayicon(NIM_ADD);<br>Image1.Left:=0;<br>Image1.Top:=0;<br>Image1.Stretch:=True;<br>StatusBar1.Panels[0].Text:=pass.dd;<br>end;<br><br>//取消图标<br>procedure TmainForm.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>if MessageDlg('确认要退出系统吗?',mtConfirmation,[mbOK,mbcancel],1)=mrok then<br>begin<br>modifytrayicon(NIM_DELETE);<br>Action:=caFree;<br>end else<br>Action:=caNone;<br>end
 
多人接受答案了。
 
后退
顶部