您的位置:首页 > 其它

stringgrid 使用技巧

2010-11-26 16:29 344 查看
1. 文字居中 加 选中时(焦点) 虚边框 如何加颜色

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
vText: PChar;
begin
with TStringGrid(Sender) do begin
vText := PChar(Cells[ACol, ARow]);
Canvas.FillRect(Rect);
DrawText(Canvas.Handle, vText, StrLen(vText), Rect,
DT_CENTER or DT_VCENTER or DT_SINGLELINE); //重绘单元格

if gdFocused in State then begin //同意camel_luo//我来加一条
Rect.Left := Rect.Left + 1;
Rect.Top := Rect.Top + 1;
Rect.Right := Rect.Right - 1;
Rect.Bottom := Rect.Bottom - 1;
Canvas.DrawFocusRect(Rect);
end;
end;
end;

2. 当行变色 指在编辑的状态下 区分当前 显示当前行与其他行的区别

IF gOperateState<>'浏览' THEN

with TStringGrid(Sender) do

begin

if (inttostr(old)<>'0') and (old<>cur) then

begin

for i:=1 to StringGrid1.ColCount-1 do

begin

Str:= StringGrid1.Cells[i,old];

FRect:=StringGrid1.CellRect(i,old); //获取区域

Canvas.Brush.Color:=clCream; //填充颜色 白色

Canvas.FillRect(FRect); //区域填充

SetTextColor(Canvas.Handle, clblack); //文本颜色

DrawText(Canvas.Handle, PChar(Str), Length(Str), FRect, 0); //windows.pas 即 win32API 调用外部函数 stdcall

end;

end;

if (inttostr(cur)<>'0') and (stringgrid1.row=cur) and (stringgrid1.row<>0) and (old<>stringgrid1.row) then

begin

old:=cur; //新一行 旧一行

for i:=1 to StringGrid1.ColCount-1 do

begin

Str:= StringGrid1.Cells[i,stringgrid1.row];

FRect:=StringGrid1.CellRect(i,stringgrid1.row);

Canvas.Brush.Color:=clskyblue;

Canvas.FillRect(FRect);

SetTextColor(Canvas.Handle, clblack);

DrawText(Canvas.Handle, PChar(Str), Length(Str), FRect, 0);

end;

end;

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: