我使用Delphi(XE5)创建图形组件。一个挑战是通过SetWorldTransform旋转闭合路径,然后使用GetPath读回轮廓。旋转工作正常,但从GetPath返回的点不会旋转(但是,填充区域(PathToRegion)按预期工作!)。
我的代码:
procedure Rotate(DestBitmap : TBitmapEx; Radians : Single; FigureRect : TRect);
// DestBitmap is where to draw the figure. Size of DestBitmap is computed from
//the actual angle and figure size (not shown here). FigureRect is the plain
//figure rectangle without rotation
var
XForm: tagXFORM;
C, S : single;
Points : array of TPoint;
NumPoints : integer;
Bytes : TByteArray;
Rgn : HRGN;
X, Y : integer;
begin
//Locate FigureRect to center of bitmap:
X := (DestBitmap.Width - FigureRect.Width) div 2;
Y := (DestBitmap.Height - FigureRect.Height) div 2;
FigureRect.Location := Point(X,Y);
//Set rotate mode
C := Cos(Radians);
S := Sin(Radians);
XForm.eM11 := C;
XForm.eM12 := S;
XForm.eM21 := -S;
XForm.eM22 := C;
XForm.eDx := (DestBitmap.Width - DestBitmap.Width * C +
DestBitmap.Height * S) / 2;
XForm.eDy := (DestBitmap.Height - DestBitmap.Width * S -
DestBitmap.Height * C) / 2;
SetGraphicsMode(DestBitmap.Canvas.Handle, GM_ADVANCED);
SetWorldTransform(DestBitmap.Canvas.Handle, XForm);
//Rotate the figure
BeginPath(DestBitmap.Canvas.Handle);
DestBitmap.Canvas.Rectangle(FigureRect);
EndPath(DestBitmap.Canvas.Handle);
FlattenPath(DestBitmap.Canvas.Handle);
NumPoints := GetPath(DestBitmap.Canvas.Handle, Points[0], Bytes[0], 0);
SetLength(Points, NumPoints);
GetPath(DestBitmap.Canvas.Handle, Points[0], Bytes[0], NumPoints);
//Points now describes the plain, unrotated figure, but if instead:
//Rgn := PathToRegion(DestBitmap.Canvas.Handle);
//Rgn describes the rotated area, as expected
end;
答案 0 :(得分:2)
这是预期的,GetPath
返回逻辑坐标中的点。而PathToRegion
的结果区域使用设备坐标 - 因此它不受变换的影响。请参阅两个函数的文档。
或者三,SetWorldTransform
转换逻辑坐标。对于逻辑世界中的一切,一切都没有改变。转换与设备有关。