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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
bcolemanAuthor Commented:
How and what do I connect this procedure to?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
philipleighsCommented:
There is an easier way.

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

    SetForeGroundWindow(Application.Handle);

Cheers,
Phil.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bcolemanAuthor Commented:
Thanks for everones help!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.