hello_win.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //+---------------------------------------------------------------------------
  2. //
  3. // HELLO_WIN.C - Windows GUI 'Hello World!' Example
  4. //
  5. //+---------------------------------------------------------------------------
  6. #include <windows.h>
  7. #define APPNAME "HELLO_WIN"
  8. char szAppName[] = APPNAME; // The name of this application
  9. char szTitle[] = APPNAME; // The title bar text
  10. const char *pWindowText;
  11. void CenterWindow(HWND hWnd);
  12. //+---------------------------------------------------------------------------
  13. //
  14. // Function: WndProc
  15. //
  16. // Synopsis: very unusual type of function - gets called by system to
  17. // process windows messages.
  18. //
  19. // Arguments: same as always.
  20. //----------------------------------------------------------------------------
  21. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  22. {
  23. switch (message) {
  24. // ----------------------- first and last
  25. case WM_CREATE:
  26. CenterWindow(hwnd);
  27. break;
  28. case WM_DESTROY:
  29. PostQuitMessage(0);
  30. break;
  31. // ----------------------- get out of it...
  32. case WM_RBUTTONUP:
  33. DestroyWindow(hwnd);
  34. break;
  35. case WM_KEYDOWN:
  36. if (VK_ESCAPE == wParam)
  37. DestroyWindow(hwnd);
  38. break;
  39. // ----------------------- display our minimal info
  40. case WM_PAINT:
  41. {
  42. PAINTSTRUCT ps;
  43. HDC hdc;
  44. RECT rc;
  45. hdc = BeginPaint(hwnd, &ps);
  46. GetClientRect(hwnd, &rc);
  47. SetTextColor(hdc, RGB(240,240,96));
  48. SetBkMode(hdc, TRANSPARENT);
  49. DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
  50. EndPaint(hwnd, &ps);
  51. break;
  52. }
  53. // ----------------------- let windows do all other stuff
  54. default:
  55. return DefWindowProc(hwnd, message, wParam, lParam);
  56. }
  57. return 0;
  58. }
  59. //+---------------------------------------------------------------------------
  60. //
  61. // Function: WinMain
  62. //
  63. // Synopsis: standard entrypoint for GUI Win32 apps
  64. //
  65. //----------------------------------------------------------------------------
  66. int APIENTRY WinMain(
  67. HINSTANCE hInstance,
  68. HINSTANCE hPrevInstance,
  69. LPSTR lpCmdLine,
  70. int nCmdShow
  71. )
  72. {
  73. MSG msg;
  74. WNDCLASS wc;
  75. HWND hwnd;
  76. pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows!";
  77. // Fill in window class structure with parameters that describe
  78. // the main window.
  79. ZeroMemory(&wc, sizeof wc);
  80. wc.hInstance = hInstance;
  81. wc.lpszClassName = szAppName;
  82. wc.lpfnWndProc = (WNDPROC)WndProc;
  83. wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;
  84. wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  85. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  86. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  87. if (FALSE == RegisterClass(&wc))
  88. return 0;
  89. // create the browser
  90. hwnd = CreateWindow(
  91. szAppName,
  92. szTitle,
  93. WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  94. CW_USEDEFAULT,
  95. CW_USEDEFAULT,
  96. 360,//CW_USEDEFAULT,
  97. 240,//CW_USEDEFAULT,
  98. 0,
  99. 0,
  100. hInstance,
  101. 0);
  102. if (NULL == hwnd)
  103. return 0;
  104. // Main message loop:
  105. while (GetMessage(&msg, NULL, 0, 0) > 0) {
  106. TranslateMessage(&msg);
  107. DispatchMessage(&msg);
  108. }
  109. return msg.wParam;
  110. }
  111. //+---------------------------------------------------------------------------
  112. //+---------------------------------------------------------------------------
  113. void CenterWindow(HWND hwnd_self)
  114. {
  115. HWND hwnd_parent;
  116. RECT rw_self, rc_parent, rw_parent;
  117. int xpos, ypos;
  118. hwnd_parent = GetParent(hwnd_self);
  119. if (NULL == hwnd_parent)
  120. hwnd_parent = GetDesktopWindow();
  121. GetWindowRect(hwnd_parent, &rw_parent);
  122. GetClientRect(hwnd_parent, &rc_parent);
  123. GetWindowRect(hwnd_self, &rw_self);
  124. xpos = rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2;
  125. ypos = rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2;
  126. SetWindowPos(
  127. hwnd_self, NULL,
  128. xpos, ypos, 0, 0,
  129. SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE
  130. );
  131. }
  132. //+---------------------------------------------------------------------------