Q&A

  • (Q)두개의 StringGrid 수평스크롤바를 같이 움직이게 하려면...
현재 두개의 StringGrid를 가지고 한개의 StringGrid 수평스크롤바를 움직이면

나머지 StringGrid 수평스크롤바도 함께 움직이게 하고 싶습니다.

어떻게 하면 좋을지 많은 조언 부탁 드립니다.

감사합니다.



1  COMMENTS
  • Profile
    김영대 1999.09.13 23:51
    // 1.먼저 아래 콤포넌트를 설치한다

    unit SyncStringGrid;



    interface



    uses

    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

    Grids;



    type

    TSyncKind = (skBoth, skVScroll, skHScroll);

    TSyncStringGrid = class(TStringGrid)

    private

    FInSync: Boolean;

    FsyncGrid: TSyncStringGrid;

    FSyncKind: TSyncKind;

    { Private declarations }



    procedure WMVScroll( Var Msg: TMessage ); message WM_VSCROLL;

    procedure WMHScroll( Var Msg: TMessage ); message WM_HSCROLL;

    protected

    { Protected declarations }

    public

    { Public declarations }

    procedure DoSync( msg, wparam: Integer; lparam: longint ); virtual;

    published

    { Published declarations }

    property SyncGrid: TSyncStringGrid read FSyncGrid write FSyncGrid;

    property SyncKind: TSyncKind read FSyncKind write FSyncKind default skBoth;

    end;



    procedure Register;



    implementation



    procedure Register;

    begin

    RegisterComponents('Samples', [TSyncStringGrid]);

    end;



    procedure TSyncStringGrid.WMVScroll( Var Msg: TMessage );

    Begin

    If not FInSync and

    Assigned( FSyncGrid ) and

    (FSyncKind In [skBoth, skVScroll])

    Then

    FSyncGrid.DoSync( WM_VSCROLL, msg.wparam, msg.lparam );

    inherited;

    end;



    procedure TSyncStringGrid.WMHScroll( Var Msg: TMessage );

    Begin

    If not FInSync and

    Assigned( FSyncGrid ) and

    (FSyncKind In [skBoth, skHScroll])

    Then

    FSyncGrid.DoSync( WM_HSCROLL, msg.wparam, msg.lparam );

    inherited;

    end;



    procedure TSyncStringGrid.DoSync( msg, wparam: Integer; lparam: longint );

    begin

    FInSync := True;

    Perform( msg, wparam, lparam );

    FinSync := False;

    end;



    end.







    // 2.아래 예제를 실행하여 두 그리드의 scroll을 움직여 본다

    unit Unit1;



    interface



    uses

    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

    Grids, SyncStringGrid;



    type

    TForm1 = class(TForm)

    SyncStringGrid1: TSyncStringGrid;

    SyncStringGrid2: TSyncStringGrid;

    procedure FormActivate(Sender: TObject);

    private

    { Private declarations }

    public

    { Public declarations }

    end;



    var

    Form1: TForm1;



    implementation

    {$R *.DFM}



    procedure TForm1.FormActivate(Sender: TObject);

    var

    i, j: Integer;

    begin

    // StringGrid1의 크기와 값을 임의로 지정한다(예제용)

    SyncStringGrid1.ColCount := 15;

    SyncStringGrid1.RowCount := 20;

    for i := 1 to SyncStringGrid1.ColCount - 1 do

    SyncStringGrid1.Cells[i, 0] := Char(Ord('A')+i-1);

    for i := 1 to SyncStringGrid1.RowCount - 1 do

    SyncStringGrid1.Cells[0, i] := IntToStr(i);;

    for i := 1 to SyncStringGrid1.ColCount - 1 do

    for j := 1 to SyncStringGrid1.RowCount - 1 do

    SyncStringGrid1.Cells[i, j] := Format('%.0n', [i * j * 10000.0]);



    // StringGrid2를 StringGrid1과 동일하게

    SyncStringGrid2.ColCount := SyncStringGrid1.ColCount;

    SyncStringGrid2.RowCount := SyncStringGrid1.RowCount;

    for i := 1 to SyncStringGrid1.ColCount - 1 do

    for j := 1 to SyncStringGrid1.RowCount - 1 do

    SyncStringGrid2.Cells[i, j] := SyncStringGrid1.Cells[i, j];



    // 두 그리드의 scroll 을 동기화 시키도록 상호 지정한다

    SyncStringGrid1.SyncGrid := SyncStringGrid2;

    SyncStringGrid2.SyncGrid := SyncStringGrid1;

    end;



    end.