它工作正常(对于我写的所有代码),按预期显示内容(我甚至轻松添加了一个水平滚动条),但我想我已经达到了垂直滚动条的某种限制.
也就是说,当我从顶部向底部滚动垂直条时,它不会将内容滚动到列表的末尾,而只会滚动到某个限制.
你知道有没有摆脱这个限制的可能性?我试过SetScrollInfo
,但它没有工作,因为限制听起来不在滚动条中,而是在TListBox
自身中.
我知道创建专用的解决方案TCustomControl
:在这种情况下,SetScrollInfo
将按预期工作.但有人知道仍然使用的解决方案/技巧TListBox
吗?
编辑:说清楚 - 我不要求(第三方)组件解决方案,但要知道是否有一些低级GDI消息要发送到标准TListBox
以覆盖此限制.如果没有,我将转到专用TCustomControl
解决方案.
这是使用TSCROLLINFO的代码:
procedure ScrollVertHuge(Handle: HWND; count: integer);
var Scroll: TSCROLLINFO;
begin
Scroll.cbSize:= sizeof(Scroll);
Scroll.fMask := SIF_DISABLENOSCROLL or SIF_RANGE;
Scroll.nMin := 0;
Scroll.nMax := count;
SetScrollInfo(Handle,SB_VERT,Scroll,false);
end;
Run Code Online (Sandbox Code Playgroud)
要精确地解决这个问题:添加和绘制两个工作当然(我的工具按预期工作),但不起作用的是垂直滚动条拖动.我重命名了问题的标题,并删除了令人困惑的已弃用的MSDN文章.
下面的内容可能应该被认为是有缺陷的操作系统行为的解决方法,因为除非启用了主题,否则列表框控件的默认窗口过程可以很好地处理拇指跟踪.出于某种原因,当启用主题时(此处测试显示的是Vista及更高版本),控件似乎依赖于Word大小的滚动位置数据WM_VSCROLL
.
首先,一个复制问题的简单项目,下面是一个拥有lbVirtualOwnerDraw
大约600,000个项目的所有者绘制虚拟()列表框(因为项目数据没有被缓存,所以不需要花点时间来填充该框).一个高大的列表框将很容易跟踪行为:
type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure ListBox1Data(Control: TWinControl; Index: Integer;
var Data: string);
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure FormCreate(Sender: TObject);
end;
[...]
procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Count := 600000;
end;
procedure TForm1.ListBox1Data(Control: TWinControl; Index: Integer;
var Data: string);
begin
Data := IntToStr(Index) + ' listbox item number';
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
// just simple drawing to be able to clearly see the items
if odSelected in State then begin
ListBox1.Canvas.Brush.Color := clHighlight;
ListBox1.Canvas.Font.Color := clHighlightText;
end;
ListBox1.Canvas.FillRect(Rect);
ListBox1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[Index]);
end;
Run Code Online (Sandbox Code Playgroud)
要查看问题只是通过拇指跟踪滚动条,您将会注意到项目是如何包装的,从每个65536开始,如Arnaud在问题评论中所述.当您松开拇指时,它会捕捉到顶部的项目High(Word)
.
下面的解决方法拦截WM_VSCROLL
控件并手动执行拇指和项目定位.为简单起见,该示例使用内插器类,但任何其他子类方法都可以:
type
TListBox = class(stdctrls.TListBox)
private
procedure WMVScroll(var Msg: TWMVScroll); message WM_VSCROLL;
end;
[...]
procedure TListBox.WMVScroll(var Msg: TWMVScroll);
var
Info: TScrollInfo;
begin
// do not intervene when themes are disabled
if ThemeServices.ThemesEnabled then begin
Msg.Result := 0;
case Msg.ScrollCode of
SB_THUMBPOSITION: Exit; // Nothing to do, thumb is already tracked
SB_THUMBTRACK:
begin
ZeroMemory(@Info, SizeOf(Info));
Info.cbSize := SizeOf(Info);
Info.fMask := SIF_POS or SIF_TRACKPOS;
if GetScrollInfo(Handle, SB_VERT, Info) and
(Info.nTrackPos <> Info.nPos) then
TopIndex := TopIndex + Info.nTrackPos - Info.nPos;
end;
else
inherited;
end;
end else
inherited;
end;
Run Code Online (Sandbox Code Playgroud)