今天写个程序,用到了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中查找到的。