关于控件StringGrid的实现效果!急!!!急!!!

lzc2125 2011-11-30 02:49:25


这是用StringGrid做的一个表格。
每两行代表一条数据。
由于我对这个控件不是很熟悉,图片里面的2个效果我实现不了

1,编辑中的单元格周围有边框,如图黄色位置
2,每条数据之间用实线隔开(行之间颜色切换的地方)。同一条数据的两行,并不是用黑线隔开

哪位高手帮忙指点一下,最好有代码,我是真的不熟悉
先谢谢了
...全文
278 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
随行的太阳 2011-12-02
  • 打赏
  • 举报
回复
用第三方控件
finish07 2011-12-02
  • 打赏
  • 举报
回复
做好收工
procedure TForm1.SG1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
a, i: integer;
s: string;
r: TRect;
Const
PenC = 1;
SelColor = clYellow;
InterCOlor = $ffebde;
GrayLine = $fcfbfa;

begin
with SG1 do
begin
s := Cells[ACol, ARow];
a := Arow div 2;
if (a mod 2 = 1) then
begin
Canvas.Brush.Color := InterCOlor;
end;

If (ARow = SG1.Row) and (ACol = SG1.Col)then // ÐÞ¸ÄÑ¡ÖÐÐÐÑÕÉ«
begin
Canvas.Brush.Color := SelColor;
end;
Canvas.FillRect(Rect);
If (ARow = SG1.Row) and (ACol = SG1.Col)then // »­±ß¿ò
begin
Canvas.Pen.Width := 3;
Canvas.Pen.Color := $847d84;
Canvas.MoveTo(Rect.Left+PenC, Rect.Top+PenC);
Canvas.LineTo(Rect.Right-PenC, rect.Top+PenC);
Canvas.LineTo(Rect.Right-PenC, Rect.Bottom-PenC);
Canvas.LineTo(Rect.Left+PenC, Rect.Bottom-PenC);
Canvas.LineTo(Rect.Left+PenC, Rect.Top+PenC);
Inc(IncA);
Label1.Caption := inttostr(Rect.Left);
Canvas.Pen.Width := 1;
end;
if (ARow mod 2 = 1) then
begin
Canvas.Pen.Color := GrayLine;
Canvas.MoveTo(Rect.Left, Rect.Top-1);
Canvas.LineTo(Rect.Right, rect.Top-1);
end;
if (ARow mod 2 = 0) then
begin
Canvas.Pen.Color := GrayLine;
Canvas.MoveTo(Rect.Left, Rect.Bottom);
Canvas.LineTo(Rect.Right, rect.Bottom);
end;
end;
end;
lzc2125 2011-12-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jayqiang 的回复:]

关于TAdvStringGrid 组件的问题。可以这样写!代码如下:
Delphi(Pascal) code

procedure TForm1.AdvStringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
s: string;
begin
……
[/Quote]

现在用你的代码,在我这边的现象是:
出现了蓝色的框,但是一闪而过,也就是进入编辑状态以后,框就没有了

遇到过没?我怀疑我是本身grid设置了什么东西,但是由于我对这东西不是很熟,也无从下手
kaikai_kk 2011-12-02
  • 打赏
  • 举报
回复
编辑状态,所画的框是不见了的,这个不要紧
tj_snowwolf 2011-12-02
  • 打赏
  • 举报
回复
StringGrid1: TStringGrid;
FInplaceEdit: TRzEdit;

private
FInplaceCol, FInplaceRow: integer;
procedure ShowEditor;

implementation

{$R *.dfm}

procedure FormCreate(Sender: TObject);
var
i ,j : integer;
begin
FInplaceEdit.Parent := StringGrid1;
for I := 0 to stringgrid1.ColCount- 1 do
begin
for j := 0 to stringgrid1.RowCount - 1 do
stringgrid1.Cells[i,j]:='aaaaa'+inttostr(i+j);
end;
end;

procedure ShowEditor;
var
ARect:TRect;
Grid:TStringGrid;
begin
Grid := FInplaceEdit.Parent As TStringGrid;
with Grid do
begin
Grid.Row := FInplaceRow;
Grid.Col := FInplaceCol;
ARect := CellRect(FInplaceCol,FInplaceRow);
InflateRect(ARect,-5,-3);
FInplaceEdit.SetBounds(ARect.Left,ARect.Top,ARect.Right-ARect.Left,ARect.Bottom-ARect.Top);
FInplaceEdit.Text := Cells[FInplaceCol,FInplaceRow];
FInplaceEdit.Visible := True;
FInplaceEdit.SetFocus;
FInplaceEdit.SelectAll;
FInplaceEdit.SelStart := -1;
end;
end;

procedure FInplaceEditChange(Sender: TObject);
begin
if ( FInplaceCol < 0 ) or (FInplaceRow < 0 ) then
Exit;
end;

procedure FInplaceEditExit(Sender: TObject);
var
Grid:TStringGrid;
begin
try
if FInplaceEdit.Visible then
begin
Grid := FInplaceEdit.Parent as TStringGrid;
Grid.Cells[FInplaceCol,FInplaceRow] := FInplaceEdit.Text;
end;
finally
end;
end;

