恢复 TDBGrid 中的水平滚动位置

发布于 2024-08-12 23:46:22 字数 1130 浏览 5 评论 0原文

我编写了一个简单的方法来对 TD​​BGrid 中的列进行排序。 如果 Option.RowSelect 设置为 False,则一切正常,但如果 R​​owSelect 为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韵柒 2024-08-19 23:46:22

这是控制最左边一列的方法:

type
  TGridFriend=class(TDBGrid);


procedure TForm1.Button2Click(Sender: TObject);
begin
  // scroll to right by one column
  TGridFriend(DBGrid1).leftCol:=TGridFriend(DBGrid1).leftCol + 1;
end;

here's a way to control what is the leftmost column:

type
  TGridFriend=class(TDBGrid);


procedure TForm1.Button2Click(Sender: TObject);
begin
  // scroll to right by one column
  TGridFriend(DBGrid1).leftCol:=TGridFriend(DBGrid1).leftCol + 1;
end;
还如梦归 2024-08-19 23:46:22

您可能会在这里找到答案:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文