2008. március 9., vasárnap

How to drop a TComboBox up instead of down


Problem/Question/Abstract:

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:

{ ... }
cbxMaxWidth: integer;

procedure pmAdjustDropList(var Msg: TMessage); message WM_USER + 1800;
{ ... }

  procedure TForm1.pmAdjustDropList(var Msg: TMessage); {WM_USER + 1800;}
  var
    LHnd: HWnd;
    rct: TRect;
    pt: TPoint;
    x: integer;
  begin
    x := ComboBox1.Height + 1;
    ComboBox1.Perform(CB_GETDROPPEDCONTROLRECT, 0, longint(@rct));
    pt := Point(rct.Left + 1, rct.Top + x);
    {Gets the handle of the window containing the pt}
    LHnd := WindowFromPoint(pt);
    rct.Left := rct.Right - cbxMaxWidth; {cbxMaxWidth is maximum width for box}
    if rct.Right - rct.Left > ComboBox1.Width then
    begin
      {Up with right side of combobox}
      pt := ComboBox1.ScreenToClient(rct.BottomRight);
      OffsetRect(rct, ComboBox1.Width - (pt.x), x);
      MoveWindow(LHnd, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top, true);
    end;
  end;

  procedure TForm1.ComboBox1DropDown(Sender: TObject);
  begin
    PostMessage(Handle, WM_USER + 1800, 0, 0);
  end;

Nincsenek megjegyzések:

Megjegyzés küldése