Einzelnen Beitrag anzeigen

DCoderHH

Registriert seit: 4. Feb 2015
Ort: Hamburg
84 Beiträge
 
Delphi 10 Seattle Professional
 
#1

Eigene Combobox-Komponente reagiert nicht auf Klicks

  Alt 12. Feb 2016, 14:33
Hallo,

ich habe mir eine Art Combobox-Komponente mit einer TForm-Ableitung nachgebaut:

Das Form enthält eine Listbox und ein Edit. Das Edit soll später dazu da sein, um die Listbox zu filtern (das ist kein Problem). Das Form soll angezeigt werden, wenn z.B. auf ein Label geklickt wird. Also ähnlich wie die Liste, die bei einer Combobox aufklappt. Wenn in der Listbox ein Eintrag ausgewählt wird oder neben das Form geklickt wird, soll das Form geschlossen werden. Auch wenn die Anwenung den Fokus verliert.

Meine Lösung dazu ist angehängt (Alles im ZIP inkl. Mini-Testprojekt). Das Anzeigen und Schließen des Forms funktioniert. Leider reagiert das Form dann aber nicht mehr auf Mausklicks. D.h. ins Edit kann nichts eingegeben werden und in der Listbox nichts angeklickt werden. Ich habe keine Ahnugn warum. Danke für eure Hilfe!

Delphi-Quellcode:
unit MyListbox;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TMyListboxDlg = class(TForm)
    Edit1: TEdit;
    ListBox1: TListBox;
    Shape1: TShape;
    procedure FormCreate(Sender: TObject);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ListBox1Click(Sender: TObject);
  private
    FOnSelect: TNotifyEvent;
    FSelectedStr: string;
  public
    procedure Execute(ShowAtControl: TControl);

    property OnSelect: TNotifyEvent read FOnSelect write FOnSelect;
    property SelectedStr: string read FSelectedStr;
  end;

var
  MyListboxDlg: TMyListboxDlg;

implementation

{$R *.dfm}


procedure TMyListboxDlg.Execute(ShowAtControl: TControl);
var
  P: TPoint;
begin

  Winapi.Windows.SetParent(Handle, GetDesktopWindow);

  // WS_EX_TOOLWINDOW
  // Window with a thin caption. Does not appear in the taskbar or in the Alt-Tab palette. WS_CAPTION also must be specified.
  SetWindowLong(Handle, GWL_EXSTYLE, (GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW));
  // And NOT
  // WS_THICKFRAME
  // Window with no Border.
  SetWindowLong(Handle, GWL_STYLE, (GetWindowLong(Handle, GWL_STYLE) and not WS_THICKFRAME));
  // And NOT
  // WS_CAPTION
  // Window that has a title bar (automatically includes the WS_BORDER if no other border style is specified).
  SetWindowLong(Handle, GWL_STYLE, (GetWindowLong(Handle, GWL_STYLE) and not WS_Caption));


  P := ShowAtControl.Parent.ClientToScreen(Point(ShowAtControl.Left,
    ShowAtControl.Top + ShowAtControl.Height));

  Left := P.X;
  Top := P.Y;

  SetCaptureControl(self);
 //SetCapture(self.Handle);


  Show;
end;

procedure TMyListboxDlg.FormCreate(Sender: TObject);
begin
  DefaultMonitor := dmDesktop;
  FormStyle := fsStayOnTop;
end;

procedure TMyListboxDlg.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  Pnt: TPoint;
  Rct: TRect;
begin
  inherited;

  GetCursorPos(Pnt);
  GetWindowRect(self.Handle, Rct);

  if not PtInRect(Rct, Pnt) then
    Free
  else
  begin
    //SendMessage(ListBox1.Handle, WM_LBUTTONDOWN, X, Y);
    SetCapture(self.Handle);
    // lbStrings.SetFocus;
  end;
end;

procedure TMyListboxDlg.ListBox1Click(Sender: TObject);
begin
  if Assigned(FOnSelect) and (ListBox1.ItemIndex > -1) then
  begin
    FSelectedStr := ListBox1.Items[ListBox1.ItemIndex];
    FOnSelect(self);
  end;
end;

// OnDeactivate := DoCloseDropDown;

end.


Hier der Aufruf im Testprojekt:

procedure TForm1.lblTestClick(Sender: TObject);
var
  Listbox: TMyListboxDlg;
begin

  Listbox := TMyListboxDlg.Create(nil);
  Listbox.Parent := self;
  Listbox.OnSelect := ListboxSelect;
  Listbox.Execute(lblTest);

end;
Angehängte Dateien
Dateityp: zip TestMyListbox.zip (53,5 KB, 12x aufgerufen)
  Mit Zitat antworten Zitat