StringGrid1.Options.goRowSelect := False;

解决方案 »

  1.   

    更好的办法是如下处理TStringGrid.OnSelectCell事件:
    procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
      var pos : integer;
    begin
      pos := GetScrollPos( StringGrid1.Handle, SB_HORZ );
      PostMessage( StringGrid1.Handle, WM_HSCROLL, SB_THUMBPOSITION+( pos SHL 16 ), 0 );
    end;
      

  2.   

    如下,稍有改进:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Grids;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
          var CanSelect: Boolean);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        fCellSel : Boolean;
        OldStringGridwndProc : TWndMethod;
        procedure StringGridWndProc(var Msg : TMessage);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    procedure TForm1.StringGridWndProc(var Msg : TMessage);
    Begin
      If (fCellSel) Then
      Begin
        Msg.Result := 1;
        fCellSel := False;
      End
      Else OldStringGridWndProc( Msg );
    End;procedure TForm1.FormCreate(Sender: TObject);
    begin
      fCellSel := False;
      OldStringGridwndProc := StringGrid1.WindowProc;
      StringGrid1.WindowProc := StringGridWndProc;
    end;procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
      var pos : integer;
          msg : TMsg;
    begin
      pos := GetScrollPos( StringGrid1.Handle, SB_HORZ );
      fCellSel := True;
      PostMessage( StringGrid1.Handle, WM_HSCROLL, SB_THUMBPOSITION+( pos SHL 16 ), 0 );
    end;end.
      

  3.   

    如下,稍有改进:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Grids;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
          var CanSelect: Boolean);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        fCellSel : Boolean;
        OldStringGridwndProc : TWndMethod;
        procedure StringGridWndProc(var Msg : TMessage);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    procedure TForm1.StringGridWndProc(var Msg : TMessage);
    Begin
      If (fCellSel) Then
      Begin
        Msg.Result := 1;
        fCellSel := False;
      End
      Else OldStringGridWndProc( Msg );
    End;procedure TForm1.FormCreate(Sender: TObject);
    begin
      fCellSel := False;
      OldStringGridwndProc := StringGrid1.WindowProc;
      StringGrid1.WindowProc := StringGridWndProc;
    end;procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
      var pos : integer;
          msg : TMsg;
    begin
      pos := GetScrollPos( StringGrid1.Handle, SB_HORZ );
      fCellSel := True;
      PostMessage( StringGrid1.Handle, WM_HSCROLL, SB_THUMBPOSITION+( pos SHL 16 ), 0 );
    end;end.
      

  4.   

    谢谢!
    虽然比以前好一些,但还是有闪烁现象,尤其是在表的维数叫多,个字段不同(有中文,英文,数字),在表的末尾空空白处都很明显能看出闪,滚动条有时也可看到闪回到开始的位置又滚回来。
    我想这可能是OnSelect的事件处理句丙调用时,WinProc已经处理了一些消息,然后才进入fSelCell=true的状态造成的。奈何?