利用Delphi實作系統狀态欄圖示
下面以一個具體的例子,詳細介紹一下利用Delphi實作系統狀态欄圖示的步驟和方法。
1.建立一個應用程式,在主窗體上增加一個TpopupMenu元件。并為該彈出選單元件增加選單項Exit,标題為“退出”。
2.在Uses中添加ShellAPI,因為在系統狀态欄中增加圖示時需調用ShellAPI中的函數
Shell_NotifyIconA。該函數需要兩個參數,其中一個是TnotifyIconDataA結構,需在主窗體中增加TnotifyIconDataA類型的全局變量ntida。
3.定義消息mousemsg,并編寫主窗體的mousemessage消息處理函數,此函數說明在圖示上用滑鼠左鍵單擊時,會打開應用程式視窗;用滑鼠右鍵單擊時,會彈出一個選單。
下面給出步驟2和3的實作代碼:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Menus, shellapi;
const
mousemsg = wm_user + 1; //自定義消息,用于處理使用者在圖示上點選滑鼠的事件
iid = 100; //使用者自定義數值,在TnotifyIconDataA類型全局變量ntida中使用
type
TForm1 = class(TForm)
......
private
//自定義消息處理函數,處理滑鼠點選圖示事件
procedure mousemessage(var message: tmessage); message mousemsg;
public
{ Public declarations }
end;
var
Form1: TForm1;
ntida: TNotifyIcondataA;
//用于增加和删除系統狀态圖示
implementation
{.DFM}
procedure TForm1.mousemessage(var message: tmessage);
mousept: TPoint; //滑鼠點選位置
begin
inherited;
if message.LParam = wm_rbuttonup then begin //用滑鼠右鍵點選圖示
getcursorpos(mousept); //擷取光标位置
popupmenu1.popup(mousept.x, mousept.y);
//在光标位置彈出選單
if message.LParam = wm_lbuttonup then begin //用滑鼠左鍵點選圖示
//顯示應用程式視窗
ShowWindow(Handle, SW_SHOW);
//在工作列上顯示應用程式視窗
ShowWindow(Application.handle, SW_SHOW);
SetWindowLong(Application.Handle, GWL_EXSTYLE,
not (GetWindowLong(Application.handle, GWL_EXSTYLE)
or WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW));
message.Result := 0;
end
4.編寫FormCreate的代碼如下:
procedure TForm1.FormCreate(Sender: TObject);
ntida.cbSize := sizeof(tnotifyicondataa); //指定ntida的長度
ntida.Wnd := handle; //取應用程式主窗體的句柄
ntida.uID := iid; //使用者自定義的一個數值,在uCallbackMessage參數指定的消息中使
ntida.uFlags := nif_icon + nif_tip + nif_message;//指定在該結構中uCallbackMessage、hIcon和szTip參數都有效
ntida.uCallbackMessage := mousemsg;
//指定的視窗消息
ntida.hIcon := Application.Icon.handle;
//指定系統狀态欄顯示應用程式的圖示句柄
ntida.szTip := 'Icon';
//當滑鼠停留在系統狀态欄該圖示上時,出現該提示資訊
shell_notifyicona(NIM_ADD, @ntida);
//在系統狀态欄增加一個新圖示
5.編寫Tform1.OnClose的代碼如下:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
Action := caNone; //不對窗體進行任何操作
ShowWindow(Handle, SW_HIDE); //隐藏主窗體
//隐藏應用程式視窗在工作列上的顯示
ShowWindow(Application.Handle, SW_HIDE);
GetWindowLong(Application.handle, GWL_EXSTYLE)
or WS_EX_TOOLWINDOW AND NOT WS_EX _APPWINDOW);
6.編寫Exit代碼如下:
當使用者點選Exit時實作完全退出應用程式。具體代碼如下:
procedure TForm1.ExitClick(Sender: TObject);
//為ntida指派,指定各項參數
ntida.cbSize := sizeof(tnotifyicondataa);
ntida.wnd := handle;
ntida.uID := iid;
ntida.uFlags := nif_icon + nif_tip + nif_message;
shell_notifyicona(NIM_DELETE, @ntida);
//删除已有的應用程式圖示
Application.Terminate;
//中斷應用程式運作,退出應用程式
通過以上步驟,我們即可用Delphi輕松實作系統狀态欄圖示。
在Delphi中實作系統狀态欄圖示(托盤圖示)
程式中增加使用該功能時,須在Uses中添加ShellAPI。添加圖示時需要的函數是
:Shell_NotifyIconA,它需要2個參數,一個參數是TnotifyIconDataA的結構,該
結構如下:
TnotifyIconDataA = record
cbSize: DWORD;
Wnd: HWND;
uID: UINT;
uFlags: UINT;
uCallbackMessage: UINT;
hIcon: HICON;
szTip: ARRAY[0..63] of AnsiChar;
end;
dwMessage取值為:
NIM_ADD 增加一個新圖示
NIM_MODIFY 修改一個已有的圖示
NIM_DELETE 删除一個已有的圖示
實作一個Delphi中實作系統狀态欄圖示的窗體步驟如下:
1)建立一個應用程式
2)在主窗體上加一個TButton元件、一個TImage元件和一個TPopupMenu元件
3)編寫主窗體的mymessaage消息處理函數,此函數說明在圖示上用滑鼠右鍵單擊
時會彈出一個菜單
4)編寫TButton元件的OnClick事件及菜單項的OnClick事件
5)程式源代碼如下所示:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, Menus, ImgList, StdCtrls, ShellAPI, ExtCtrls;
const
mymsg = wm_user + 1;
iid = 100;
type
TForm1 = class(TForm)
Button1: TButton;
PopupMenu1: TPopupMenu;
a11: TMenuItem;
b11: TMenuItem;
c11: TMenuItem;
Image1: TImage;
procedure Button1Click(Sender: TObject);
procedure c11Click(Sender: TObject);
procedure a11Click(Sender: TObject);
private
{ Private declarations }
procedure mymessage(var message:tmessage);
message mymsg;
public
{ Public declarations }
end;
var
Form1: TForm1;
ntid: TnotifyIconDataA;
implementation
{$R *.dfm}
//隐藏視窗,在托盤上顯示圖示
procedure TForm1.Button1Click(Sender: TObject);
begin
ntid.cbsize := sizeof(TnotifyIconDataA);
ntid.Wnd := Handle;
ntid.uID := iid;
ntid.uFlags := NIF_ICON + NIF_TIP + NIF_MESSAGE;
ntid.uCallbackMessage := mymsg;
ntid.hIcon := image1.Picture.Icon.Handle;
ntid.szTip := 'asdf';
shell_noti
fyicona(NIM_ADD,@ntid);
self.Visible := false;
end;
//處理滑鼠右鍵,彈出菜單
procedure TForm1.mymessage(var message:tmessage );
mypt:Tpoint;
Inherited;
if message.LParam = WM_RBUTTONUP then
begin
getCursorPos(mypt);
PopupMenu1.Popup(mypt.X, mypt.Y);
message.rResult := 0;
procedure TForm1.c11Click(Sender: TObject);
close;
//還原視窗,托盤圖示消失
procedure TForm1.a11Click(Sender: TObject);
ntid.cbSize := sizeof(TnotifyIconDataA);
shell_notifyicona(NIM_DELETE,@ntid);
self.Visible := true;
end.
檔案是同一類型元素的有序集合,是記憶體與外設間傳輸資料的管道。一些外設如顯示器、鍵盤、列印機等都可以看作檔案,但最常用的還是磁盤檔案,這也是本章我們主要讨論的對象。
Delphi繼承了Object Pascal的檔案管理功能,并有很大的發展,其中最主要的是提供了用于檔案管理的标準控件,同時也提供了更多的檔案管理函數。利用Delphi的強大功能,開發一個自己的檔案管理系統就成為很容易的事。
本章首先介紹Delphi檔案管理的基本概念和标準過程/函數,并提供了一個記錄檔案的應用執行個體,這是從我們實際課題開發中提取出來的。而後介紹Delphi提供的檔案控件的使用方法。最後提供的一個綜合例程MDI檔案管理器則是對Delphi檔案管理功能的綜合應用。