创建一个从 TListbox 派生的组件并处理 scollbar 的显示。示例代码就像插入的类一样。
外观可以通过设计和自己的风格进行调整(使用较新的 Delphi 版本)。
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TListBox = Class(StdCtrls.TListBox)
Constructor Create(AOwner: TComponent); override;
private
FHiddenScrollbar: Boolean;
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure WMMouseMove(var Message: TWMMouseMove); message WM_MOUSEMOVE;
published
public
Property HiddenScrollbar: Boolean Read FHiddenScrollbar;
End;
TForm3 = class(TForm)
ListBox1: TListBox;
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
{ TListBox }
constructor TListBox.Create;
begin
inherited;
FHiddenScrollbar := true;
end;
procedure TListBox.CreateParams(var Params: TCreateParams);
VAR
Style: Integer;
BEGIN
inherited;
if FHiddenScrollbar then
Params.Style := Params.Style AND not WS_VSCROLL
else
Params.Style := Params.Style or WS_VSCROLL;
end;
procedure TListBox.WMMouseMove(var Message: TWMMouseMove);
var
p: TPoint;
begin
inherited;
GetCursorPos(p);
p := ScreenToClient(p);
if p.X > (Width - 20) then
begin
if FHiddenScrollbar then
begin
FHiddenScrollbar := false;
RecreateWnd;
end;
end
else
begin
if not FHiddenScrollbar then
begin
FHiddenScrollbar := true;
RecreateWnd;
end;
end;
end;
end.