Windows Programming/Delphi 6/7

Delphi6/7 TDBGrid - Scroll problem

denisOh 2010. 9. 13. 00:56

델파이에서 기본적으로 제공하는 DBGrid에는 한가지 문제점이 있다.
그리드에서 마우스 스크롤을 한 뒤 원하는 셀을 클릭해보면 원치 않는 행이 선택되는 것을 볼 수 있다.
마우스 스크롤을 아랫쪽으로 스크롤을 하면 실제 커서 포인터는 위로 이동하고,
반대로 마우스 스크롤을 윗쪽으로 스크롤을 하면 실제 커서 포인터는 아래로 이동한다.

이것은 DBGrid가 Visible 부분은 잘 처리하는데, 커서 부분은 잘 처리하지 못하는 문제이다.
따라서 마우스 스크롤시 커서 부분을 잘 처리시켜줘야 한다.

잘 처리하는 방법은 WndProc에 새로운 메시지 처리기를 등록하고, WM_MOUSEWHEEL 쪽을 알맞게 구현하면 된다.
아래 소스처럼 구현 하면 된다. 자세한 설명은 나중에...



출처 : http://www.delphi3000.com/articles/article_3892.asp?SK=DBGrid

type
  TomaInvento = class(TControl);

//So we can, later on, substitute the WndProc of DBGrid1 with an instruction similar to:

  DBGrid1.WindowProc := DBGrid1PillaLaRueda;

//for example in "Oncreate" of our Form
//Here you have the Unit of a form in which I have placed a DBGrid a TTable and a TDataSource to check all the invention


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids, Db, DBTables;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure DBGrid1PillaLaRueda(var Message: TMessage);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

type
  TomaInvento = class(TControl);

procedure TForm1.DBGrid1PillaLaRueda(var Message: TMessage);
var
  Cuanto : short;
begin
  if (Message.Msg = WM_MOUSEWHEEL) then begin
    Cuanto:=HIWORD(Message.WParam);
    Cuanto:=Cuanto div 120;
    DbGrid1.DataSource.DataSet.MoveBy(-Cuanto)
  end else TomaInvento(DBGrid1).WndProc(Message);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGrid1.WindowProc := DBGrid1PillaLaRueda;
end;

end.