Custom message processing function (is applet to tray)

Source: Internet
Author: User

Many applications now have a feature that when a user chooses to minimize a window, the window is not minimized to the taskbar as usual, but "minimized" into a taskbar icon. Such functions as FoxMail 3.0 Netvampire 3.0 are provided. Implementing such a feature is not really complicated, and when the window is minimized, the window emits a wm_syscommand message, you just need to intercept the Windows Wm_syscommand message, hide the window when the window is minimized, and call the WINDOWSAPI function Shell_ NotifyIcon add the defined icon to the taskbar, Shell_NotifyIcon's function definition is this: functions Shell_NotifyIcon (Dwmessage:dword; lpdata: Pnotifyicondata): BOOL; stdcall; The parameter dwmessage specifies the operation of the Shell_NotifyIcon function, which can be one of the three values of the Nim_add nim_delete nim_modify, corresponding to the action of adding an icon, deleting the icon, and modifying the icon.
The definition of the PNOTIFYICONDATA structure pointed to by the----parameter lpdata is as follows:
_notifyicondataw = Record
Cbsize:dword;
Wnd:hwnd;
Uid:uint;
Uflags:uint;
Ucallbackmessage:uint;
Hicon:hicon;
Sztip:array [0..63] of Widechar;
End

Tnotifyicondata = _notifyicondataw;
----In this structure WND indicate the owning window, ucallbackmessage indicates the callback message, if WND and Ucallbackmessage are indicated, then when the user has action on the taskbar icon (such as clicking the icon, moving the cursor on the icon, etc.). The system sends a UCALLBACKMESSAGE message to the window specified by WND. Hicon is the handle to the icon to be added, Sztip is the cue line of the icon (that is, when you move the cursor to the icon, the text appears in a small yellow box). News. To implement the above functions, the main thing is to process wm_syscommand messages and custom icon messages, which do not have corresponding events in Delphi. This will require the use of Delphi's custom message processing capabilities to intercept and process these messages.

----First look at the following procedure. Before executing the program, the first thing is to change the Form1 's Icon property, to load an icon for FORM1, otherwise there will be a blank on the taskbar.

The code is as follows:

InterfaceusesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls,shellapi; ConstWm_baricon=wm_user+ $;typetminfrm=class(Tform)Private    {Private Declarations}    //when the window is minimized, the window emits a WM_SYSCOMMAND message//The main thing is to process wm_syscommand messages and custom icon messages,//These messages do not have a corresponding event in Delphi. You need to use the custom message processing capabilities of Delphi to intercept and process these messages .    procedureWmsyscommand (var message: Tmessage);messageWm_syscommand; procedureWmbaricon (var message: Tmessage);messageWm_baricon;  Public    {Public Declarations}  End;varminfrm:tminfrm;Implementation{$R *.DFM}{TFORM10}procedureTminfrm.wmbaricon (var message: tmessage);varLpdata:pnotifyicondata;begin  if(Message.lparam = wm_lbuttondown) Then   begin    //If the user clicks on the taskbar icon, the icon is deleted and the window is replied. Lpdata: =new (PNOTIFYICONDATAA); Lpdata.cbsize:= the;//SizeOf (PNOTIFYICONDATAA);Lpdata.wnd: =Minfrm.handle; Lpdata.hicon:=MinFrm.Icon.Handle; //Ucallbackmessage indicates the callback message, if WND and Ucallbackmessage are indicated,    //when the user has an action on the taskbar icon (such as clicking the icon, moving the cursor on the icon, etc.).     //the system sends a UCALLBACKMESSAGE message to the WND specified windowLpdata.ucallbackmessage: =Wm_baricon; Lpdata.uid:=0; Lpdata.sztip:='Samples'; Lpdata.uflags:= Nif_iconorNif_messageorNif_tip;    Shell_NotifyIcon (Nim_delete,lpdata);    Dispose (lpdata); Minfrm.visible:=True; End;End;procedureTminfrm.wmsyscommand (var message: Tmessage); varLpdata:pnotifyicondata; //struct WND indicates the owning windowbegin   if message. Wparam=sc_icon Then     //Sc_icon of the system   begin      //Hide the window and add an icon to the taskbar if the user minimizes the windowlpdata:=New (PNOTIFYICONDATAA); Lpdata.cbsize:= the; //SizeOf (PNOTIFYICONDATAA);Lpdata.wnd: = Minfrm.handle;//Current Window HandleLpdata.hicon: =MinFrm.Icon.Handle; Lpdata.ucallbackmessage:= Wm_baricon;//Callback MessageLpdata.uid: =0; Lpdata.sztip:='Samples'; Lpdata.uflags:=Nif_iconorNif_messageorNif_tip; //function Shell_NotifyIcon (Dwmessage:dword; lpdata:pnotifyicondata): BOOL; stdcall;    //The parameter dwmessage specifies the operation of the Shell_NotifyIcon function, which can be nim_add nim_delete nim_modify    //one of three values that corresponds to the action of adding icons, deleting icons, and modifying icons. Shell_NotifyIcon (Nim_add,lpdata);    Dispose (lpdata); Minfrm.visible:= False;//Hide Form   End Else   begin     //if it is another Systemcommand message, the system default handler is called to handle it. DefWindowProc (minfrm.handle,message.   Msg,message.wparam,message.lparam); End;End;End.

Http://www.cnblogs.com/gaiyang/archive/2011/09/15/2177237.html

Custom message processing function (is applet to tray)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.