Closing a TPopupMenu used with TTrayIcon

I am using a TTrayIcon with a TPopupMenu and when I right click on the trayicon my popup menu comes up like it is supposed to, but when I click off it.  I click on some other part of the screen.  I want it to go away like so many other applications do.  Is there a way to do this?
LVL 1
bcolemanAsked:
Who is Participating?
 
philipleighsConnect With a Mentor Commented:
There is an easier way.

In the OnPopup event of the popup menu, type this:

    SetForeGroundWindow(Application.Handle);

Cheers,
Phil.

0
 
dhertzfeCommented:
Hi,

Try adding this procedure to your form.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI,
  Menus, StdCtrls, ExtCtrls;

type
  TfrmMain = class(TForm)
    PopupMenu1: TPopupMenu;
  private
    ResDLLMod:    THandle;
  public
      procedure IconCallBackMessage( var Mess : TMessage ); message WM_USER + 100;
  end;




procedure TfrmView.IconCallBackMessage( var Mess : TMessage );
var pt:  TPoint;
   sEventLog : String;
begin
   case Mess.lParam of
      WM_LBUTTONDBLCLK : sEventLog := 'Left Double Click';
      WM_LBUTTONDOWN :  begin
                           sEventLog := 'Left Down';
                           if (clickFlg = true) then
                           begin
                              clickFlg := false;
                              PopupMenu1.Free;
                           end;
                        end;
      WM_LBUTTONUP : sEventLog := 'Left Up';
      WM_MBUTTONDBLCLK : sEventLog := 'M Dbl';
      WM_MBUTTONDOWN : sEventLog := 'M D';
      WM_MBUTTONUP : sEventLog := 'M U';
      WM_MOUSEMOVE : sEventLog := 'movement';
      WM_MOUSEWHEEL : sEventLog := 'Wheel';
      WM_RBUTTONDBLCLK : sEventLog := 'r dbl';
      WM_RBUTTONDOWN :  begin
                           clickFlg := true;
                           GetCursorPos(pt);
                           SetForegroundWindow(frmMain.Handle);
                           PopupMenu1.Popup(pt.x, pt.y);
                           sEventLog := 'r down';
                        end;
      WM_RBUTTONUP : sEventLog := 'r up';
   end;
end;

====================================
Chad
0
 
bcolemanAuthor Commented:
How and what do I connect this procedure to?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bcolemanAuthor Commented:
How and what do I connect this procedure to?
0
 
dhertzfeCommented:
Connect the procedure to your form.

My example shows:

TfrmView.IconCallBackMessage( var Mess : TMessage );

   should read

TfrmMain.IconCallBackMessage( var Mess : TMessage );
 

Sorry for the mixup.

Chad
0
 
bcolemanAuthor Commented:
I added the procedure and it is never involked.  I added application.messagebox to see if the procedure was ran, and it never came up.
0
 
bcolemanAuthor Commented:
Thanks for everones help!!!
0
All Courses

From novice to tech pro — start learning today.