如题,找了网上资料,基本没什么结果。想了变通方式(即:不使goRowSelect为true,选择一个cell时,该行背景变色),但是问了问题还没找到比较好的方式(http://topic.csdn.net/u/20090319/14/0bf7ea36-d0c0-40c9-8da3-1428906a0333.html)。 所以我查看了Delphi7的相关源码,改了一些东西,基本 达到了我的要求。不过由于本人水品有限,不知这样改后会不会有较大的危害,或有更简单的方式达到目标。请大家帮忙看看,谢谢关注,Thanks。改了5处:
一:注释掉 一点东西:function TCustomGrid.CanEditShow: Boolean;
begin
Result := {([goRowSelect, goEditing] * Options = [goEditing]) and }
FEditorMode and not (csDesigning in ComponentState) and HandleAllocated and
((goAlwaysShowEditor in Options) or IsActiveControl);
end;二:TCustomGrid protected 下添加MyPoint:TPoint;
Procedure MyPro(ACol,ARow: Integer);procedure TCustomGrid.MyPro(ACol,ARow: Integer);
var ss:TGridRect;
tt:TRect;
begin
MyPoint.X:=ACol;
MyPoint.Y:=ARow; ss.Left:=1;
ss.Top:=Row;
ss.Right:=ColCount-1;
ss.Bottom:=Row;
GridRectToScreenRect(ss, tt, True);
Windows.InvalidateRect(Handle, @tt, False);
end;三:TCustomGrid.ShowEditor中添加点东西,添加后为:procedure TCustomGrid.ShowEditor;
begin
if (goEditing in Options)and(goRowSelect in Options) then
begin
FAnchor.X:=MyPoint.X;
FAnchor.Y:=MyPoint.y;
FCurrent.X:=MyPoint.X;
FCurrent.y:=MyPoint.y;
end; FEditorMode := True;
UpdateEdit;
end;四:procedure TCustomGrid.MouseDown(Button: TMouseButton; Shift: TShiftState;X, Y: Integer); 中改一句话// if (CellHit.X = FCurrent.X) and (CellHit.Y = FCurrent.Y) then //这句注释掉
if (CellHit.X = MyPoint.X) and (CellHit.Y = MyPoint.Y) then //改为这句
五:function TCustomDrawGrid.SelectCell(ACol, ARow: Longint): Boolean;中添加一句,改后为:function TCustomDrawGrid.SelectCell(ACol, ARow: Longint): Boolean;
begin
MyPro(ACol, ARow); //加的这句
Result := True;
if Assigned(FOnSelectCell) then FOnSelectCell(Self, ACol, ARow, Result);
end;
一:注释掉 一点东西:function TCustomGrid.CanEditShow: Boolean;
begin
Result := {([goRowSelect, goEditing] * Options = [goEditing]) and }
FEditorMode and not (csDesigning in ComponentState) and HandleAllocated and
((goAlwaysShowEditor in Options) or IsActiveControl);
end;二:TCustomGrid protected 下添加MyPoint:TPoint;
Procedure MyPro(ACol,ARow: Integer);procedure TCustomGrid.MyPro(ACol,ARow: Integer);
var ss:TGridRect;
tt:TRect;
begin
MyPoint.X:=ACol;
MyPoint.Y:=ARow; ss.Left:=1;
ss.Top:=Row;
ss.Right:=ColCount-1;
ss.Bottom:=Row;
GridRectToScreenRect(ss, tt, True);
Windows.InvalidateRect(Handle, @tt, False);
end;三:TCustomGrid.ShowEditor中添加点东西,添加后为:procedure TCustomGrid.ShowEditor;
begin
if (goEditing in Options)and(goRowSelect in Options) then
begin
FAnchor.X:=MyPoint.X;
FAnchor.Y:=MyPoint.y;
FCurrent.X:=MyPoint.X;
FCurrent.y:=MyPoint.y;
end; FEditorMode := True;
UpdateEdit;
end;四:procedure TCustomGrid.MouseDown(Button: TMouseButton; Shift: TShiftState;X, Y: Integer); 中改一句话// if (CellHit.X = FCurrent.X) and (CellHit.Y = FCurrent.Y) then //这句注释掉
if (CellHit.X = MyPoint.X) and (CellHit.Y = MyPoint.Y) then //改为这句
五:function TCustomDrawGrid.SelectCell(ACol, ARow: Longint): Boolean;中添加一句,改后为:function TCustomDrawGrid.SelectCell(ACol, ARow: Longint): Boolean;
begin
MyPro(ACol, ARow); //加的这句
Result := True;
if Assigned(FOnSelectCell) then FOnSelectCell(Self, ACol, ARow, Result);
end;
我想要的就是 这一点点的技巧 。
begin
self.vg_col:=1;
self.vg_row:=1;
end;procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
self.vg_col:=ACol;
self.vg_row:=ARow;
end;procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
s:widestring;
begin
s:=self.StringGrid1.Cells[self.vg_col,self.vg_row];
if (Key>48) and (Key<=128) then
self.StringGrid1.Cells[self.vg_col,self.vg_row]:=s+char(key);
if Key=8 then
begin
self.StringGrid1.Cells[self.vg_col,self.vg_row]:=Copy(s,1,Length(s)-1);
end;
if Key=32 then
begin
self.StringGrid1.Cells[self.vg_col,self.vg_row]:=s+' ';
end;
end;
Edit1.SetBounds,绑定到单元格区域
=======================================================================
void __fastcall Tfm_ly_bill::sgSelectCell(TObject *Sender, int ACol,
int ARow, bool &CanSelect)
{
if(sg->RowCount==ARow+1) { CanSelect=false; return; } //最后一行不整行显示
if((Tag==0||Tag==1)&&(ACol==5||ACol==8))
sg->Options=sg->Options<<goEditing<<goAlwaysShowEditor<<goTabs;
else sg->Options=sg->Options>>goAlwaysShowEditor>>goEditing>>goTabs;
//=========================
sg->Refresh();
TRect irect;
for(int i=1;i<sg->ColCount-1;i++)
{
irect=sg->CellRect(i,ARow);
if((Tag==0||Tag==1)&&(i==5||i==8)) sg->Canvas->Brush->Color=0x0012266B;
else sg->Canvas->Brush->Color=0x00C56A31; // 0x00804000
sg->Canvas->FillRect(irect);
sg->Canvas->Font->Color=clWhite;
sg->Canvas->TextRect(irect,irect.Left+1,irect.Top+1,sg->Cells[i][ARow]);
} //end for
}
然后下面就是画上不同的色彩,以示区别.你的条件可以不必这么长的.