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;