?
Solved

Popup menu for system tray icon

Posted on 2001-08-29
5
Medium Priority
?
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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

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.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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