TBrush使用问题

    今天写个程序,用到了TBrush,但是当Brushsytle=bsClear时,老是会显示为bsSolid,于是我就开始查看TBrush源代码,开始看SetStyle,如下:

 

procedure TBrush.SetStyle(Value: TBrushStyle);
var
  BrushData: TBrushData;
begin
  if (Value <> Style) or ((Value = bsClear) and (Color <> clWhite)) then
  begin
{$IF DEFINED(CLR)}
    BrushData := GetBrushData;
    BrushData.Style := Value;
    if BrushData.Style = bsClear then
      BrushData.Color := clWhite;
    SetBrushData(BrushData);
    if BrushData.RefCount = 0 then
      BrushData.Free;
{$ELSE}
    GetData(BrushData);
    BrushData.Style := Value;
    if BrushData.Style = bsClear then
      BrushData.Color := clWhite;
    SetData(BrushData);
{$IFEND}
  end;
end;

 

查看一下,原来是bsClear时,color必须为clWhite,于是我修改程序,如果为bsClear,我就将Color赋为clwhite,一试结果还是不行。

这次我开始查看SetColor,

procedure TBrush.SetColor(Value: TColor);
var
  BrushData: TBrushData;
begin
  if (Value <> Color) or ((Style = bsClear) and (Style <> bsSolid)) then
  begin
{$IF DEFINED(CLR)}
    BrushData := GetBrushData;
    BrushData.Color := Value;
    if BrushData.Style = bsClear then
      BrushData.Style := bsSolid;
    SetBrushData(BrushData);
    if BrushData.RefCount = 0 then
      BrushData.Free;
{$ELSE}
    GetData(BrushData);
    BrushData.Color := Value;
    if BrushData.Style = bsClear then
      BrushData.Style := bsSolid;
    SetData(BrushData);
{$IFEND}
  end;
end;

原来问题出在这里, 设置颜色后,如果颜色不为clWhite ,直接修改为bsSolid,见代码:

if BrushData.Style = bsClear then
      BrushData.Style := bsSolid;
我的解决方法当然是先判断,如果是bsClear就不对color进行设置。

但同时发现一个小问题就是,大家请看这个判断语句:

if (Value <> Color) or ((Style = bsClear) and (Style <> bsSolid)) then
附上:

 TBrushStyle = (bsSolid, bsClear, bsHorizontal, bsVertical,
    bsFDiagonal, bsBDiagonal, bsCross, bsDiagCross);

 

此时:style:TBrushStyle ;

我在想,如果style=bsClear后,为什么还要加上Style<>bsSolid?

((Style = bsClear) and (Style <> bsSolid)) 和(Style=bsClear)难道不是一样的吗?

于是我查了一下Delphi7,再看SetColor如下:

procedure TBrush.SetColor(Value: TColor);
var
  BrushData: TBrushData;
begin
  GetData(BrushData);
  BrushData.Color := Value;
  if BrushData.Style = bsClear then BrushData.Style := bsSolid;
  SetData(BrushData);
end;

 

SetStyle如下:

procedure TBrush.SetStyle(Value: TBrushStyle);
var
  BrushData: TBrushData;
begin
  if (Value <> Style) or ((Value = bsClear) and (Color <> clWhite)) then
  begin
{$IF DEFINED(CLR)}
    BrushData := GetBrushData;
    BrushData.Style := Value;
    if BrushData.Style = bsClear then
      BrushData.Color := clWhite;
    SetBrushData(BrushData);
    if BrushData.RefCount = 0 then
      BrushData.Free;
{$ELSE}
    GetData(BrushData);
    BrushData.Style := Value;
    if BrushData.Style = bsClear then
      BrushData.Color := clWhite;
    SetData(BrushData);
{$IFEND}
  end;
end;
我猜想,应该是英什么卡公司没有完全看懂代码直接复制、粘贴过来的代码吧。呵呵,意外收获。

以上代码是在Delphi2009和Delphi7中查找到的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值