Delphi5中dbgrid的一点凝惑?(100分)

  • 主题发起人 主题发起人 gcys
  • 开始时间 开始时间
G

gcys

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大师:
我在用Delphi5中,发现Dbgrid控件的MouseDown事件有一个小问题,
在实现拖拽时,鼠标指针击在网格处(数据区)拖拽,不会激活MouseDown事
件,而指向空白处或表头就起作用。这怎么回事?
我做的程序总不能让用户这样用吧?请问各位大师有何高招?
谢谢!
 
你用的是自动拖拽吧?
把dbgrid1.dragmode=自动 改成人工拖拽
然后在dbgrid的mousedown事件中加入dbgrid1.begindrag(false);
 
你用的不是D5吧。
 
应该是D5吧? D4的dbgrid根本没有mousedown事件.
 
如果真是这样,那就是D5的bug
 
这个问题倒还有点奇怪。

我用的也是D5,却没有这个现象!我再试试!下次再来回答你!
 
感谢各位大师捧场。
我用的是人工拖拽。在OnMouseDown事件中书写如下:
begin
if (button=mbleft) and (query1.recordcount>0)
then dbgrid1.BeginDrag(false);
end;
经跟踪执行,发现鼠标在Dbgrid1网格上(数据处)单击不激活本事件;只有
在空白处或标题处才起作用。
???
我原来在D4中用过InfoPower for D4组件,其中的Twwdbgrid控件就没这毛病。
我看了一下Tdbgrid的源代码,说实话,俺还真没看出个明堂来.
因我做的程序中用到的拖拽操作比较多,所以这个问题很关键。请各位大师帮小弟一把。

 
//一个dbgrid的拖放例子,试一下吧
---------------
The GridU1 unit
---------------

unit GridU1;

interface

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

type
TForm1 = class(TForm)
MyDBGrid1: TDBGrid;
Table1: TTable;
DataSource1: TDataSource;
Table2: TTable;
DataSource2: TDataSource;
MyDBGrid2: TDBGrid;
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 : TDBGrid;
begin
DG := Sender as TDBGrid;
SGC := DG.MouseCoord(X,Y);
if (SGC.X > 0) and (SGC.Y > 0) then
(Sender as TDBGrid).BeginDrag(False);
end;

procedure TForm1.MyDBGrid1DragOver(Sender, Source: TObject;
X, Y: Integer; State: TDragState; var Accept: Boolean);
var
GC : TGridCoord;
begin
GC := (Sender as TDBGrid).MouseCoord(X,Y);
Accept := Source is TDBGrid 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 TDBGrid;
GC := DG.MouseCoord(X,Y);
with DG.DataSource.DataSet do begin
with (Source as TDBGrid).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.
 
hubdog兄,感谢你的贴子。但问题是TDbgrid的MouseDown事件出了问题,其余一切
等于白费啊。
 
d5的dbgrid确实有问题,我重载了一下mousedown就可以了,把我前面的例子中
dbgrid用下面这个mydbgrid代替就可以了。

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.
 
接受答案了.
 
后退
顶部