Many people think that Delphi is a RAD tool, including myself, during his school days on Delphi also biased, out of the "ivory tower", involving a wide range, and the problems encountered more slowly and also have their own point of views Experience. In fact, Delphi is based on Object Pascal language development tool, that is to say Delphi is essentially a language tool, and is truly object-oriented. Below me cite the example is to use Delphi to a tray of small procedures. Duanxiaojinghan procedures, the context clear, I will explain in detail some of the key. Designate Junjie, as the MFC ripping the same level after level, I also came to a "Paodingjieniu."
Delphi involved in the programming aspect of the system, without exception, should call API functions in the unit in ShellAPI.pas to use the API function prototype.
Combat drill:
1. A new application: File-> New Applicaton Interface part in the definition of a message constants: const WM_NID = WM_USER +1000; requirements from the start WM_USER user-defined information.
2. Define a global variables: NotifyIcon: TNotifyIconData, NotifyIcon is a very important variable, the entire process is basically turn around in this variable. TNotifyIconData is a record types, hold down the Ctrl key and double-click in the TNotifyIconData enter ShellAPI.pas unit. (Note: In Delphi, this is a very good analysis of the source code means all the source code, you want to know the inside story behind the process, the best way is to analyse the source code!) Appear at the following assignments:
TNotifyIconData = TNotifyIconDataA this point is obvious, that is to say TNotifyIconData and TNotifyIconDataA the same kind of data types, and then down to see:
TNotifyIconDataA = _NOTIFYICONDATAA, meaning just the same, and goes to see:
Type
_NOTIFYICONDATAA = Record
CbSize: DWORD;
Wnd: HWND;
UID: UINT;
UFlags: UINT;
UCallbackMessage: UINT;
HIcon: HICON;
SzTip: array [0 .. 63] of AnsiChar;
End;
This can be really "long come out, Utah Instead遮面." Right now, everyone is very clear, we have the definition of global variables is a NotifyIcon contains seven components of the record type variables, the equivalent of C / C + + in the structure of the variables (C / C + + programmers should be more familiar with the ). Below we explain to each record type in the seven parts of what each function.
1> cbSize is your definition of the size of the NotifyIcon variables used SizeOf (TNotifyIconData) can be achieved, if you are a skilled C / C + + programmer, you should not unfamiliar. In C / C + +, whenever a structure for the variable distribution of the time in memory: SizeOf (Struct type) was informed that the storage of such a structure variables, the number of memory.
2> Wnd is a handle, you want the news tray procedures to deal with the form which has let Wnd point that form.
For example: you prepare the tray in the task bar, click on the icon when the form is the form in the "show" and "hide" between the switch at the main form put Wnd.
3> uID: If you want to create multiple small procedure trays, then how distinguish between them? ID is, to rely on this distinction.
3> uFlags is a flag, it said that the current procedure trays created by the nature of which:
NIF_ICON said that the current set up by the icon (hIcon value) is effective
NIF_MESSAGE said that the current system set up by the news (that is, uCallBackMessage value) is effective
NIF_TIP said that the current set up by the tips of the (szTip value) is effective.
4> 7 uCallBackMessage This is the inside of one of the most important. Here designated a callback information here that is the definition of a message when you click or right-click the tray icon will be the time to point you in the form Wnd by sending a uCallBackMessage were in the definition of news, then you in the process of defining a news function to deal with the news. This put Windows on the package of information flow are handled well.
6> hIcon handle for the tray icon, according to you can handle this increase, modify, delete icon.
7> szTip is that when you put the mouse on the task bar icon on the tray when the pop-up to the messages.
Here, I spent a great deal of ink on TNotifyIconData insider, the clear part of this, the back of the logical things.
3. Double-click the main window, enter the code FormCreate region:
TForm1.FormCreate (Sender: TObject);
Begin
/ / NotifyIcon for global variables, the procedure has been defined at the beginning
With NotifyIcon do
Begin
CbSize: = SizeOf (TNotifyIconData);
Wnd: = Handle; / / refers to the current form of the handle Form1
UID: = 1;
UFlags: = NIM_ICON or NIM_MESSAGE or NIM_TIP;
UCallBackMessage: = WM_NID;
HIcon: = Application.Icon.Handle;
SzTip: = "Chang Wu Shao";
End;.
/ / Set up the good NotifyIcon accession to the system variables in order to deal with
Shell_NotifyIcon (NIM_ADD, @ NotifyIcon);
End;
4. The next step is to define a message processing function: to the system to a form of a message, by the following function to deal with this. Each message handling functions are dealing with a particular type of news, we carefully look at the following function in the definition and the definition of the general function of what is the same: message handling functions in the source added after the name, so that when the system WM_NID to news that automatically triggers WMNID message processing function.
Procedure WMNID (var msg: TMessage); message WM_NID;
Begin
Case msg.LParam of
WM_LBUTTONUp; Form1.Visible: = not Form1.Visible;
WM_RBUTTONUP: ShowMessage ( 'you click on the Right');
End;
End;
Well, one of the most simple procedure was born, we set up their own favorite icon.
Project-> Options, select Application pages load in the Icon of their favorite icons, this is running in the task bar is displayed by your favorite icon. When you click on the icon, will form Form1 and not visible in the foreseeable switch between, that is to say click, and then click to hide. When you right-click on the icon will pop up when a message: "you click on the right."
5. Finally, remember to close the application's time to swap release procedures established by the pallet, or else will occupy system resources.
TForm1.FormDestroy (Sender: TObject);
Begin
Shell_NotifyIcon (NIM_DELETE, @ NotifyIcon);
End;
Graduation fast six months, a lot of things can not be understood in the school, not a profound understanding; out to the community, a lot of contacts Road, friends, benefited from every experience sharing, and always wanted to in the text, a study to sum up their own things , and to the common exchange. |