Link to home
Start Free TrialLog in
Avatar of bcoleman
bcoleman

asked on

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?
Avatar of dhertzfe
dhertzfe

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
Avatar of bcoleman

ASKER

How and what do I connect this procedure to?
How and what do I connect this procedure to?
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
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.
ASKER CERTIFIED SOLUTION
Avatar of philipleighs
philipleighs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for everones help!!!