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. switch (g_DrawType)
  197. {
  198. case ID_SETPIXEL:
  199. DrawPixel(hDC);
  200. break;
  201. case ID_GETPIXEL:
  202. GetPixelColor(hDC);
  203. break;
  204. case ID_LINE:
  205. DrawLine(hDC);
  206. break;
  207. case ID_ARC:
  208. DrawArc(hDC);
  209. break;
  210. case ID_POLYLINE:
  211. DrawPolyLine(hDC);
  212. break;
  213. case ID_DRAWBIZER:
  214. DrawBizer(hDC);
  215. break;
  216. case ID_POLYDRAW:
  217. DrawPolyDraw(hDC);
  218. break;
  219. //矩形
  220. case ID_RECT:
  221. DrawRect(hDC);
  222. break;
  223. case ID_ELLIPSE:
  224. DrawEllipse(hDC);
  225. break;
  226. case ID_PIE:
  227. DrawPie(hDC);
  228. break;
  229. case ID_CHORD:
  230. DrawChord(hDC);
  231. break;
  232. case ID_POLYGON:
  233. DrawPolygon(hDC);
  234. break;
  235. }
  236. //画笔使用2
  237. //取出画笔
  238. SelectObject(hDC, hOldPen);
  239. //销毁画笔
  240. DeleteObject(hPen);
  241. //画刷使用2
  242. //取出画刷
  243. SelectObject(hDC, hOldBrush);
  244. //销毁画刷
  245. DeleteObject(hBrush);
  246. EndPaint(hWnd,&ps);
  247. }
  248. //OnCommand
  249. void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  250. {
  251. int nCmdID = LOWORD(wParam);
  252. switch (nCmdID)
  253. {
  254. case ID_SETPIXEL: //Get 和 Set都是同样的代码,所以这样简写
  255. case ID_GETPIXEL:
  256. case ID_LINE: //线
  257. case ID_ARC: //弧
  258. case ID_POLYLINE: //折线
  259. case ID_DRAWBIZER: //BIZER曲线
  260. case ID_POLYDRAW: //多样式线
  261. //矩形
  262. case ID_RECT:
  263. case ID_ELLIPSE:
  264. case ID_PIE:
  265. case ID_CHORD:
  266. case ID_POLYGON:
  267. g_DrawType = nCmdID; //保存绘制图形类型
  268. //刷新窗口
  269. InvalidateRect(hWnd, NULL, TRUE);
  270. break;
  271. case ID_REDPEN: //红色画笔
  272. //把这个笔保存到全局变量中
  273. g_nPenColor = RGB(255, 0, 0); //画笔颜色
  274. //刷新窗口
  275. InvalidateRect(hWnd, NULL, TRUE);
  276. break;
  277. case PS_PSDASH: //画笔的样式为虚线
  278. g_nPenStyle = PS_DASH;
  279. InvalidateRect(hWnd, NULL, TRUE);
  280. break;
  281. case ID_PEN1: //画笔宽度为1
  282. g_nPenWdith = 1;
  283. InvalidateRect(hWnd, NULL, TRUE);
  284. break;
  285. case ID_PEN5: //画笔宽度为5
  286. g_nPenWdith = 5;
  287. InvalidateRect(hWnd, NULL, TRUE);
  288. break;
  289. //画刷颜色
  290. case ID_REDBRUSH:
  291. g_nBrushColor = RGB(255, 0, 0);
  292. InvalidateRect(hWnd, NULL, TRUE);
  293. break;
  294. case ID_GREENBRUSH:
  295. g_nBrushColor = RGB(0, 255, 0);
  296. InvalidateRect(hWnd, NULL, TRUE);
  297. break;
  298. //画刷样式
  299. case ID_SOLIDBRUSH:
  300. g_nBrushStyle = 0xFFFFFFFF;
  301. InvalidateRect(hWnd, NULL, TRUE);
  302. break;
  303. case ID_HSDIAGCROSS:
  304. g_nBrushStyle = HS_DIAGCROSS;
  305. InvalidateRect(hWnd, NULL, TRUE);
  306. break;
  307. case ID_EXIT:
  308. PostQuitMessage(0);
  309. break;
  310. }
  311. }
  312. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  313. {
  314. switch (nMsg)
  315. {
  316. case WM_PAINT:
  317. OnPaint(hWnd, nMsg, wParam, lParam);
  318. break;
  319. case WM_COMMAND:
  320. OnCommand(hWnd, nMsg, wParam, lParam);
  321. break;
  322. case WM_DESTROY:
  323. PostQuitMessage(0);
  324. break;
  325. }
  326. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  327. }
  328. BOOL RegisterWnd(LPSTR pszClassName)
  329. {
  330. WNDCLASSEX wce = { 0 };
  331. wce.cbClsExtra = 0;
  332. wce.cbSize = sizeof(wce);
  333. wce.cbWndExtra = 0;
  334. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  335. wce.hCursor = NULL;
  336. wce.hIcon = NULL;
  337. wce.hIconSm = NULL;
  338. wce.hInstance = g_hInst;
  339. wce.lpfnWndProc = WndProc;
  340. wce.lpszClassName = pszClassName;
  341. wce.lpszMenuName = NULL;
  342. wce.style = CS_VREDRAW | CS_HREDRAW;
  343. ATOM aTom = RegisterClassEx(&wce);
  344. if (aTom)
  345. {
  346. return TRUE;
  347. }
  348. else
  349. {
  350. return FALSE;
  351. }
  352. }
  353. HWND CreateWnd(LPSTR pszClassName)
  354. {
  355. CHAR szTextWindow[256] = { 0 };
  356. LoadString(g_hInst, IDS_WNDID, szTextWindow, 256);
  357. //添加菜单
  358. HMENU hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU1));
  359. HWND hWnd = CreateWindowEx(0, pszClassName, szTextWindow, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  360. NULL, hMenu, g_hInst, NULL);
  361. return hWnd;
  362. }
  363. void ShowWnd(HWND hWnd)
  364. {
  365. ShowWindow(hWnd, SW_SHOW);
  366. UpdateWindow(hWnd);
  367. }
  368. void Msg()
  369. {
  370. MSG msg = { 0 };
  371. while (GetMessage(&msg, NULL, 0, 0))
  372. {
  373. TranslateMessage(&msg);
  374. DispatchMessage(&msg);
  375. }
  376. }
  377. void ConsoleWnd()
  378. {
  379. AllocConsole();
  380. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  381. CHAR szText[] = "Debug start:\n";
  382. WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
  383. }
  384. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  385. {
  386. g_hInst = hInstance;
  387. //AllocConsole();
  388. RegisterWnd("9999");
  389. HWND hWnd = CreateWnd("9999");
  390. ShowWnd(hWnd);
  391. Msg();
  392. return 0;
  393. }





posted @ 2016-06-10 08:17  -刀狂剑痴-  阅读(332)  评论(0编辑  收藏  举报