hello_win.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. char *pWindowText;
  11. HINSTANCE g_hInst; // current instance
  12. void CenterWindow(HWND hWnd);
  13. //+---------------------------------------------------------------------------
  14. //
  15. // Function: WndProc
  16. //
  17. // Synopsis: very unusual type of function - gets called by system to
  18. // process windows messages.
  19. //
  20. // Arguments: same as always.
  21. //----------------------------------------------------------------------------
  22. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  23. {
  24. switch (message)
  25. {
  26. // ----------------------- first and last
  27. case WM_CREATE:
  28. CenterWindow(hwnd);
  29. break;
  30. case WM_DESTROY:
  31. PostQuitMessage(0);
  32. break;
  33. // ----------------------- get out of it...
  34. case WM_RBUTTONUP:
  35. DestroyWindow(hwnd);
  36. break;
  37. case WM_KEYDOWN:
  38. if (VK_ESCAPE == wParam)
  39. DestroyWindow(hwnd);
  40. break;
  41. // ----------------------- display our minimal info
  42. case WM_PAINT:
  43. {
  44. PAINTSTRUCT ps;
  45. HDC hdc;
  46. RECT rc;
  47. hdc = BeginPaint(hwnd, &ps);
  48. GetClientRect(hwnd, &rc);
  49. SetTextColor(hdc, RGB(240,240,96));
  50. SetBkMode(hdc, TRANSPARENT);
  51. DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
  52. EndPaint(hwnd, &ps);
  53. break;
  54. }
  55. // ----------------------- let windows do all other stuff
  56. default:
  57. return DefWindowProc(hwnd, message, wParam, lParam);
  58. }
  59. return 0;
  60. }
  61. //+---------------------------------------------------------------------------
  62. //
  63. // Function: WinMain
  64. //
  65. // Synopsis: standard entrypoint for GUI Win32 apps
  66. //
  67. //----------------------------------------------------------------------------
  68. int APIENTRY WinMain(
  69. HINSTANCE hInstance,
  70. HINSTANCE hPrevInstance,
  71. LPSTR lpCmdLine,
  72. int nCmdShow)
  73. {
  74. MSG msg;
  75. WNDCLASS wc;
  76. HWND hwnd;
  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)) return 0;
  88. // create the browser
  89. hwnd = CreateWindow(
  90. szAppName,
  91. szTitle,
  92. WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  93. CW_USEDEFAULT,
  94. CW_USEDEFAULT,
  95. 360,//CW_USEDEFAULT,
  96. 240,//CW_USEDEFAULT,
  97. 0,
  98. 0,
  99. g_hInst,
  100. 0);
  101. if (NULL == hwnd) return 0;
  102. pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows!";
  103. // Main message loop:
  104. while (GetMessage(&msg, NULL, 0, 0) > 0)
  105. {
  106. TranslateMessage(&msg);
  107. DispatchMessage(&msg);
  108. }
  109. return msg.wParam;
  110. }
  111. //+---------------------------------------------------------------------------
  112. //+---------------------------------------------------------------------------
  113. void CenterWindow(HWND hwnd_self)
  114. {
  115. RECT rw_self, rc_parent, rw_parent; HWND hwnd_parent;
  116. hwnd_parent = GetParent(hwnd_self);
  117. if (NULL==hwnd_parent) hwnd_parent = GetDesktopWindow();
  118. GetWindowRect(hwnd_parent, &rw_parent);
  119. GetClientRect(hwnd_parent, &rc_parent);
  120. GetWindowRect(hwnd_self, &rw_self);
  121. SetWindowPos(hwnd_self, NULL,
  122. rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2,
  123. rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2,
  124. 0, 0,
  125. SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE
  126. );
  127. }
  128. //+---------------------------------------------------------------------------