我想在DBGrid中把一条记录拖到别的容器中,在ListBox中我做过,在ListBox的onMouseDown中用以下语句开始:
if (Sender=ListBox) and (Button=mbLeft) then
begin
ListBox.BeginDrag(false);
end;
但是在DBGrid就不行,因为DBGrid在记录的地方不能激活onMouseDown事件,只有在空白的地方才能激活,这样我永远也选不到我要拖走的那条记录。
if (Sender=ListBox) and (Button=mbLeft) then
begin
ListBox.BeginDrag(false);
end;
但是在DBGrid就不行,因为DBGrid在记录的地方不能激活onMouseDown事件,只有在空白的地方才能激活,这样我永远也选不到我要拖走的那条记录。
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
ApplicationEvents1: TApplicationEvents;
Edit1: TEdit;
procedure Edit1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure Edit1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Edit1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := (Source is TDBGrid) and (Assigned(TDBGrid(Source).DataSource)) and
Assigned(TDBGrid(Source).DataSource.DataSet) and
TDBGrid(Source).DataSource.DataSet.Active;
end;procedure TForm1.Edit1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
TEdit(Sender).Text := TDBGrid(Source).DataSource.DataSet.Fields[0].AsString;
end;procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if Msg.hwnd <> DBGrid1.Handle then Exit;
case Msg.message of
WM_LBUTTONDOWN: begin
DBGrid1.Perform(Msg.message, Msg.wParam, Msg.lParam);
DBGrid1.BeginDrag(Mouse.DragImmediate, Mouse.DragThreshold);
Handled := True;
end;
end;
end;
procedure TForm1.Edit1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := (Source is TDBGrid) and (Assigned(TDBGrid(Source).DataSource)) and
Assigned(TDBGrid(Source).DataSource.DataSet) and
TDBGrid(Source).DataSource.DataSet.Active;
end;procedure TForm1.Edit1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
TEdit(Sender).Text := TDBGrid(Source).DataSource.DataSet.Fields[0].AsString;
end;procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in Shift then begin
if not FMouseDown then
TDBGrid(Sender).BeginDrag(Mouse.DragImmediate, Mouse.DragThreshold);
FMouseDown := True
end else FMouseDown := False;
end;