-2

On a Form, I have TrayIcon and PopupMenu components.

  • the PopupMenu is assigned to the Form and TrayIcon.
  • in the TrayIcon's OnClick event, I show the Form.
  • the PopupMenu has 2 items to show/hide the Form.

When I run the project, the Form shows ok. Right-click on the Form, the PopupMenu appears.

On TrayIcon, left-click shows the Form ok.

On TrayIcon, right-click shows the PopupMenu. Select the "SHOW" item, the Form shows ok.

But, after this, the PopupMenu is not enabled anymore. Right-clicking does not work!

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Gu.
  • 1,947
  • 4
  • 30
  • 49
  • What does `c) nom. a)` mean? – James L. Feb 22 '17 at 19:51
  • sor., bad en.: = repeat a) step on top list – Gu. Feb 22 '17 at 19:56
  • if anyone of the senior reset to correct the text - I will not mind. full description in the ru thread http://delphimaster.ru/cgi-bin/forum.pl?id=1487777210&n=18 but there, too, the answer is no – Gu. Feb 22 '17 at 20:02
  • 1
    Ask in Russian on the Russian SO. This is incomprehensible. – David Heffernan Feb 22 '17 at 20:06
  • More quick repro: 1) Run 2) right click on tray icon 3) right click on form - no popup menu. BTW the form is enabled fine, just the popup does not pop. – Sertac Akyuz Feb 22 '17 at 20:09
  • google translate:2 David. know. There to help me could not. Such was not earlier, I assume a glitch of the compiler. But I want to know your opinion. Because here wrote. Or are You, as soon as you meet "RU" goodbye? + In Russia tomorrow is a holiday (50 minutes) to it. I congratulate you with his attack :) – Gu. Feb 22 '17 at 20:14
  • 1
    AutoPopup becomes false after the trayicon launches the menu. – Sertac Akyuz Feb 22 '17 at 20:15
  • AutoPopup on Form I change 0-2 (no,auto,,,) - not changing. – Gu. Feb 22 '17 at 20:18
  • @Gu - I commented the reason for the behavior you observe. That's what the VCL does, not you. – Sertac Akyuz Feb 22 '17 at 20:20
  • Related: [Delphi “Tray” icon (NotifyIcon) context-menu does not disappear when clicking outside it](http://stackoverflow.com/questions/1188905/) – Remy Lebeau Feb 22 '17 at 20:28
  • Thanks to whoever corrected the text. a happy holiday. ++ the project details will soon give the (source) – Gu. Feb 22 '17 at 20:35
  • I need some time now to give the source... – Gu. Feb 22 '17 at 20:41
  • project full source http://rgho.st/7h85tzVhq http://my-files.ru/kh6wjc http://dropmefiles.com/WLIUi http://sendfile.su/1311486 VIRT https://www.virustotal.com/en/file/f4d9cc60d980f2fbb5abd6e4e022e5ff49e7b41055db82fcdbbdc9629f61fcd7/analysis/1487796543/ SCR my D http://rgho.st/6WzJT8hsw – Gu. Feb 22 '17 at 21:14
  • I add to comment top full source on problem topic (any filehosting for fileles and image). If need - adm< add to main text. snx. – Gu. Feb 22 '17 at 21:21
  • 2 Remy. You helped me, I you some kind of a "plus" this site" sign to add? I did not find what.. If need - write me, from my profile. – Gu. Feb 22 '17 at 21:49
  • 2 Remy 2 :) the problem is not yet solved (I think all the bugs to the developer), but thanks for the help again. – Gu. Feb 22 '17 at 21:56

3 Answers3

3

Displaying a PopupMenu from a tray icon is a little tricky. There is actually a well-known issue in Windows itself that causes problems, and it is even documented in MSDN:

TrackPopupMenu function

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);

To account for this in Delphi, you can set the PopupMenu.AutoPopup property to false and then call the PopupMenu.Popup() method when needed, eg:

procedure TForm1.FormContextPopup(Sender: TObject);
begin
  ShowPopup;
end;

procedure TForm1.TrayIcon1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then ShowPopup;
end;

procedure TForm1.ShowPopup;
begin
  BringToFront;
  with Mouse.CursorPos do
    PopupMenu1.Popup(X, Y);
  PostMessage(Handle, WM_NULL, 0, 0);
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

I observed the PopUpMenu's "AutoPopup" property was false on the show event; setting it back restored expected behavior:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    Show1: TMenuItem;
    Hide1: TMenuItem;
    TrayIcon1: TTrayIcon;
    procedure Show1Click(Sender: TObject);
    procedure Hide1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Hide1Click(Sender: TObject);

begin
   Hide;
   TrayIcon1.Visible := true;
end;

procedure TForm1.Show1Click(Sender: TObject);

begin
   ///////////////////
   //
   // Comment out this line and app will have OP's observed behavior
   Popupmenu.AutoPopup := true;
   ///////////////////

   Show;
end;

end.
Dave Olson
  • 1,435
  • 1
  • 9
  • 16
  • It's the WM_RBUTTONUP handler. And there's a nice ProcessMessages preceding it. Very good code! – Sertac Akyuz Feb 22 '17 at 20:30
  • I add "Popupmenu.AutoPopup := true;", and form1.popupmenu:= popupmenu1, to OnShow, OnClik to show, not working :( – Gu. Feb 22 '17 at 21:39
  • main. I work D7-Xe-10, it - easy code conflict, for simply D users. at this - ??? how D10.1 bug? – Gu. Feb 22 '17 at 21:42
  • @Gu - re: your comment "not working": I'd suggest you post some code to show us all what is not working; clearly you have some other things going on in your code. – Dave Olson Feb 22 '17 at 21:46
  • @Gu - re: your comment "main. I work D7-Xe-10, it - easy..." - This is very nearly unintelligible to me - I guest you are trying to tell us your Delphi version? Or that it worked in some versions? Sorry, this language barrier is really thick. – Dave Olson Feb 22 '17 at 21:49
1

I had the same problem when tried what you did, It seems that the value of form.popupmenu becomes nil when you hide the form, my solution was to add another popupmenu with the same event handlers, assign the first one to the form and the second to the tray icon and it will work.

edit

Sertac Akyuz thanks for the note The Popupmenu.AutoPopup becomes false when hiding the form and not form.popupmenu becomes nil

Nasreddine Galfout
  • 2,550
  • 2
  • 18
  • 36