GdiPlusMatrix.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //Download by http://www.NewXing.com
  2. /**************************************************************************\
  3. *
  4. * Copyright (c) 1998-2001, Microsoft Corp. All Rights Reserved.
  5. *
  6. * Module Name:
  7. *
  8. * GdiplusMatrix.h
  9. *
  10. * Abstract:
  11. *
  12. * GDI+ Matrix class
  13. *
  14. \**************************************************************************/
  15. class Matrix : public GdiplusBase
  16. {
  17. public:
  18. friend class Graphics;
  19. friend class GraphicsPath;
  20. friend class TextureBrush;
  21. friend class LinearGradientBrush;
  22. friend class PathGradientBrush;
  23. friend class Pen;
  24. friend class Region;
  25. // Default constructor is set to identity matrix.
  26. Matrix()
  27. {
  28. GpMatrix *matrix = NULL;
  29. lastResult = DllExports::GdipCreateMatrix(&matrix);
  30. SetNativeMatrix(matrix);
  31. }
  32. Matrix(IN REAL m11,
  33. IN REAL m12,
  34. IN REAL m21,
  35. IN REAL m22,
  36. IN REAL dx,
  37. IN REAL dy)
  38. {
  39. GpMatrix *matrix = NULL;
  40. lastResult = DllExports::GdipCreateMatrix2(m11, m12, m21, m22,
  41. dx, dy, &matrix);
  42. SetNativeMatrix(matrix);
  43. }
  44. Matrix(IN const RectF& rect,
  45. IN const PointF* dstplg)
  46. {
  47. GpMatrix *matrix = NULL;
  48. lastResult = DllExports::GdipCreateMatrix3(&rect,
  49. dstplg,
  50. &matrix);
  51. SetNativeMatrix(matrix);
  52. }
  53. Matrix(IN const Rect& rect,
  54. IN const PointI* dstplg)
  55. {
  56. GpMatrix *matrix = NULL;
  57. lastResult = DllExports::GdipCreateMatrix3I(&rect,
  58. dstplg,
  59. &matrix);
  60. SetNativeMatrix(matrix);
  61. }
  62. ~Matrix()
  63. {
  64. DllExports::GdipDeleteMatrix(nativeMatrix);
  65. }
  66. Matrix *Clone() const
  67. {
  68. GpMatrix *cloneMatrix = NULL;
  69. SetStatus(DllExports::GdipCloneMatrix(nativeMatrix,
  70. &cloneMatrix));
  71. if (lastResult != Ok)
  72. return NULL;
  73. return new Matrix(cloneMatrix);
  74. }
  75. Status GetElements(OUT REAL *m) const
  76. {
  77. return SetStatus(DllExports::GdipGetMatrixElements(nativeMatrix, m));
  78. }
  79. Status SetElements(IN REAL m11,
  80. IN REAL m12,
  81. IN REAL m21,
  82. IN REAL m22,
  83. IN REAL dx,
  84. IN REAL dy)
  85. {
  86. return SetStatus(DllExports::GdipSetMatrixElements(nativeMatrix,
  87. m11, m12, m21, m22, dx, dy));
  88. }
  89. REAL OffsetX() const
  90. {
  91. REAL elements[6];
  92. if (GetElements(&elements[0]) == Ok)
  93. return elements[4];
  94. else
  95. return 0.0f;
  96. }
  97. REAL OffsetY() const
  98. {
  99. REAL elements[6];
  100. if (GetElements(&elements[0]) == Ok)
  101. return elements[5];
  102. else
  103. return 0.0f;
  104. }
  105. Status Reset()
  106. {
  107. // set identity matrix elements
  108. return SetStatus(DllExports::GdipSetMatrixElements(nativeMatrix,
  109. 1.0, 0.0, 0.0, 1.0, 0.0, 0.0));
  110. }
  111. Status Multiply(IN const Matrix *matrix,
  112. IN MatrixOrder order = MatrixOrderPrepend)
  113. {
  114. return SetStatus(DllExports::GdipMultiplyMatrix(nativeMatrix,
  115. matrix->nativeMatrix,
  116. order));
  117. }
  118. Status Translate(IN REAL offsetX,
  119. IN REAL offsetY,
  120. IN MatrixOrder order = MatrixOrderPrepend)
  121. {
  122. return SetStatus(DllExports::GdipTranslateMatrix(nativeMatrix, offsetX,
  123. offsetY, order));
  124. }
  125. Status Scale(IN REAL scaleX,
  126. IN REAL scaleY,
  127. IN MatrixOrder order = MatrixOrderPrepend)
  128. {
  129. return SetStatus(DllExports::GdipScaleMatrix(nativeMatrix, scaleX,
  130. scaleY, order));
  131. }
  132. Status Rotate(IN REAL angle,
  133. IN MatrixOrder order = MatrixOrderPrepend)
  134. {
  135. return SetStatus(DllExports::GdipRotateMatrix(nativeMatrix, angle,
  136. order));
  137. }
  138. Status RotateAt(IN REAL angle,
  139. IN const PointF& center,
  140. IN MatrixOrder order = MatrixOrderPrepend)
  141. {
  142. if(order == MatrixOrderPrepend)
  143. {
  144. SetStatus(DllExports::GdipTranslateMatrix(nativeMatrix, center.X,
  145. center.Y, order));
  146. SetStatus(DllExports::GdipRotateMatrix(nativeMatrix, angle,
  147. order));
  148. return SetStatus(DllExports::GdipTranslateMatrix(nativeMatrix,
  149. -center.X,
  150. -center.Y,
  151. order));
  152. }
  153. else
  154. {
  155. SetStatus(DllExports::GdipTranslateMatrix(nativeMatrix,
  156. - center.X,
  157. - center.Y,
  158. order));
  159. SetStatus(DllExports::GdipRotateMatrix(nativeMatrix, angle,
  160. order));
  161. return SetStatus(DllExports::GdipTranslateMatrix(nativeMatrix,
  162. center.X,
  163. center.Y,
  164. order));
  165. }
  166. }
  167. Status Shear(IN REAL shearX,
  168. IN REAL shearY,
  169. IN MatrixOrder order = MatrixOrderPrepend)
  170. {
  171. return SetStatus(DllExports::GdipShearMatrix(nativeMatrix, shearX,
  172. shearY, order));
  173. }
  174. Status Invert()
  175. {
  176. return SetStatus(DllExports::GdipInvertMatrix(nativeMatrix));
  177. }
  178. // float version
  179. Status TransformPoints(IN OUT PointF* pts,
  180. IN INT count = 1) const
  181. {
  182. return SetStatus(DllExports::GdipTransformMatrixPoints(nativeMatrix,
  183. pts, count));
  184. }
  185. Status TransformPoints(IN OUT PointI* pts,
  186. IN INT count = 1) const
  187. {
  188. return SetStatus(DllExports::GdipTransformMatrixPointsI(nativeMatrix,
  189. pts,
  190. count));
  191. }
  192. Status TransformVectors(IN OUT PointF* pts,
  193. IN INT count = 1) const
  194. {
  195. return SetStatus(DllExports::GdipVectorTransformMatrixPoints(
  196. nativeMatrix, pts, count));
  197. }
  198. Status TransformVectors(IN OUT PointI* pts,
  199. IN INT count = 1) const
  200. {
  201. return SetStatus(DllExports::GdipVectorTransformMatrixPointsI(
  202. nativeMatrix,
  203. pts,
  204. count));
  205. }
  206. BOOL IsInvertible() const
  207. {
  208. BOOL result = FALSE;
  209. SetStatus(DllExports::GdipIsMatrixInvertible(nativeMatrix, &result));
  210. return result;
  211. }
  212. BOOL IsIdentity() const
  213. {
  214. BOOL result = FALSE;
  215. SetStatus(DllExports::GdipIsMatrixIdentity(nativeMatrix, &result));
  216. return result;
  217. }
  218. BOOL Equals(IN const Matrix *matrix) const
  219. {
  220. BOOL result = FALSE;
  221. SetStatus(DllExports::GdipIsMatrixEqual(nativeMatrix,
  222. matrix->nativeMatrix,
  223. &result));
  224. return result;
  225. }
  226. Status GetLastStatus() const
  227. {
  228. Status lastStatus = lastResult;
  229. lastResult = Ok;
  230. return lastStatus;
  231. }
  232. private:
  233. Matrix(const Matrix &);
  234. Matrix& operator=(const Matrix &);
  235. protected:
  236. Matrix(GpMatrix *nativeMatrix)
  237. {
  238. lastResult = Ok;
  239. SetNativeMatrix(nativeMatrix);
  240. }
  241. VOID SetNativeMatrix(GpMatrix *nativeMatrix)
  242. {
  243. this->nativeMatrix = nativeMatrix;
  244. }
  245. Status SetStatus(Status status) const
  246. {
  247. if (status != Ok)
  248. return (lastResult = status);
  249. else
  250. return status;
  251. }
  252. protected:
  253. GpMatrix *nativeMatrix;
  254. mutable Status lastResult;
  255. };