Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to detect when a TPopupMenu is closed Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
VCL-Menu
Language
Delphi 5.x
Views
99
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Is it possible to know when a popup menu closes? I really need to differentiate 
between when a user selects an item or when the menu disappears because the user 
clicks somewhere else and it loses focus. Is there some message sent to the app 
window once TrackPopupMenu() returns?

Answer:

Solve 1:

There are messages that are send to the window specified as the menus owner in the 
call to TrackPopupMenu. If you are using Delphi 5, add the following unit to your 
project and your form will get the three custom messages defined in the units 
interface:

1   unit ExPopupList;
2   
3   interface
4   
5   uses Controls;
6   
7   const
8     CM_MENUCLOSED = CM_BASE - 1;
9     CM_ENTERMENULOOP = CM_BASE - 2;
10    CM_EXITMENULOOP = CM_BASE - 3;
11  
12  implementation
13  
14  uses Messages, Forms, Menus;
15  
16  type
17    TExPopupList = class(TPopupList)
18    protected
19      procedure WndProc(var message: TMessage); override;
20    end;
21  
22    { TExPopupList }
23  
24  procedure TExPopupList.WndProc(var message: TMessage);
25  
26    procedure Send(msg: Integer);
27    begin
28      if Assigned(Screen.Activeform) then
29        Screen.ActiveForm.Perform(msg, message.wparam, message.lparam);
30    end;
31  
32  begin
33    case message.Msg of
34      WM_ENTERMENULOOP:
35        Send(CM_ENTERMENULOOP);
36      WM_EXITMENULOOP:
37        Send(CM_EXITMENULOOP);
38      WM_MENUSELECT:
39        with TWMMenuSelect(message) do
40          if (Menuflag = $FFFF) and (Menu = 0) then
41            Send(CM_MENUCLOSED);
42    end;
43    inherited;
44  end;
45  
46  initialization
47    Popuplist.Free;
48    PopupList := TExPopupList.Create;
49    {Note: will be freed by Finalization section of Menus unit}
50  
51  end.



Solve 2

The TPopupMenu.Popup method (which is used to display such a menu even when 
presented "automatically" by the VCL) has it's own message pump whilst being 
displayed. i.e. the Popup procedure only returns to the caller when the menu has 
been dismissed.

I used this feature to implement a minor extension to TPopupMenu that not only 
raises an event when the menu has been dismissed, but also peeks in the relevant 
message queue for the presence of a WM_COMMAND message - i.e. was the menu 
dismissed because an item was selected or because the menu was cancelled with no 
item selected. This can then be reflected in the event.

52  { ... }
53  type
54    TIXPopupMenuEvent = procedure(Sender: TObject; Cancelled: Boolean) of object;
55  
56    TIXPopupMenu = class(TPopupMenu)
57    private
58      eOnDismissed: TIXPopupMenuEvent;
59    public
60      procedure Popup(X, Y: Integer); override;
61    published
62      property OnDismissed: TIXPopupMenuEvent read eOnDismissed write eOnDismissed;
63    end;
64  
65  implementation
66  
67  {TIXPopupMenu}
68  
69  procedure TIXPopupMenu.Popup(X, Y: Integer);
70  var
71    msg: tagMSG;
72  begin
73    inherited;
74    if Assigned(OnDismissed) then
75      OnDismissed(Self, PeekMessage(msg, PopupList.Window, WM_COMMAND,
76        WM_COMMAND, PM_NOREMOVE) = FALSE);
77  end;



			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC