9WinMap 映射

  1. #include <windows.h>
  2. #include <iostream>
  3. CHAR szText[256] = { 0 };
  4. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
  5. HINSTANCE g_hInst = NULL; //窗口句柄
  6. HANDLE g_hStdout = NULL; //控制台句柄
  7. //OnPaint
  8. void OnPaint(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  9. {
  10. PAINTSTRUCT ps = { 0 };
  11. HDC hDC = BeginPaint(hWnd, &ps);
  12. Rectangle(hDC, 100, 100, 200, 200); //默认情况下是100个逻辑单位
  13. int nOldMap = SetMapMode(hDC, MM_LOMETRIC);//0.1MM X方向向右,Y方向向上
  14. Rectangle(hDC, 100, -100, 200, -200); //默认情况下是100个逻辑单位
  15. SetMapMode(hDC, nOldMap);//恢复旧的映射模式
  16. nOldMap = SetMapMode(hDC, MM_HIMETRIC);
  17. Rectangle(hDC, 100, -100, 200, -200); //默认情况下是100个逻辑单位
  18. SetMapMode(hDC, nOldMap);
  19. nOldMap = SetMapMode(hDC, MM_ISOTROPIC);
  20. SetViewportExtEx(hDC, 2, 2, NULL); //屏幕单位
  21. SetWindowExtEx(hDC, 4, 4, NULL); //逻辑单位
  22. SetWindowOrgEx(hDC, 100, 100, NULL); //X和Y偏移100
  23. Rectangle(hDC, 100, 100, 200, 200); //默认情况下是100个逻辑单位
  24. SetMapMode(hDC, nOldMap);
  25. EndPaint(hWnd, &ps);
  26. }
  27. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  28. {
  29. switch (nMsg)
  30. {
  31. case WM_PAINT:
  32. OnPaint(hWnd, nMsg, wParam, lParam);
  33. break;
  34. case WM_DESTROY:
  35. PostQuitMessage(0);
  36. break;
  37. }
  38. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  39. }
  40. BOOL RegisterWnd(LPSTR pszClassName)
  41. {
  42. WNDCLASSEX wce = { 0 };
  43. wce.cbSize = sizeof(wce);
  44. wce.cbClsExtra = 0;
  45. wce.cbWndExtra = 0;
  46. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  47. wce.hCursor = NULL;
  48. wce.hIcon = NULL;
  49. wce.hIconSm = NULL;
  50. wce.hInstance = g_hInst;
  51. wce.lpfnWndProc = WndProc;
  52. wce.lpszClassName = pszClassName;
  53. wce.lpszMenuName = NULL;
  54. wce.style = CS_HREDRAW | CS_VREDRAW;
  55. ATOM atom = RegisterClassEx(&wce);
  56. if (atom == NULL)
  57. {
  58. return FALSE;
  59. }
  60. else
  61. {
  62. return TRUE;
  63. }
  64. }
  65. HWND CreateWnd(LPSTR pszClassName)
  66. {
  67. HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  68. CW_USEDEFAULT, NULL, NULL, g_hInst, 0);
  69. return hWnd;
  70. }
  71. void ShowWnd(HWND hWnd)
  72. {
  73. ShowWindow(hWnd, SW_SHOW);
  74. UpdateWindow(hWnd);
  75. }
  76. void Msg()
  77. {
  78. MSG msg = { 0 };
  79. while (GetMessage(&msg, NULL, 0, 0))
  80. {
  81. TranslateMessage(&msg);
  82. DispatchMessage(&msg);
  83. }
  84. }
  85. void ConsoleWnd()
  86. {
  87. AllocConsole();
  88. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  89. CHAR szText[] = "Debug start:\n";
  90. WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
  91. }
  92. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  93. {
  94. g_hInst = hInstance;
  95. //ConsoleWnd();
  96. RegisterWnd("oooo");
  97. HWND hWnd = CreateWnd("oooo");
  98. ShowWnd(hWnd);
  99. Msg();
  100. return 0;
  101. }





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