yonni
yonni

Reputation: 352

Handling NIN_POPUPOPEN, NIN_POPUPCLOSE message of system tray icon

I want to display a form when the cursor enters the icon, and it disappears shortly after the cursor leaves the icon, similar to the Process Hacker software.

(It displays a form above the system tray that displays information about running applications when the cursor enters the icon, with additional options such as attaching the form that will not disappear, opening settings and more, and the form disappears a few seconds after leaving the icon)

I saw this post

Edit:

I do not need to add these events as properties (like the built-in OnMouseMove, and as in TTrayIconEx), I just want to add a handler procedure that will receive the messages sent from the icon when the cursor hovers over it or leaves it. For example, the messages Remy Lebeau mentioned in his reply (NIN_POPUPOPEN, NIN_POPUPCLOSE)

Is this possible, and how?

updating:

In fact, I used this code as experience:

unit MainFormtest;

interface

uses
  ShellAPI, Windows, Messages, SysUtils, Classes,
  Vcl.Controls, Vcl.Forms, Vcl.ExtCtrls, Vcl.Menus;

type
  TTesterForm = class(TForm)
    DelayHide: TTimer;
    TestPopupMenu: TPopupMenu;
    PMClose: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure DelayHideTimer(Sender: TObject);
    procedure FormMouseEnter(Sender: TObject);
    procedure FormMouseLeave(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure PMCloseClick(Sender: TObject);
    procedure TestPopupMenuPopup(Sender: TObject);
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  public
    TrayIconForTest: TNotifyIconData;
    procedure TrayMouseMessage(var Msg: TMessage); Message WM_SYSTEM_TRAY_MESSAGE;
  end;

var
  TesterForm: TTesterForm;

implementation

{$R *.dfm}

procedure TTesterForm.FormCreate(Sender: TObject);
begin
  with TrayIconForTest do
  begin
    cbSize := TNotifyIconData.SizeOf;
    Wnd := Handle;
    uID := $20;
    uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
    uCallBackMessage := WM_SYSTEM_TRAY_MESSAGE;
    hIcon := Application.Icon.Handle;
    szTip := 'this is test icon';
    uVersion := 4;
    dwInfoFlags := NIIF_USER;
    hBalloonIcon := Application.Icon.Handle;
  end;
  Shell_NotifyICon(NIM_ADD, @TrayIconForTest);
  Shell_NotifyIcon(NIM_SETVERSION, @TrayIconForTest);
end;

procedure TTesterForm.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.ExStyle := Params.ExStyle and not WS_EX_APPWINDOW;
  Params.WndParent := Application.Handle;
end;

procedure TTesterForm.TestPopupMenuPopup(Sender: TObject);
begin
  DelayHide.Enabled := False;
end;

procedure TTesterForm.TrayMouseMessage(var Msg: TMessage);
begin
  case LOWORD(Msg.Lparam) of
    NIN_POPUPOPEN:
    begin
      TesterForm.Show;
      DelayHide.Enabled := False;
      TestPopupMenu.CloseMenu;
    end;
    NIN_POPUPCLOSE:
      DelayHide.Enabled := True;
    WM_RBUTTONDOWN, WM_LBUTTONDOWN:
      TestPopupMenu.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
  end;
end;

procedure TTesterForm.DelayHideTimer(Sender: TObject);
begin
  TesterForm.Hide;
end;

procedure TTesterForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Shell_NotifyIcon(NIM_DELETE, @TrayIconForTest);
  Application.Terminate;
end;

procedure TTesterForm.FormHide(Sender: TObject);
begin
  DelayHide.Enabled := False;
end;

procedure TTesterForm.FormMouseEnter(Sender: TObject);
begin
  DelayHide.Enabled := False;
end;

procedure TTesterForm.FormMouseLeave(Sender: TObject);
begin
  DelayHide.Enabled := True;
end;

procedure TTesterForm.PMCloseClick(Sender: TObject);
begin
  testerForm.Close;
end;

end.

But he suffers from several problems:

Can you direct me to fix the code?

Upvotes: 0

Views: 331

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595369

TTrayIcon, and TTrayIconEx provided in the other post you mention, do not support what you want.

However, the underlying Win32 tray icon API, Shell_NotifyIcon(), does. After adding your icon with NIM_ADD, you have to use NIM_SETVERSION setting NOTIFYICONDATA.uVersion to NOTIFYICON_VERSION_4 or higher to enable the API to send NIN_POPUPOPEN and NIN_POPUPCLOSE notification messages to your tray icon's owner window (note: this works only on Vista+):

  • NIN_POPUPOPEN. Sent when the user hovers the cursor over an icon to indicate that the richer pop-up UI should be used in place of a standard textual tooltip.
  • NIN_POPUPCLOSE. Sent when a cursor no longer hovers over an icon to indicate that the rich pop-up UI should be closed.

UPDATE:

  • The menu does not close when a mouse button is pressed anywhere (so I added TestPopupMenu.CloseMenu to close the menu when the cursor re-enters).

This is a well-known issue (multiple questions on StackOverflow about it), and is covered in the Remarks section of the documentation for the Win32 TrackPopupMenu() function (which TPopupMenu.Popup() calls internally):

To display a context menu for a notification icon, the current window must be the foreground window before the application calls TrackPopupMenu or TrackPopupMenuEx. Otherwise, the menu will not disappear when the user clicks outside of the menu or the window that created the menu (if it is visible). If the current window is a child window, you must set the (top-level) parent window as the foreground window.

However, when the current window is the foreground window, the second time this menu is displayed, it appears and then immediately disappears. To correct this, you must force a task switch to the application that called TrackPopupMenu. This is done by posting a benign message to the window or thread, as shown in the following code sample:

SetForegroundWindow(hDlg);

// Display the menu
TrackPopupMenu(   hSubMenu,
                  TPM_RIGHTBUTTON,
                  pt.x,
                  pt.y,
                  0,
                  hDlg,
                  NULL);

PostMessage(hDlg, WM_NULL, 0, 0);

In this case, hDlg can be the Handle of your TForm, eg:

WM_RBUTTONDOWN, WM_LBUTTONDOWN: begin
  SetForegroundWindow(Handle);
  with Mouse.CursorPos do
    TestPopupMenu.Popup(X, Y);
  PostMessage(Handle, WM_NULL, 0, 0);
end;

Upvotes: 3

Related Questions