procedure FInplaceEditKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
Grid:TStringGrid;
begin
if Key = 9 then
begin
Grid := FInplaceEdit.Parent as TStringGrid;
FInplaceEditExit(nil);
if FInplaceCol = 2 then
begin
if (ssShift in Shift) then
begin
if (FInplaceRow > 0) then
begin
FInplaceCol := 3;
Dec(FInplaceRow);
end;
end
else
FInplaceCol := 3;
end
else
if (FInplaceCol = 3) then
begin
if ssShift in Shift then
FInplaceCol := 2
else
if (Grid.RowCount-1 > FInplaceRow) then
begin
FInplaceCol := 2;
Inc(FInplaceRow);
end;
end;
ShowEditor;
end;
if Key in [38,40] then //DirectionKey KeyBoard Event
begin
Grid := FInplaceEdit.Parent as TStringGrid;
FInplaceEditExit(nil);
if (Key = 38) and (FInplaceRow > 0) then
FInplaceRow := FInplaceRow -1;
if (Key = 40) and (FInplaceRow < Grid.RowCount-1) then
FInplaceRow := FInplaceRow + 1;
ShowEditor;
end;

end;

procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
str:string;
ix,iy:integer;
ARect :TRect;
begin
with TStringGrid(Sender).Canvas do
begin
str := TStringGrid(Sender).Cells[ACol, ARow];

if ARow mod 2 = 0 then
begin
Brush.Color := clAqua;
end
else
begin
Brush.Color := clWhite;
end;

ix := Rect.Left + (Rect.Right - Rect.Left -TextWidth(str)) div 2 ;
iy := Rect.Top + (Rect.Bottom - Rect.Top -TextHeight(str)) div 2;
if ( gdSelected in State) then
begin
Brush.Color := clYellow;
Font.Color := clRed;
end
else
Font.Color:= clBlack;
FillRect(Rect);
if (ACol in [2,3]) then
begin
ARect := Rect;
InflateRect(ARect,-4,-2);
Pen.Color := clActiveCaption;
Rectangle(ARect);
end;
// Font.Color := clWindow;
TextOut(ix, iy, str);
end;
end;

procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
iCol,iRow,i:integer;
begin
stringgrid1.MouseToCell(x,y,iCol,iRow);
if (iCol < 0) and (iRow<0) then
Exit;
if iCol in [3,2] then
begin
FInplaceCol := iCol;
FInplaceRow := iRow;
ShowEditor;
end
else
FInplaceEdit.Text := '';
end;

颜色自己调整,画线上面已经有了
jayqiang 2011-12-01
  • 打赏
  • 举报
回复
关于TAdvStringGrid 组件的问题。可以这样写!代码如下:

procedure TForm1.AdvStringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
s: string;
begin
s := AdvStringGrid1.Cells[ACol, ARow];
if ARow mod 2 = 0 then
begin
AdvStringGrid1.Canvas.Font.Color := clBlack;
AdvStringGrid1.Canvas.Brush.Color := $00FFCACA;
AdvStringGrid1.Canvas.FillRect(Rect);
AdvStringGrid1.Canvas.TextOut(Rect.Left, Rect.top, s);
end;

if gdFocused in State then
begin
AdvStringGrid1.Canvas.Brush.Color := clYellow;
AdvStringGrid1.Canvas.FillRect(Rect);
AdvStringGrid1.Canvas.Pen.Width := 3;
AdvStringGrid1.Canvas.Pen.Color := clBlue;
AdvStringGrid1.Canvas.Pen.Style := psInsideFrame;
AdvStringGrid1.Canvas.Rectangle(AdvStringGrid1.CellRect(ACol, ARow));
AdvStringGrid1.Canvas.TextOut(Rect.Left + 5, Rect.top +3, s);
AdvStringGrid1.Canvas.Brush.Color := clBlue;
AdvStringGrid1.Canvas.DrawFocusRect(AdvStringGrid1.CellRect(ACol, ARow));
end;
end;
jayqiang 2011-12-01
  • 打赏
  • 举报
回复
不好意思!我电脑上没有 TAdvStringGrid 组件,所以也没法去测试你说的问题,不过我看了一下你那个贴子,道理应该是一样的!你应该可以参考这份代码的!
lzc2125 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jayqiang 的回复:]
已经都代码上传了,下载地址
http://download.csdn.net/detail/jayqiang/3867181
[/Quote]

如果是可操作的单元可呢??
看我另外一个问题:http://topic.csdn.net/u/20111201/16/2469615c-6d4f-4c60-bace-06a776f25a34.html
谢谢了
jayqiang 2011-12-01
  • 打赏
  • 举报
回复
颜色可能不对!自己根据Demo调整一下吧!
jayqiang 2011-12-01
  • 打赏
  • 举报
回复

已经都代码上传了,下载地址
http://download.csdn.net/detail/jayqiang/3867181
一剑飘雪 2011-11-30
  • 打赏
  • 举报
回复
三高?

5,928

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