Author: Tomas Rutkauskas
I'm trying to make a component that acts in certain cases like a combobox, i.e.
when the user presses a button, a list box is shown and the user can select an
item. The problem I have is that I need the list to hide itself whenever the user
clicks the mouse outside the list (including clicks in non-windowed controls).
Answer:
One way you can do this is by listening for CM_CANCELMODE messages in the parent of
your drop-down list (presumably an edit control or something similar). You will
probably then have to work out whether the message originated from a click on the
drop-down list or elsewhere. In the code below, FPopup points to the list component:
1 2 procedure TMyPopupEdit.CMCancelMode(varmessage: TCMCancelMode);
3 var4 P: TPoint;
5 R: TRect;
6 begin7 {Get the top-left coordinate of the Sender and see if it is within the popup8 control. If not, close the popup without changing the text in the edit box.}9 P.X := message.Sender.Left;
10 P.Y := message.Sender.Top;
11 R := Rect(0, 0, FPopup.Width, FPopup.Height);
12 ifnot PtInRect(R, P) then13 if FPopupVisible then14 PopupCloseUp(FPopup, False);
15 end;