Author: Tomas Rutkauskas
I have been able to find out how to increase the width of a combo box drop down so
that it is wide enough to read the text. However if my combo box is positioned on
the right hand side of a form when there is a particularly wide list the scroll bar
and the list gets cut off on the edge of the screen. Is there a way to change the
position the dropdown list?
Answer:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, stdctrls, Unit2;
8
9 type
10 TForm1 = class(TForm)
11 Button1: TButton;
12 Memo1: TMemo;
13 ComboBox1: TComboBox;
14 procedure Button1Click(Sender: TObject);
15 procedure ComboBox1DropDown(Sender: TObject);
16 private
17 { Private declarations }
18 procedure WMUser(var msg: TMessage); message WM_USER;
19 public
20 { Public declarations }
21 end;
22
23 var
24 Form1: TForm1;
25
26 implementation
27
28 {$R *.dfm}
29
30 procedure TForm1.Button1Click(Sender: TObject);
31 var
32 i: Integer;
33 begin
34 for i := 0 to 20 do
35 combobox1.Items.add(StringofChar(Chr(Ord('A') + i), Random(50) + 10));
36 combobox1.Perform(CB_SETDROPPEDWIDTH, combobox1.Width * 2, 0)
37 end;
38
39 function EnumProc(wnd: HWND; var wndresult: HWND): BOOL; stdcall;
40 var
41 classname: array[0..63] of Char;
42 begin
43 Result := True;
44 GetClassname(wnd, classname, sizeof(classname));
45 if SameText(classname, 'ComboLBox') then
46 begin
47 Result := false;
48 wndresult := wnd;
49 end;
50 end;
51
52 procedure TForm1.ComboBox1DropDown(Sender: TObject);
53 var
54 wnd: HWND;
55 r: Trect;
56 w: Integer;
57 begin
58 wnd := 0;
59 EnumThreadWindows(GetCurrentThreadID, @EnumProc, integer(@wnd));
60 if wnd <> 0 then
61 begin
62 PostMessage(handle, WM_USER, wnd, 0);
63 end
64 else
65 memo1.lines.add('Window not found');
66 end;
67
68 procedure TForm1.WMUser(var msg: TMessage);
69 var
70 wnd: HWND;
71 r: Trect;
72 w: Integer;
73 begin
74 wnd := msg.wparam;
75 GetWindowRect(wnd, r);
76 if r.Right > Screen.width then
77 begin
78 w := r.right - r.Left;
79 MoveWindow(wnd, Screen.Width - w, r.top, w, r.Bottom - r.Top, true);
80 end;
81 memo1.lines.add(format('Wnd: %x, r: (%d,%d,%d,%d)', [wnd, r.left, r.top, r.right,
82 r.bottom]));
83 end;
84
85 initialization
86 randomize;
87 end.
|