修正TiAnlogDisplay显示的问题
原来在DELPHI7下的程序移到XE2下,修修改改,总算行了。一直没注意这个TiAnlogDisplay控件,界面上用了好多这个控件,运行,显示为空,也不报错。看设计界面,也是空的,本来应当显示初始值的。用的是6.0.4 for xe2(网友自己改的),相同的源码在D7下没问题,这就奇怪了。再放个TiAnlogOuput控件(父类是TiCustomEdit),显示也没问题。这2个控件都继承自TiComponent,(中间的一些控件和显示无关),是控件自己的问题了。看源码,也看不出有什么不同的地方。它们的显示都是通过PaintTo这个方法,设断点,出问题了:
procedure TiAnalogDisplay.iPaintTo(Canvas: TCanvas); var AText : String; ATextRect : TRect; ATextFlags : TiTextFlags; begin with Canvas, ATextRect do begin if not ErrorActive then begin if FPrecision >= 0 then AText := Trim(Format('%.' + IntToStr(FPrecision) + 'f', [FValue])) + FUnitsText else AText := Trim(Format('%g', [FValue])) + FUnitsText; Font.Assign(FFont); DrawBackGround(Canvas, BackGroundColor); end else begin AText := ErrorText; Font.Assign(ErrorFont); DrawBackGround(Canvas, ErrorBackGroundColor); end; case BorderStyle of ibsNone : begin ATextRect.Top := 2; ATextRect.Bottom := Height - 2; end; ibsRaised : begin ATextRect.Top := 2; ATextRect.Bottom := Height - 2; end; ibsLowered : begin ATextRect.Top := 2; ATextRect.Bottom := Height - 4; end; end; case FAlignment of iahCenter : begin ATextFlags := [itfHCenter, itfVCenter, itfSingleLine]; ATextRect.Left := 2; ATextRect.Right := Width - 2; end; iahLeft : begin ATextFlags := [itfHLeft, itfVCenter, itfSingleLine]; ATextRect.Left := 2 + FAlignmentMargin; ATextRect.Right := Width - 2; end; else begin ATextFlags := [itfHRight, itfVCenter, itfSingleLine]; ATextRect.Left := 2; ATextRect.Right := Width - 2 - FAlignmentMargin; end; end; Brush.Style := bsClear; iDrawText(Canvas, AText, ATextRect, ATextFlags, True, BackGroundColor); end; end;
这儿的Right和Bottom值变成一个巨大的数!(如44062789),Hight和Width本身的值没问题,但是代码的运算(简单运算,不就是减个常数),Bottom和Right就变成一个巨大数了,怀疑表达式中的Hight和Width在运算的时候被替换了,真奇怪啊
修改成这样:
procedure TiAnalogDisplay.iPaintTo(Canvas: TCanvas); var AText : String; ATextRect : TRect; ATextFlags : TiTextFlags; begin with Canvas, ATextRect do begin if not ErrorActive then begin if FPrecision >= 0 then AText := Trim(Format('%.' + IntToStr(FPrecision) + 'f', [FValue])) + FUnitsText else AText := Trim(Format('%g', [FValue])) + FUnitsText; Font.Assign(FFont); DrawBackGround(Canvas, BackGroundColor); end else begin AText := ErrorText; Font.Assign(ErrorFont); DrawBackGround(Canvas, ErrorBackGroundColor); end; case BorderStyle of ibsNone : begin ATextRect.Top := 2; ATextRect.Bottom := Self.Height - 2; end; ibsRaised : begin ATextRect.Top := 2; ATextRect.Bottom := Self.Height - 2; end; ibsLowered : begin ATextRect.Top := 2; ATextRect.Bottom := Self.Height - 4; end; end; case FAlignment of iahCenter : begin ATextFlags := [itfHCenter, itfVCenter, itfSingleLine]; ATextRect.Left := 2; ATextRect.Right := Self.Width - 2; end; iahLeft : begin ATextFlags := [itfHLeft, itfVCenter, itfSingleLine]; ATextRect.Left := 2 + FAlignmentMargin; ATextRect.Right := Self.Width - 2; end; else begin ATextFlags := [itfHRight, itfVCenter, itfSingleLine]; ATextRect.Left := 2; ATextRect.Right := Self.Width - 2 - FAlignmentMargin; end; end; Brush.Style := bsClear; iDrawText(Canvas, AText, ATextRect, ATextFlags, True, BackGroundColor); end; end;
OK,好了