Solved

Popup menu for system tray icon

Posted on 2001-08-29
5
404 Views
Last Modified: 2010-04-06
The problem is that the popup menu dont desappear
after left mouse button clicked on the another app.

I use following code for showing the popup menu:

procedure TMainForm.WMAPPIcon(var Message: TMessage);
var
  pt: TPoint;
begin
  case Message.LParam of
    WM_RBUTTONUP:
      begin
        GetCursorPos(pt);
        PopupMenuIcon.Popup(pt.x, pt.y);
      end;
    WM_LBUTTONDBLCLK:
      begin
        Application.Restore;
        SetForegroundWindow(Handle);
      end;
  end;
end;

0
Comment
Question by:alx512
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:smurff
ID: 6436249
I think the answer is callbacks. try this it works for me.
regards
Smurff


unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, Menus,ShellApi;


const
 WM_MYTRAYICONCALLBACK = WM_USER + 1000;

type
 TForm1 = class(TForm)
   PopupMenu1 : TPopupMenu;
   Close1     : TMenuItem;
   Restore1   : TMenuItem;
   procedure Close1Click(Sender: TObject);
   procedure Restore1Click(Sender: TObject);
   procedure FormCreate(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
 private
   { Private declarations }
   MyTrayIcon: TNotifyIconData;
   procedure AppMinimize(Sender:TObject);
   procedure WMMyTrayIconCallback(var Msg:TMessage);
                      message WM_MYTRAYICONCALLBACK;
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 Application.OnMinimize := AppMinimize;
 ShowWindow(Application.Handle, SW_HIDE);
 MyTrayIcon.cbSize := SizeOf(TNotifyIconData);
 MyTrayIcon.Wnd    := Handle;
 MyTrayIcon.uId    := 1;
 MyTrayIcon.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE;
 MyTrayIcon.uCallBackMessage := WM_MYTRAYICONCALLBACK;
 MyTrayIcon.hIcon  := Application.Icon.Handle;
 MyTrayIcon.szTip  := 'Click on Icon to see the options';
 Shell_NotifyIcon(NIM_ADD, @MyTrayIcon);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 Shell_NotifyIcon(NIM_DELETE, @MyTrayIcon);
 Application.Terminate;
end;

procedure TForm1.AppMinimize(Sender:TObject);
begin
 Application.Minimize;
 ShowWindow(Application.Handle, SW_HIDE);
end;

procedure TForm1.WMMyTrayIconCallback(var Msg:TMessage);
var
 CursorPos : TPoint;
begin
 case Msg.lParam of
   WM_LBUTTONDOWN :
     begin
       GetCursorPos(CursorPos);
       PopupMenu1.Popup(CursorPos.X,CursorPos.Y);
     end;
 end;
end;

procedure TForm1.Close1Click(Sender: TObject);
begin
 Shell_NotifyIcon(NIM_DELETE, @MyTrayIcon);
 Application.Terminate;
end;

procedure TForm1.Restore1Click(Sender: TObject);
begin
 Application.Restore;
 ShowWindow(Application.Handle, SW_Show);
end;

end.
0
 
LVL 1

Author Comment

by:alx512
ID: 6436318
This answer is not what I nead.

I ask  how i can cancel popup menu whithowt
selecting any menu item.
0
 
LVL 6

Accepted Solution

by:
MannSoft earned 50 total points
ID: 6436658
Here is the code I use, and I think it should do what you want (it seems to work here anyway).

WM_RBUTTONDOWN:
begin
  if Assigned(FOnRightClick) then
    FOnRightClick(Self)
  else
    if Assigned(FPopupMenu) then
    begin
      SetForegroundWindow(FForm.Handle);
      GetCursorPos(Mouse);
      FPopupMenu.PopUp(Mouse.X, Mouse.Y);
      PostMessage(FForm.Handle, WM_NULL, 0, 0);
    end;
end;

FForm is type TForm and is assigned a value in the components create method like: FForm := (AOwner as TForm);
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 6436668
Of course that was a portion of my CASE statement in the components custom WindowProc.  Sorry, I have a habit of not making everything clear and causing confusion sometimes :)
0
 
LVL 1

Author Comment

by:alx512
ID: 6436699
You are right MannSoft. I read this code in MSDN yet.
the remark for TrackPopupMenu function discribe this problem.  All work good.

Thanks for your help.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question