Solved

Popup menu for system tray icon

Posted on 2001-08-29
5
401 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now