Solved

Popup menu for system tray icon

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 193
CheckListBox usage 3 82
delphi popmenu non latine charcters 3 35
Problem working with dynamic array - help 2 36
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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