stringgrid 使用技巧

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值