Ez a példa egy komponens és egy mintaalkalmazás elkészítésén keresztül bemutatja, hogy hogyan
lehet két DBGrid tetszőleges mezői között alkalmazni a Drag & Drop (Fogd és Vidd) technikát. (A
példa a Delphi 3-as és 4-es verziói alatt működik, de egyes kisebb változtatásokkal használható a
Delphi 1-es és 2-es verzióival is.)


Készíts egy új Unit-ot (File/New/Unit). A lenti MyDBGrid unit szövegét másold bele és
mentsd el MyDBGrid.pas néven. Ez lesz az új DBGrid komponens.


Most installáld az új komponenst: Component/Install Component. Válts át az 'Into New
Package' fülre. A Unit neve szerkesztőmezőbe hívd be a MyDBGrid.pas fájlt. Nevezd el
az új komponens-csomagot 'MyPackage.dpk'-nak. Nyomd meg az igen gombot, amikor a Delphi
közli, hogy az új csomag installálva lesz, majd az OK-t, amikor jelzi, hogy a 'VCL30.DPL' szükséges hozzá. Zárd be a csomag
-szerkesztőt és mentsd el a komponens-csomagot.


Készíts egy új alkalmazást: File/New Application. Kattints jobb gombbal a Form-ra (Form1) és
válaszd a gyorsmenüből a 'View As Text' menüpontot. A lenti GridU1 form szöveges forrást másold be
a Form1 forrásába. Most kattints jobb gombbal a Form1 forrásába és válaszd ki a 'View As Form'
menüpontot. Eltarthat egy rövid ideig míg visszavált Form nézetre mert közben meg kell nyitnia az
adatbázis táblákat is. Ezután a lenti GridU1 Unit szövegét másold be az 'Unit1'-be.

Mentsd el az alkalmazást: File/Save Project As. A unitot nevezd el 'GridU1.pas'-nak, az
alkalmazást pedig 'GridProj.dpr'-nek.

Futtasd az alkalmazást és ha minden igaz, máris működni fog a Drag&Drop technika a két DBGrid
mezői között.


-----------------
The MyDBGrid unit
-----------------

unit MyDBGrid;
interface

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

type
     TMyDBGrid = class(TDBGrid)
   private
     { Private declarations }
     FOnMouseDown: TMouseEvent;
   protected
     { Protected declarations }
     procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
   published
     { Published declarations }
   property Row;
   property OnMouseDown read FOnMouseDown write FOnMouseDown;
   end;

procedure Register;
implementation

procedure TMyDBGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   if Assigned(FOnMouseDown) then
     FOnMouseDown(Self, Button, Shift, X, Y);
   inherited MouseDown(Button, Shift, X, Y);
end;

procedure Register;
begin
   RegisterComponents('Samples', [TMyDBGrid]);
end;
end.

---------------
The GridU1 unit
---------------

unit GridU1;

interface

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

type
   TForm1 = class(TForm)
     MyDBGrid1: TMyDBGrid;
     Table1: TTable;
     DataSource1: TDataSource;
     Table2: TTable;
     DataSource2: TDataSource;
     MyDBGrid2: TMyDBGrid;
     procedure MyDBGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
     procedure MyDBGrid1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
     procedure MyDBGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
   private
     { Private declarations }
   public
     { Public declarations }
   end;

var
   Form1: TForm1;

implementation

{$R *.DFM}

var
   SGC : TGridCoord;

procedure TForm1.MyDBGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
   DG : TMyDBGrid;
begin
   DG := Sender as TMyDBGrid;
   SGC := DG.MouseCoord(X,Y);
   if (SGC.X > 0) and (SGC.Y > 0) then
     (Sender as TMyDBGrid).BeginDrag(False);
end;

procedure TForm1.MyDBGrid1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
var
   GC : TGridCoord;
begin
   GC := (Sender as TMyDBGrid).MouseCoord(X,Y);
   Accept := Source is TMyDBGrid and (GC.X > 0) and (GC.Y > 0);
end;

procedure TForm1.MyDBGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
   DG : TMyDBGrid;
   GC : TGridCoord;
   CurRow : Integer;
  begin
     DG := Sender as TMyDBGrid;
     GC := DG.MouseCoord(X,Y);
     with DG.DataSource.DataSet do
     begin
       with (Source as TMyDBGrid).DataSource.DataSet do
       Caption := 'You dragged "'+Fields[SGC.X-1].AsString+'"';
       DisableControls;
       CurRow := DG.Row;
       MoveBy(GC.Y-CurRow);
       Caption := Caption+' to "'+Fields[GC.X-1].AsString+'"';
       MoveBy(CurRow-GC.Y);
       EnableControls;
     end;
    end;
end.

---------------
The GridU1 form
---------------

object Form1: TForm1
   Left = 200
   Top = 108
   Width = 544
   Height = 437
   Caption = 'Form1'
   Font.Charset = DEFAULT_CHARSET
   Font.Color = clWindowText
   Font.Height = -11
   Font.Name = 'MS Sans Serif'
   Font.Style = []
   PixelsPerInch = 96
   TextHeight = 13
  object MyDBGrid1: TMyDBGrid
   Left = 8
   Top = 8
   Width = 521
   Height = 193
   DataSource = DataSource1
   Row = 1
   TabOrder = 0
   TitleFont.Charset = DEFAULT_CHARSET
   TitleFont.Color = clWindowText
   TitleFont.Height = -11
   TitleFont.Name = 'MS Sans Serif'
   TitleFont.Style = []
   OnDragDrop = MyDBGrid1DragDrop
   OnDragOver = MyDBGrid1DragOver
   OnMouseDown = MyDBGrid1MouseDown
   end
  object MyDBGrid2: TMyDBGrid
   Left = 7
   Top = 208
   Width = 521
   Height = 193
   DataSource = DataSource2
   Row = 1
   TabOrder = 1
   TitleFont.Charset = DEFAULT_CHARSET
   TitleFont.Color = clWindowText
   TitleFont.Height = -11
   TitleFont.Name = 'MS Sans Serif'
   TitleFont.Style = []
   OnDragDrop = MyDBGrid1DragDrop
   OnDragOver = MyDBGrid1DragOver
   OnMouseDown = MyDBGrid1MouseDown
  end
  object Table1: TTable
   Active = True
   DatabaseName = 'DBDEMOS'
   TableName = 'ORDERS'
   Left = 104
   Top = 48
  end
  object DataSource1: TDataSource
   DataSet = Table1
   Left = 136
   Top = 48
  end
  object Table2: TTable
   Active = True
   DatabaseName = 'DBDEMOS'
   TableName = 'CUSTOMER'
   Left = 104
   Top = 240
  end
  object DataSource2: TDataSource
   DataSet = Table2
   Left = 136
   Top = 240
  end
end