恢复 TDBGrid 中的水平滚动位置
我编写了一个简单的方法来对 TDBGrid 中的列进行排序。 如果 Option.RowSelect 设置为 False,则一切正常,但如果 RowSelect 为 True,则水平位置滚动在排序列后不会恢复。 所以我尝试 GetScrollPos 和 SetScrollPos 来恢复水平滚动位置,ScrollBar 转到正确的位置,但 TDBGrid 没有滚动,方法如下:
procedure TDBGrid.TitleClick(Column: TColumn);
var
CurrenctPosition: TBookmark;
PosScroll: Integer;
begin
inherited TitleClick(Column);
if FAllowTitleClick and (Assigned(DataSource))
and (Assigned(DataSource.DataSet))
and (DataSource.DataSet.Active)
and (Assigned(Column.Field))
and (Column.Field.FieldKind <> fkLookup) then
begin
//Get position scroll
PosScroll := GetScrollPos(Handle, SB_HORZ);
CurrenctPosition := DataSource.DataSet.GetBookmark;
FPaintInfo.ColPressed := False;
FPaintInfo.ColPressedIdx := -1;
if ValidCell(FCell) then
InvalidateCell(FCell.X, FCell.Y);
SortColumn(Column);
DataSource.DataSet.GotoBookmark(CurrenctPosition);
//Set position scroll
SetScrollPos(Handle, SB_HORZ, PosScroll, True);//<- need to be refreshed
end;
end;
这可能可以使用循环中的 Perform(WM_HSCROLL, SB_LINERIGHT, 0) 来修复,但不是好主意。 有人有更好的解决方案吗?
I wrote a simple method to sort column in TDBGrid.
If Option.RowSelect set to False everything works fine, but if RowSelect gets True the horizontal position scroll doesn't restore after sort column.
So I try GetScrollPos and SetScrollPos to restore horizontal Scroll position, the ScrollBar goes to the right position but TDBGrid didn't scroll, here is the method:
procedure TDBGrid.TitleClick(Column: TColumn);
var
CurrenctPosition: TBookmark;
PosScroll: Integer;
begin
inherited TitleClick(Column);
if FAllowTitleClick and (Assigned(DataSource))
and (Assigned(DataSource.DataSet))
and (DataSource.DataSet.Active)
and (Assigned(Column.Field))
and (Column.Field.FieldKind <> fkLookup) then
begin
//Get position scroll
PosScroll := GetScrollPos(Handle, SB_HORZ);
CurrenctPosition := DataSource.DataSet.GetBookmark;
FPaintInfo.ColPressed := False;
FPaintInfo.ColPressedIdx := -1;
if ValidCell(FCell) then
InvalidateCell(FCell.X, FCell.Y);
SortColumn(Column);
DataSource.DataSet.GotoBookmark(CurrenctPosition);
//Set position scroll
SetScrollPos(Handle, SB_HORZ, PosScroll, True);//<- need to be refreshed
end;
end;
This can maybe fixed using Perform(WM_HSCROLL, SB_LINERIGHT, 0) in loop but isn't good idea.
Anybody have better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是控制最左边一列的方法:
here's a way to control what is the leftmost column:
您可能会在这里找到答案:
http://www.species.net/Aves/Cassowary /delphi.htm
在文本中查找“SetScrollPos”。
也许ModifyScrollBar(Code, SB_THUMBPOSITION, Value) 可以解决这个问题。
You might find an answer here:
http://www.species.net/Aves/Cassowary/delphi.htm
Look for "SetScrollPos" in the text.
Maybe ModifyScrollBar(Code, SB_THUMBPOSITION, Value) holds the solution.