如何在DBGrid中同时选择行和列,并高亮显行出来?这样的效果有如十字光标一样。最好有 (200分)

  • 主题发起人 主题发起人 quejian
  • 开始时间 开始时间
Q

quejian

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在DBGrid中同时选择行和列,并高亮显行出来?这样的效果有如十字光标一样。最好有
例程代码。
 
想法很好,希望能早日看到答案。
有例程代码请发我一份。谢谢!
Dreamer_afei@163.com
 
问题解决再加分
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, Grids, DBGrids;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
procedure DBGrid1DrawDataCell(Sender: TObject;
const Rect: TRect;
Field: TField;
State: TGridDrawState);
procedure Table1AfterScroll(DataSet: TDataSet);
procedure DBGrid1ColEnter(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject;
const Rect: TRect;
Field: TField;
State: TGridDrawState);
var
ARow,ACol,ThisLeft,ThisTop,ThisRight,ThisBottom,i:integer;
begin
ARow:=TStringGrid(DBGrid1).Row;
ACol:=TStringGrid(DBGrid1).Col;
thisLeft:=0;
for i:=TStringGrid(DBGrid1).LeftCol to TStringGrid(DBGrid1).Col-1 do
thisLeft:=thisLeft+TStringGrid(DBGrid1).ColWidths+1;
ThisRight:=thisLeft+TStringGrid(DBGrid1).ColWidths[ACol];
thisTop:=0;
for i:=0 to ARow-1 do
thisTop:=ThisTop+TStringGrid(DBGrid1).RowHeights+1;
thisBottom:= thisTop+TStringGrid(DBGrid1).RowHeights+1;
DBGrid1.Canvas.Brush.Style:=bsClear;
DBGrid1.Canvas.Pen.Color:=clRed;
DBGrid1.Canvas.Pen.Style:=psSolid;
DBGrid1.Canvas.Rectangle(0,thisTop+1,thisRight+10+1,thisBottom-1);
DBGrid1.Canvas.Rectangle(ThisLeft+13,1,thisRight+11,thisBottom-1);
end;

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
begin
DBGrid1.Invalidate;
end;

procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
DBGrid1.Invalidate;
end;
end.

作个了大概的样子,只是十字光标的一半——左上半部分,完美的效果就只有靠你自己了。
代码很容易读懂,就不加注释了。
 
wk_knife兄能否帮忙帮到底,送佛送到天啊
 
我觉得上面那个形状已经挺好看、在需要对齐行输入时也比较醒目,没必要作成十字型。
再者做成十字考虑的东西更多,比方字段不够添满DBgrid,记录不够添满DBGrid时,十字光标
的底端和右端都需要考虑,程序要复杂得多。在我看来有点不值得。
 
单但取得 行、列位置来说,不难:
procedure TForm1.DBGrid1DblClick(Sender: TObject);
begin
memo1.Lines.Clear;
memo1.Lines.Add(inttostr(dbgrid1.selectedindex));
memo1.Lines.Add(inttostr(dbgrid1.DataSource.DataSet.recno));
end;
 
wk_knife,我也觉得这样做是浪费,但老板的意思要这样做我也没有办法
 
做好后能给我发一份吗?期待!

chenghm_boy@163.net
 
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject;
const Rect: TRect;
Field: TField;
State: TGridDrawState);
begin
if (Rect.Top = TStringGrid(DBGrid1).CellRect(TStringGrid(DBGrid1).Col, TStringGrid(DBGrid1).Row).Top) and (not (gdFocused in State)) then
Dbgrid1.Canvas.Brush.Color := clYellow;
if (Rect.Left = TStringGrid(DBGrid1).CellRect(TStringGrid(DBGrid1).Col, TStringGrid(DBGrid1).Row).Left) and (not (gdFocused in State)) then
Dbgrid1.Canvas.Brush.Color := clYellow;
DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;

procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
DBGrid1.Invalidate;
end;

呵呵,还有点Bug,勉强应付吧:
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部