Author: Tomas Rutkauskas
You know how when you open a TComboBox that is near the bottom of the physical
screen, Windows places the list above the ComboBox rather than below. Is there any
way in Delphi to force that behavior? In other words, I'm trying to come up with a
drop-up TComboBox.
Answer:
Here is some code that may help you:
1 { ... }
2 cbxMaxWidth: integer;
3
4 procedure pmAdjustDropList(var Msg: TMessage); message WM_USER + 1800;
5 { ... }
6
7 procedure TForm1.pmAdjustDropList(var Msg: TMessage); {WM_USER + 1800;}
8 var
9 LHnd: HWnd;
10 rct: TRect;
11 pt: TPoint;
12 x: integer;
13 begin
14 x := ComboBox1.Height + 1;
15 ComboBox1.Perform(CB_GETDROPPEDCONTROLRECT, 0, longint(@rct));
16 pt := Point(rct.Left + 1, rct.Top + x);
17 {Gets the handle of the window containing the pt}
18 LHnd := WindowFromPoint(pt);
19 rct.Left := rct.Right - cbxMaxWidth; {cbxMaxWidth is maximum width for box}
20 if rct.Right - rct.Left > ComboBox1.Width then
21 begin
22 {Up with right side of combobox}
23 pt := ComboBox1.ScreenToClient(rct.BottomRight);
24 OffsetRect(rct, ComboBox1.Width - (pt.x), x);
25 MoveWindow(LHnd, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom -
26 rct.Top, true);
27 end;
28 end;
29
30 procedure TForm1.ComboBox1DropDown(Sender: TObject);
31 begin
32 PostMessage(Handle, WM_USER + 1800, 0, 0);
33 end;
|