Delphi Custom Message Application Example

Source: Internet
Author: User
Tags function definition

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 rather "minimized" into a taskbar icon. Such features are provided like Foxmail 3.0 Netvampire 3.0.

Implementing such a function is actually not complicated, when the window is minimized, the window emits wm_syscommand messages, you need to intercept Windows Wm_syscommand messages, hide the window when the window is minimized, and invoke the WINDOWSAPI function Shell_ NotifyIcon adds the defined icon to the taskbar, Shell_NotifyIcon's function definition is this: function 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 an icon, and modifying an icon.

The pnotifyicondata structure that the parameter lpdata points to is defined 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 indicates the window to which it belongs, Ucallbackmessage indicates the callback message, and if WND and Ucallbackmessage are indicated, the user moves on the taskbar icon (such as clicking on the icon, moving the cursor over the icon, etc.). The system sends a UCALLBACKMESSAGE message to the WND specified window. Hicon is the handle to the icon you want to add, Sztip is the cue line for the icon (that is, when you move the cursor to the icon, the text appears in a small yellow box). News. To achieve the above functions, the most important thing is to process wm_syscommand messages and custom icon messages, these messages in Delphi there is no corresponding event. Here you need to use the custom message processing functionality of Delphi to intercept and process the messages.

First look at the following program. Before executing the program, first change the icon property of the Form1, and load the Form1 with an image, otherwise a blank will appear on the taskbar.

Unit Unit1;
Interface
Uses
Windows, Messages, Sysutils,
Classes, Graphics, Controls, Forms,
Dialogs,shellapi;
Const
wm_baricon=wm_user+200;
Type
TForm1 = Class (Tform)
Private
Procedure Wmsyscommand (Var
Message:tmessage); Message Wm_syscommand;
Procedure Wmbaricon (Var
message:tmessage); message Wm_baricon;
{Private declarations}
Public
{Public declarations}
End
Var
Form1:tform1;
Implementation
{$R *. DFM}
Procedure Tform1.wmsyscommand
(Var message:tmessage);
Var
Lpdata:pnotifyicondata;
Begin
If Message.wparam = Sc_icon Then
Begin
If the user minimizes the window, the window
Hide and add icons to the taskbar
Lpdata: = new (PNOTIFYICONDATAA);
Lpdata.cbsize: = 88;
SizeOf (PNOTIFYICONDATAA);
Lpdata.wnd: = Form1.handle;
Lpdata.hicon: = Form1.Icon.Handle;
Lpdata.ucallbackmessage: = Wm_baricon;
Lpdata.uid: = 0;
Lpdata.sztip: = ' Samples ';
Lpdata.uflags: = Nif_icon
or Nif_message or nif_tip;
Shell_NotifyIcon (Nim_add,lpdata);
Dispose (lpdata);
Form1.visible: = False;
End
Else
Begin
If it's another systemcommand,
The message invokes the system default handler function.
DefWindowProc (form1.handle,message.
Msg,message.wparam,message.lparam);
End
//
End
Procedure Tform1.wmbaricon (var message:tmessage);
Var
Lpdata:pnotifyicondata;
Begin
if (Message.lparam = wm_lbuttondown) Then
Begin
If the user clicks on the taskbar icon, remove the icon and Reply to the window.
Lpdata: = new (PNOTIFYICONDATAA);
Lpdata.cbsize: = 88;//sizeof (PNOTIFYICONDATAA);
Lpdata.wnd: = Form1.handle;
Lpdata.hicon: = Form1.Icon.Handle;
Lpdata.ucallbackmessage: = Wm_baricon;
Lpdata.uid: = 0;
Lpdata.sztip: = ' Samples ';
Lpdata.uflags: = Nif_icon or nif_message or nif_tip;
Shell_NotifyIcon (Nim_delete,lpdata);
Dispose (lpdata);
Form1.visible: = True;
End
End
End.

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.