程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 任務欄中的托盤程序設計

任務欄中的托盤程序設計

編輯:Delphi

 

 

 

  在我們平常設計項目時通常把一些常駐內存或使用頻率比較高的程序做成托盤的形式,一來能使程序外觀得到整潔性,二來能使程序更具專業性。
  在設計任務欄中的托盤程序時主要用到了一個WIN API函數Shell_NotifyIcon,大部分功能的實現都是在此函數基礎上來實現的,該函數的原型如下: 
  WINSHELLAPI BOOL WINAPI Shell_NotifyIcon(
DWORD dwMessage, // message identifier
PNOTIFYICONDATA pnid // pointer to structure
                     );
  通過看到參數我們知道要傳遞一個dwMessage消息參數的標識類型和一個指向具體修改信息的結構pnid,第一個參數的具體取值如下:
  dwMessage
  Identifier of the message to send. This parameter can be one of these values:
  NIM_ADD Adds an icon to the status area.{添加圖標}
  NIM_DELETE Deletes an icon from the status area.{刪除圖標}
  NIM_MODIFY Modifies an icon in the status area. {修改圖標}
  即為添加一個圖標,刪除一個圖標和修改一個圖標在任務欄的右部區域中。可以通過第一個參數的內容來定制對任務欄狀態區的操作,第二個參數的具體取值如下:
  pnid
  Pointer to a NOTIFYICONDATA structure. The content of the structure depends on the value of dwMessage. 
  此參數為一個指向NOTIFYICONDATA結構的指針,通過這個結構來對設計者想要的操作進行定義,結構成員定義如下:
  typedef struct _NOTIFYICONDATA { // nid 
DWORD cbSize; {結構的大小}
HWND hWnd; {關聯狀態區圖標的窗口句柄,用來處理回調函數}
UINT uID; {應用程序定義的狀態區圖標的標識符}
UINT uFlags; {此成員表明NOTIFYICONDATA記錄中的成員uCallbackMessage、hIcon和szTip這三者的哪些項的值有效}
UINT uCallbackMessage; {程序定義的消息標識符,例如當鼠標在狀態區圖標上移動或者左右點擊所發生的事情時,操作系統將向Wnd指定的那個窗口發送uCallbackMessage消息。在uCallbackMessage消息中,lParam參數包含了Windows的鼠標消息的類型,而wParam參數則包含了圖標標識(即uID)。有效的鼠標消息包括以下幾個:WM_LBUTTONDOWN、WM_RBUTTONDOWN、WM_MBUTTONDOWN、WM_LBUTTONUP、WM_RBUTTONUP、WM_MBUTTONUP、WM_MOUSEMOVE、WM_LBUTTONDBLCLK、WM_RBUTTONDBLCLK以及WM_MBUTTONDBLCLK。
}
HICON hIcon; {指定一個應用程序定義的圖標的句柄}
char szTip[64];{在圖標上顯示的提示信息} 
  } NOTIFYICONDATA, *PNOTIFYICONDATA; 

  通過前面對Shell_NotifyIcon函數的基本認識,我們知道了具體的操作有添加,刪除和修改圖標,下面我們以一個實例來具體的去實現托盤程序的效果。 
  新建一個工程,然後放入ApplicationEvents1控件,在additional控件面板中,本例程帶有彈出菜單功能。
  代碼如下:
unit Unit1;

interface

uses
Windows,shellapi, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,AppEvnts, ImgList, Menus;
{一定要uses shellapi單元}
const
ghy_tray=wm_user+2;
type
TForm1 = class(TForm)
ImageList1: TImageList;
ApplicationEvents1: TApplicationEvents;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
procedure ApplicationEvents1Minimize(Sender: TObject);
procedure ApplicationEvents1Restore(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure WMSysCommand(var msg: TMessage);message wm_syscommand;
{ Private declarations }
public
procedure mytray(var Msg: TMessage); message ghy_tray;
{ Public declarations }
end;

var
Form1: TForm1;
tray1,tray2:TNotifyIconData;
ico1,ico2:ticon;
tags:boolean;
implementation

{$R *.DFM}
procedure Tform1.WMSysCommand(var msg: TMessage);
begin
if msg.WParam = SC_MINIMIZE then
begin
showwindow(application.handle,sw_hide);
inherited;
end
else
inherited;
end;

procedure tform1.mytray(var Msg: TMessage);
var
pt:tpoint;
begin
GetCursorPos(pt);

case msg.lParam of
WM_LBUTTONDOWN:
begin
//鼠標左鍵被按下
if tags=true then
begin
application.Minimize;
form1.Hide;
end
else
begin
application.Restore;
end;
end;

WM_LBUTTONUP:
begin
//釋放鼠標左鍵
end;
wm_rbuttondown:
begin
//鼠標右鍵被按下
SetForegroundWindow(Form1.Handle);
form1.PopupMenu1.Popup(pt.x,pt.y);
end
else//調用父類的WndProc方法處理其它消息
inherited;
end;


end;

procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
tags:=false;
ShowWindow(Application.Handle,SW_HIDE);


tray2.cbSize:=sizeof(tray2);
tray2.Wnd:=form1.Handle;
tray2.uID:=0;
tray2.uFlags:=NIF_ICON or NIF_TIP or NIF_MESSAGE;
tray2.uCallbackMessage:=ghy_tray;
tray2.hIcon:=ico2.Handle;
tray2.szTip:=’我的托盤程序’;
if not Shell_NotifyIcon(NIM_modify,@tray2) then
begin
ShowMessage(’no’);
end;
end;

procedure TForm1.ApplicationEvents1Restore(Sender: TObject);
begin
tags:=true;
form1.show;

ico1:=ticon.Create;
ico2:=ticon.create;
imagelist1.GetIcon(0,ico1);
imagelist1.geticon(1,ico2);
tray1.cbSize:=sizeof(tray1);
tray1.Wnd:=form1.Handle;
tray1.uID:=0;
tray1.uFlags:=NIF_ICON or NIF_TIP or NIF_MESSAGE;
tray1.uCallbackMessage:=ghy_tray;
tray1.hIcon:=ico1.Handle;
tray1.szTip:=’我的托盤程序’;
if not Shell_NotifyIcon(NIM_modify,@tray1) then
begin
ShowMessage(’no’);
end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ico1:=ticon.Create;
ico2:=ticon.create;
imagelist1.GetIcon(0,ico1);
imagelist1.geticon(1,ico2);
tray1.cbSize:=sizeof(tray1);
tray1.Wnd:=form1.Handle;
tray1.uID:=0;
tray1.uFlags:=NIF_ICON or NIF_TIP or NIF_MESSAGE;
tray1.uCallbackMessage:=ghy_tray;
tray1.hIcon:=ico1.Handle;
tray1.szTip:=’我的托盤程序’;
if not Shell_NotifyIcon(NIM_delete,@tray1) then
begin
ShowMessage(’no’);
end;

end;

procedure TForm1.N1Click(Sender: TObject);
begin
showmessage(’you click open’);
end;

procedure TForm1.N2Click(Sender: TObject);
begin
showmessage(’you click save’);
end;

procedure TForm1.N3Click(Sender: TObje

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved