Write the TRAY program with C ++ Builder

zhaozj2021-02-17  71

TRAY is a special area on the Windows 9x task bar. Its technical name is "taskbar jet", some software (such as Jinshan Word III) runs an icon on the tray when running, using the user to know This program is running in the background. It is easy to activate it. It usually only needs to click this icon, it is very convenient. TRAY programming is special, but it is not difficult, mainly including three aspects such as icons, tool tips, and messages, which are part of the shell programming. Shellapi provides a shell-notifyicon function that can be used, delete, or modify the icon in the tray. After placing the icon on the tray, the Windows Shell will be responsible for notifying the app on the icon. The shell-notifyicon function is defined as follows:

Winshellapi Bool WinApi Shell-Notify, PNOTICONDATA PNID;

DwMessage denotes actions to be completed: NIM-Add (Add Icon), NIM-delete (delete icon), NIM-Modify (modify icon or prompt text), PNID is a pointer to the Notifyicondata structure, the definition of the structure is as follows:

TypeDef struct -notify; // The number of bytes of the structure must be initialized with the size of the structure. HWND hWnd; // Accept the window handle of the TRAY icon message; // is used by the application defined icon ID UINT UFLAGS; // Used to identify the domain that needs to change its value, // nif_icon indicates that HICON is valid, can be used to modify Icon, // nif_message indicates that uCallbackMessage is valid, used to define messages, // NIF-TIP indicates that the SZTIP parameter is valid, modify the tooltip. UINT UCALLBACKMESSAGE; // Application defined message Hicon Hicon; // TRAY icon handle char sztip [64]; // Tool Tip text} notifyicondata;

Below we will explain the implementation method through a concrete example, the program is running without displaying a main form, and only an icon is added to the tray, double-click the icon to close the program.

Create a new project to place a Timer control to the form. Open the unit1.h file, add the header file description #include , add a statement of some data members and methods in the private paragraph defined in TFORM1:

Unsigned int iconMessage; // Defined message void addtrayicon (); // Add icon void RemoveTrayicon () on the tray; // Remove icons from the tray

Since the processing of the custom message is to be added, the window process function WndProc must be overused, and the protected section is added in the definition of TFORM1: Virtual Void - FastCall WndProc (Messages :: TMESSAGE & Message);

Define the corresponding member functions in Unit1.cpp:

Void TFORM1 :: AddTrayicon () {Notifyicondata IconData; Memset (IconData); // Initialize Structure IconData's Domain to 0 iconData.cbsize = SizeOf (icondata); icondata.hwnd = Handle; Strncpy (IconData.sztip, "Unknown Status", SizeOf (IconData.sztip); iconData.hicon = Application-> icon-> handle; iconData.UCallbackMessage = iconMessage; iconData.uflags = nif-message | NIF-ICON | NIF- TIP; Shell-NotifyIcon (NIM-ADD, & icondata);} void TForm1 :: RemoveTrayIcon () {NOTIFYICONDATA icondata; memset (& icondata, 0, sizeof (icondata)); icondata.cbSize = sizeof (icondata); icondata.hWnd = Handle; Shell-Notifyicon (Nim-delete, & iconData);

Repetition TFORM1's WndProc function, add processing code for custom messages, which is actually equivalent to the subclass of the TFORM class.

Void __fastcall tform1 :: wndproc (messages :: tMESSAGE & Message) {if (message.msg == iconMessage) {if (message.lparam == wm-lbuttondblclk) {Application-> Terminate (); // If you double-click the icon, Close the application} return;} TForm :: WndProc (Message); // For other messages, call the base class WndProc function to make Windows default. }

Create a form of oncreate event handle:

Void __fastcall tform1 :: formcreate (TOBJECT * Sender) {iconMessage = registerWindowMessage ("iconnotify"); addtrayicon ();}

A user message is defined by calling the RegisterWindowMessage function, or a message number that is not used by WM_USER N can also be obtained.

Void __fastcall tform1 :: formdestroy (TOBJECT * Sender) {transovetrayicon (); // The form is deleted in the icon in the tray when it is closed.

Write the Timer event code of Timer1, when the user leaves the mouse on the icon, display the prompt text:

void __fastcall TForm1 :: Timer1Timer (TObject * Sender) {NOTIFYICONDATA icondata; memset (& icondata, 0, sizeof (icondata)); icondata.cbSize = sizeof (icondata); icondata.hWnd = Handle; String s = "my icon! "; // Define the prompt text STRNCPY (icondata.sztip, s.c_str (), sizeof (icondata.sztip)); iconData.uflags = nif-tip; shell-notifyicon (nim-modify, & icondata);} Do not display the main form, only the corresponding program icon is placed on the tray, select View | Project Source from the C Builder Gatherings, add code after the Application → Initialize () statement of the Winmain function:

ShowWindow (Application → Handle, Sw-Hide); Application → ShowMainform = False;

Press F9 to compile and run the program, the corresponding icon will appear on the tray. The above code is compiled in C Builder3, PWIN98 environments and runs. = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -

//

__________ / lb / ___ Outinn

/ _ [] _ ​​/ ____ / /

/ _________ / | () | / __ /

Http://outinn.yeah.net/

| ____ / - | __ | - / | Welcome to Visit Outinn!

| __ | == | ___ | || | __ |

- = - = - = - | _ || _ | = - FANCY,

Outinn@china.com

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

New Post(0)