Delphi tray programming actual combat drill

xiaoxiao2021-03-06  19

[Document Guide] There is no exception in Delphi to call the API function without exception. In the shellapi.pas unit, many people who have the prototype of the API function to be used by the Delphi are a RAD tool, including myself at school. I also have a prejudice to Delphi. Now I have gone out of the "ivory tower", which is widened, and there is more problems, and I have a little experience in my own experience. In fact, Delphi is a development tool based on Object Pascal language, that is, Delphi is essentially a language tool and is a true object-oriented. The following example is a tray appler that implemented with Delphi. The program is short, and the context is clear, I will explain the key part. Just like Mr. Wen Jie, the MFC layer was peeled off, today I also came to "Ding Jie Cow". There is no exception to call the API function without exception in Delphi, and there is a prototype of the API function to be used in the shellapi.PAS unit. Practical drill: one. Create a new application: file-> New Applicat defines a message constant in the interface section: const wm_nid = WM_USER 1000; the system specifies that the message starts from WM_USER to customize the message. Two. Define a global variable: Notifyicon: Tnotifyicondata, Notifyicon is a very important variable, and the entire program is basically around this variable. TNOTIFYICONDATA is a record type, hold down the Ctrl key, double-click on TNOTICONDATA to enter the shellapi.pas unit. (Note: In Delphi, this is a very good way to analyze the source code. The source code explains everything. You have to know the insider behind the program, the best way is to analyze the source code!) The following occurred assignment: TNotifyIconData = TNotifyIconDataA, the meaning is clear, that TNotifyIconData and TNotifyIconDataA be of the same data type, then look down there: TNotifyIconDataA = _NOTIFYICONDATAA, just as with the meaning, then look down: type _NOTIFYICONDATAA = record cbSize: DWORD; wnd: hwnd; uid: uint; ucallback measure: uint; hICON: hic; sztip: array [0..63] of ansichar; end; this can be 媸 恰 敉蚧 敉蚧 汲      胝 胝 诿妗 诖蠹 诖蠹 宄      缙 是 是 是 缙 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是Structural variables (C / C programmers should be more familiar). Let's explain what the seven parts of the record type are explained one by one. 1> CBSIZE is the size of the NOTIFYICON variable you define, you can get in sizeof (tnotifyicondata), if you are a skilled C / C programmer, you should not be unfamiliar. In C / C , whenever you want to allocate memory for a structural variable: How much memory should be known to store one such a structural variable via SIZEOF (STRUCT TYPE).

2> WND is a handle, and you want the message generated by the tray program to handle the WND to point to that form. For example: When you are ready to click on the tray small icon in the taskbar, the form is switched between "Show" and "Hide", point the WND to the main form. 3> Uid: If you want to create multiple pallets, how do you distinguish between them? It is distinguished by this ID. 4> uflags is a logo, which indicates which properties of the currently created tray program: Nif_icon indicates that the currently set icon (ie, the value of HiCON) is a valid NIF_MESSAGE represents the currently set system message (ie UCALLBACKMESSAGE) It is a valid NIF_TIP represents the currently set prompt (ie, the value of SZTIP) is effective. 5> UcallbackMessage This is the most important one in seven parts. Here, you specify a callback message, which means that a message name is defined. When you click or Right-click the tray icon, you will send a message name defined in UCALLBACKMESSAGE in the WND, then you are A message is defined in the program to process the message. This will handle the entire plurality of windows about the message. 6> HiCon is the handle of the tray icon, which you can add, modify, and delete icons according to this handle. 7> Sztip is the prompt information that is played when your mouse is placed on the small icon of the task bar tray. Here I spent a lot of pen ink introducing TNOTIFYICONDATA, making this part clear, and there is a constant chapter. three. Double-click the main form, enter the code area of ​​formcreate: tForm1.FormCreate (Sender: TOBJECT); begin // notifyicon is global variable, in the beginning of the program with Notifyicon Do Begin Cbsize: = SizeOf (TNOTICONDATA); WND: = Handle; // pointing to the current form FORM1 handle Uid: = 1; uflags: = nim_icon or nim_message: = WM_NID; hICON: = Application.icon.Handle; sztip: = "Application Name"; // Add a variable NOTIFYICON to the system to handle shell_notifyicon (nim_add, @ notifyicon); End; 4. Next is to define a message processing function: The system issues a message to the form, which is processed by the following function. Each message handler is handled for a certain type of message. You carefully see the definition of the following function and the general function definition there is no difference: message processing function To add the name of the message, so when the system When you come to the WM_NID message, it is automatically triggered the WMNID message processing function.

转载请注明原文地址:https://www.9cbs.com/read-42666.html

New Post(0)