Mimic outside click on a popup menu - delphi

You know how in a popup menu if you click outside of the menu the popup disappears? Well I would like to reproduce that.
Currently I am having a panel created with some components on it when a button is clicked. The panel and sub components go away when you click a certain button on the panel. However I want it to go away if you click anywhere outside of the panel.

I think you would need to install and application-wide mouse message hook, and if your panel is visible and the coordinates of the mouse event are outside of your panel bounds, then hide the panel. Also handle the WM_ACTIVATE message in your form.
[Edited: removed answer involving SetCaptureControl() because the bahaviour is not appropriate in this circumstance]

Related

Get the calling component of TPopupMenu

I have a TStringGrid, that has a TPopupMenu connected.
By clicking one event of the popup menu, I would like to get the calling component. Is that possible?
Background:
It is a bigger project, every form has a "BasicForm" I can inherited from. So I would like to provide a "default popup menu" for grids that have stuff like Copy, Select, and so on in it. In the inherited form I only match the grid (if exists) with that popup and I'm done.
Seems you are looking for the PopupComponent property of TPopupMenu:
Vcl.Menus.TPopupMenu.PopupComponent
Indicates the component that last displayed the popup menu in response
to a right mouse click.
Read PopupComponent to determine which control is currently using the
popup menu. In applications where multiple controls share the same
pop-up menu, use PopupComponent to determine which of them displayed
the menu.
Set PopupComponent to associate a control with the menu before calling
the Popup method programmatically to bring up the pop-up menu.

How to have a list of options when clicking a TButton on Firemonkey?

I know that TButton on firemonkey let me add a TPopUp Menu. But it only works when you right click it.
I need make it open right below the button when you do the normal click.
PopupMenu.PopUp(X, Y) is based on the form I believe. How to translate the X,Y of the button (that is deep inside other layouts) to the same coordinate?
And when clicking and the PopUp is shown some weird behavior happens with the selection bar of the popup menu that disappear. The button keeps pressed, that is good.
Look here:
http://blogs.embarcadero.com/sarinadupont/2013/10/17/463/
Is an example for mobile, but you use in desktop too.

Avoid click propagation on Page that is under the current page on a TPageControl

I have a TPageControl that contains five pages and the page shown is alternated setting the ActivePageIndex property in this way:
PageControl1.ActivePageIndex := 4;
the problem is that the page below covered by the page currently shown get click on his buttons while the mouse is pressed on the above page, how can I avoid this behaviour ? How can I avoid the propagation of the click on Pages below the currently shown (that is also the current index)?
The application uses CLX as Graphics library instead of VCL.
Delphi does not do click Propagation.
I would check if the button that gets click are placed on the TabSheet and not on the parent control. Find the button you want in the drop down list of object inspector and press - the selected item will be the parent of the button. Is it a TabSheet?
Or you can add this code as a first line of your button-click-function
ShowMessage(TButton(Sender).Parent.Name);

Why doesn't clicking on my TToolButton show the DropdownMenu?

I'm using Delphi 2010 and I have a TToolButton contained by a TToolBar. Assigned to the 'DropdownMenu' property of my TToolButton is a standard TPopupMenu.
The only way I can get the menu to appear is to click on the area pointed to by the red arrow in the image. Currently, clicking the area pointed to by the green arrow shows the button as pressed, but the dropdown menu does not appear.
What I want is if the user clicks anywhere (pointed to by green arrow or red arrow) for the menu to appear. Is it possible to enable this functionality?
The idea of the tbsDropDown style is to have a button that triggers some default action when clicked, but provides more variations of that action in the drop down menu.
If you don't care about the down arrow disappearing, you can set the style to tbsButton and no matter where the button is clicked, it will show the popup menu.
If you are like me and you do want to have the arrow there to indicate that there are more options behind this button, you can call CheckMenuDropdown in the button's OnClicked event handler.
Set Style to tbsButton. Then you can click anywhere to show the drop-down menu, but you will lose the arrow.

How can I make a menu stay visible after it's clicked?

I'm using Delphi. I have my popup menu ready, and in a submenu I want to click, but I don't want the popup menu to disappear. I want to click and make it stay.
Easy one, disable the item. < g >
Joke aside, not an easy task.
If you want the popup menu to act like a top-most form (that is you can both interact with the items and with controls on your form without deactivating the menu), forget it, it cannot be done with a standard menu.
But if your requirement is as exactly as stated in your question (click an item, and the menu is not closed - click outside and it is closed), with some work, it would be possible.
Your aim would be to subclass the window that your submenu will reside in to override some message handling. First, derive a new class from 'TPopupList' and override its 'WndProc'. Here is an example doing this.
If you intend to prevent the closing of the menu by selecting root items then in PopupList's WndProc you'd handle WM_ENTERIDLE and get the popup menu's window handle from the message's lParam and subclass the popup menu's window.
If you only intend to prevent the closing of the menu by selecting submenu items, then in PopupList's WndProc you'd watch for WM_INITMENUPOPUP messages where the wParam is "not" the handle of the PopupMenu itself, that would mean a submenu is about to be shown. Only then you'd handle WM_ENTERIDLE and get the window handle that the submenu resides in and subclass that window.
After subclassing the window that the popup menu or the popup submenu resides in, you'd intercept an undocumented 'MN_BUTTONDOWN' ($01ED) message and prevent further handling (not call the original window procedure). Probably you'd also want to prevent closing of the menu with the keyboard; you'd watch for WM_KEYDOWN (Enter) and WM_CHAR (accelerator) messages.
Well, would take some work I guess.
I don't know how can you do that but TAdvStickyPopupMenu component (TMS software) can do this.

Resources