Many people think Delphi is a RAD tool, including myself, who was biased against Delphi when I was in school. Now that I have stepped out of the "ivory tower", it has covered a wider range of areas and encountered more problems, and I have gradually gained my own point of view. Experience. In fact, Delphi is a development tool based on the Object Pascal language, which means that Delphi is essentially a language tool and is truly object-oriented. The example I give below is a tray applet implemented in Delphi. The program is short and concise, with clear context. I will explain the key parts in detail. Just like Mr. Hou Junjie peeling off the MFC layer by layer, today I also have to "cook the ox".
In Delphi, when it comes to system programming, API functions must be called without exception. There are prototypes of the API functions to be used in the ShellAPI.pas unit.
Practical exercises:
one. Create a new application: File->New Application. Define a message constant in the Interface section: const WM_NID=WM_USER+1000; The system stipulates that messages for users can be customized starting from WM_USER.
two. Define a global variable: NotifyIcon:TNotifyIconData. NotifyIcon is a very important variable. The entire program basically revolves around this variable. TNotifyIconData is a record type. Hold down the Ctrl key and double-click TNotifyIconData to enter the ShellAPI.pas unit. (Note: In Delphi, this is a very good way to analyze the source code. The source code speaks for itself. If you want to know the inside story behind the program, the best way is to analyze the source code!) At this time, the following appears Assignment statement:
TNotifyIconData = TNotifyIconDataA, this meaning is very obvious, that is to say, TNotifyIconData and TNotifyIconDataA are the same data type, and then look down:
TNotifyIconDataA = _NOTIFYICONDATAA, the meaning is the same as before, look down:
type
_NOTIFYICONDATAA = record
cbSize: DWord;
Wnd: HWND;
uID: UINT;
uFlags: UINT;
uCallbackMessage: UINT;
hIcon: HICON;
szTip: array [0..63] of AnsiChar;
end;
This is really "coming out after being called out for a long time, but still holding the pipa half-hiding the face." Now everyone is very clear. The global variable NotifyIcon we just defined is actually a record type variable containing 7 components, which is equivalent to the structure variable in C/C++ (C/C++ programmers should be very familiar with it. ). Let's explain the functions of each of the seven parts of the record type one by one.
1> cbSize is the size of the NotifyIcon variable you defined, which can be obtained using SizeOf(TNotifyIconData). If you are a skilled C/C++ programmer, you should be familiar with it. In C/C++, whenever you want to allocate memory for a structure variable, you must use SizeOf(Struct type) to know how much memory is needed to store such a structure variable.
2> Wnd is a handle. Which form do you want the messages generated by the tray program to handle? Just let Wnd point to that form.
For example: when you want to click on the small tray icon on the taskbar to switch the form between "show" and "hide", point Wnd to the main form.
3> uID: If you want to create multiple tray applets, how to distinguish them? It is distinguished by this ID number.
3> uFlags is a flag bit, which indicates the properties of the currently created tray program:
NIF_ICON indicates that the currently set icon (that is, the value of hIcon) is valid
NIF_MESSAGE indicates that the currently set system message (that is, the value of uCallBackMessage) is valid
NIF_TIP indicates that the currently set tip bar (that is, the value of szTip) is valid.
4> uCallBackMessage This is the most important one among the 7 parts. Specify a callback message here, that is to say, define a message name here. When you click or right-click the tray icon, a message name defined in uCallBackMessage will be sent to the form pointed to by Wnd, and then you Define a message out function in the program to process this message. In this way, the entire set of messages related to Windows is processed.
6> hIcon is the handle of the tray icon. Based on this handle, you can add, modify, and delete icons.
7> szTip is the prompt message that pops up when your mouse is placed on the small icon in the taskbar tray.
Here I have spent a lot of ink introducing the inside story of TNotifyIconData. Once this part is clarified, the rest will fall into place.
three. Double-click the main form to enter the code area of FormCreate:
TForm1.FormCreate(Sender:TObject);
Begin
//NotifyIcon is a global variable and has been defined at the beginning of the program
with NotifyIcon do
begin
cbSize:=SizeOf(TNotifyIconData);
Wnd:=Handle; //Handle pointing to the current form Form1
uID:=1;
uFlags:=NIM_ICON or NIM_MESSAGE or NIM_TIP;
uCallBackMessage:=WM_NID;
hIcon:=application.Icon.Handle;
szTip:=”The evil young man of the Zhang family”;
end;.
//Add the set variable NotifyIcon to the system for processing
Shell_NotifyIcon(NIM_ADD,@NotifyIcon);
End;
Four. The next step is to define a message processing function: when the system sends a message to the form, it is processed by the following function. Each message processing function processes a certain type of message. Please take a closer look at the difference between the definition of the function body below and the general function definition: the message processing function must add the name of the message at the end, so that when the system sends When a WM_NID message comes, the WMNID message processing function is automatically triggered.
PRocedure WMNID(var msg:TMessage);message WM_NID;
begin
case msg.LParam of
WM_LBUTTONUp; Form1.Visible:=not Form1.Visible;
WM_RBUTTONUP: ShowMessage('You clicked the right button');
End;
End;
Well, the simplest program is born. Everyone can set their favorite icons.
Project->Options, select the Application page, and load your favorite icon in the Icon item, so that when the program is running, your favorite icon will be displayed in the taskbar. When you click the icon, the form Form1 will switch between visible and invisible, that is to say, click to display it and click again to hide it. When you right-click the icon, a message pops up: "You clicked the right button."
five. Finally, remember to release the created tray program when closing the application, otherwise it will occupy system resources.
TForm1.FormDestroy(Sender:TObject);
Begin
Shell_NotifyIcon(NIM_DELETE,@NotifyIcon);
End;
It’s been almost half a year since I graduated. I still can’t understand many things in school and my understanding is not deep enough. When I go out into the society, I have met many Taoist friends and I have benefited a lot. Every time I have an experience, I always want to write it down in words to summarize what I have learned. Second, let’s communicate with everyone.
E_Mail: [email protected] QQ: 8133413 The bad boy of the Zhang family