7Windows_paint GDI绘图

Posted 养老保险年审

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了7Windows_paint GDI绘图相关的知识,希望对你有一定的参考价值。

  1. // 7Windows_paint.cpp : 定义应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "7Windows_paint.h"
  5. #include "resource.h"
  6. #include <iostream>
  7. HINSTANCE g_hInst = NULL;
  8. HANDLE g_hStdout = NULL; //控制台
  9. int g_DrawType = 0;
  10. COLORREF g_nPenColor = RGB(0,0,0); //画笔颜色
  11. int g_nPenStyle = PS_SOLID; //画笔样式
  12. int g_nPenWdith = 1; //画笔宽度
  13. COLORREF g_nBrushColor = RGB(0, 0, 0); //画刷
  14. int g_nBrushStyle = 0xFFFFFFFF; //画刷样式
  15. CHAR szText[256] = { 0 }; //备用字符串
  16. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
  17. void DrawPixel(HDC hDC)
  18. {
  19. COLORREF nColor = RGB(255,0,0);
  20. SetPixel(hDC, 100, 100, nColor);
  21. }
  22. void GetPixelColor(HDC hDC)
  23. {
  24. COLORREF nColor = GetPixel(hDC, 100, 100);
  25. int nRed = GetRValue(nColor);
  26. int nGreen = GetGValue(nColor);
  27. int nBlue = GetBValue(nColor);
  28. sprintf_s(szText, 256, "nRed=%d,nGreen=%d,nBlue=%d", nRed, nGreen, nBlue);
  29. //PrintLog(szText);
  30. MessageBox(NULL, szText, "color", MB_OK);
  31. }
  32. //DrawLine
  33. void DrawLine(HDC hDC)
  34. {
  35. MoveToEx(hDC, 0, 0, NULL); //当前点
  36. LineTo(hDC, 500, 500);
  37. MoveToEx(hDC, 500, 0, NULL);
  38. LineTo(hDC, 0, 500);
  39. }
  40. //DrawArc弧
  41. void DrawArc(HDC hDC)
  42. {
  43. SetArcDirection(hDC, AD_CLOCKWISE);//AD_COUNTERCLOCKWISE //顺时针
  44. Arc(hDC, 400, 200, 500, 300, 500, 200, 400, 200); //顺时针和逆时针画出来的东西不同
  45. SetArcDirection(hDC, AD_COUNTERCLOCKWISE);//AD_CLOCKWISE //逆时针
  46. Arc(hDC, 500, 200, 600, 300, 600, 200, 500, 400); //顺时针和逆时针画出来的东西不同
  47. MoveToEx(hDC, 200, 200, NULL);
  48. AngleArc(hDC, 200, 200, 100, 90, 270);
  49. LineTo(hDC, 200, 200);
  50. }
  51. //DrawPolyLine
  52. //折线
  53. void DrawPolyLine(HDC hDC)
  54. {
  55. POINT ptPolyLine[7] = { 0 }; //三角形需要4个点
  56. ptPolyLine[0].x = 100;
  57. ptPolyLine[0].y = 100;
  58. ptPolyLine[1].x = 200;
  59. ptPolyLine[1].y = 100;
  60. ptPolyLine[2].x = 200;
  61. ptPolyLine[2].y = 200;
  62. ptPolyLine[3].x = 300;
  63. ptPolyLine[3].y = 200;
  64. ptPolyLine[4].x = 300;
  65. ptPolyLine[4].y = 300;
  66. ptPolyLine[5].x = 400;
  67. ptPolyLine[5].y = 300;
  68. ptPolyLine[6].x = 400;
  69. ptPolyLine[6].y = 400;
  70. //两点绘制一直线
  71. Polyline(hDC, ptPolyLine, 7); //绘制折线
  72. //PolylineTo(hDC, ptPolyLine, 4);
  73. /*
  74. 分成两组,这两组之间没有任何关联
  75. */
  76. DWORD nGroup[2] = { 3, 4 };
  77. //PolyPolyline(hDC, ptPolyLine, nGroup, 2);
  78. }
  79. //DrawBizer曲线
  80. void DrawBizer(HDC hDC)
  81. {
  82. POINT ptBizer[7] = { 0 };
  83. ptBizer[0].x = 100; //端点
  84. ptBizer[0].y = 100;
  85. ptBizer[1].x = 150; //控制点
  86. ptBizer[1].y = 50;
  87. ptBizer[2].x = 300; //控制点
  88. ptBizer[2].y = 150;
  89. ptBizer[3].x = 300; //端点
  90. ptBizer[3].y = 100;
  91. ptBizer[4].x = 300; //控制点
  92. ptBizer[4].y = 400;
  93. ptBizer[5].x = 400; //控制点
  94. ptBizer[5].y = 200;
  95. ptBizer[6].x = 500; //端点
  96. ptBizer[6].y = 300;
  97. PolyBezier(hDC, ptBizer, 7);
  98. //为了看的更清楚,把切线也给划出来
  99. MoveToEx(hDC, ptBizer[0].x, ptBizer[0].y,NULL);
  100. LineTo(hDC, ptBizer[1].x, ptBizer[1].y);
  101. MoveToEx(hDC, ptBizer[2].x, ptBizer[2].y, NULL);
  102. LineTo(hDC, ptBizer[3].x, ptBizer[3].y);
  103. }
  104. //DrawPolyDraw多样式线
  105. void DrawPolyDraw(HDC hDC)
  106. {
  107. POINT ptDraw[4] = { 0 };
  108. ptDraw[0].x = 100;
  109. ptDraw[0].y = 100;
  110. ptDraw[1].x = 200;
  111. ptDraw[1].y = 100;
  112. ptDraw[2].x = 200;
  113. ptDraw[2].y = 200;
  114. ptDraw[3].x = 300;
  115. ptDraw[3].y = 200;
  116. BYTE ptType[4] = { 0 }; //PolyDraw第三个参数
  117. ptType[0] = PT_MOVETO;
  118. ptType[1] = PT_LINETO;
  119. ptType[2] = PT_MOVETO;
  120. ptType[3] = PT_LINETO;
  121. PolyDraw(hDC, ptDraw, ptType, 4);
  122. }
  123. //Rect 矩形
  124. void DrawRect(HDC hDC)
  125. {
  126. //矩形
  127. Rectangle(hDC, 100, 100, 200, 200);
  128. //带圆角矩形
  129. RoundRect(hDC, 300, 100, 400, 200, 10, 10);
  130. }
  131. //DrawEllipse圆和椭圆
  132. void DrawEllipse(HDC hDC)
  133. {
  134. //圆
  135. Ellipse(hDC, 100, 100, 200, 200);
  136. //椭圆
  137. Ellipse(hDC, 300, 100, 500, 200);
  138. }
  139. //DrawPie饼图
  140. void DrawPie(HDC hDC)
  141. {
  142. Pie(hDC, 100, 100, 500, 400, 500, 100, 100, 100);
  143. }


  144. //Chord弦
  145. void DrawChord(HDC hDC)
  146. {
  147. Chord(hDC, 100, 100, 500, 400, 500, 100, 100, 100);
  148. }
  149. //DrawPolygon多边形
  150. void DrawPolygon(HDC hDC)
  151. {
  152. POINT ptPolygon[7] = { 0 };
  153. ptPolygon[0].x = 100;
  154. ptPolygon[0].y = 100;
  155. ptPolygon[1].x = 200;
  156. ptPolygon[1].y = 100;
  157. ptPolygon[2].x = 200;
  158. ptPolygon[2].y = 200;
  159. ptPolygon[3].x = 300;
  160. ptPolygon[3].y = 200;
  161. ptPolygon[4].x = 300;
  162. ptPolygon[4].y = 300;
  163. ptPolygon[5].x = 400;
  164. ptPolygon[5].y = 300;
  165. ptPolygon[6].x = 400;
  166. ptPolygon[6].y = 400;
  167. Polygon(hDC, ptPolygon, 7);
  168. }
  169. void OnPaint(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  170. {
  171. PAINTSTRUCT ps = { 0 };
  172. HDC hDC = BeginPaint(hWnd,&ps);
  173. //画笔使用
  174. //创建画笔
  175. HPEN hPen = CreatePen(g_nPenStyle, g_nPenWdith, g_nPenColor);
  176. //设置画笔到当前DC
  177. HPEN hOldPen = (HPEN)SelectObject(hDC, hPen);
  178. //画刷使用*/
  179. //创建画刷
  180. /*以下一句为默认画刷*/
  181. //HBRUSH hBrush = CreateSolidBrush(g_nBrushColor);
  182. //创建画刷样式时改写
  183. HBRUSH hBrush = NULL;
  184. if (g_nBrushStyle == 0xFFFFFFFF)
  185. {
  186. hBrush = CreateSolidBrush(g_nBrushColor);
  187. }
  188. else
  189. {
  190. hBrush = CreateHatchBrush(g_nBrushStyle, g_nBrushColor);
  191. }
  192. //设置画刷到当前DC
  193. HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
  194. //绘制图形
  195. //在绘制图形里面根据类型绘制图形
  196. 以上是关于7Windows_paint GDI绘图的主要内容,如果未能解决你的问题,请参考以下文章

    使用 gdi+ 进行无闪烁绘图

    GDI Win32 绘图图

    MFC GDI绘图基础

    离屏绘图 GDI+

    GDI+ 多线程绘图

    适用于所有 Win32 程序员的在 Windows Aero Glass(DWM、GDI、GDI+)上绘图的文档和 API 示例