home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 January / PCWorld_2000-01_cd.bin / Software / Servis / Devc / _SETUP.5 / Group21 / test.c < prev    next >
C/C++ Source or Header  |  1999-06-18  |  6KB  |  214 lines

  1. /*
  2.  * A basic example of Win32 programmiwng in C.
  3.  *
  4.  * This source code is in the PUBLIC DOMAIN and has NO WARRANTY.
  5.  *
  6.  * Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
  7.  */
  8.  
  9. #include <windows.h>
  10. #include <string.h>
  11. #include <iostream.h>
  12.  
  13. /*
  14.  * This is the window function for the main window. Whenever a message is
  15.  * dispatched using DispatchMessage (or sent with SendMessage) this function
  16.  * gets called with the contents of the message.
  17.  */
  18. LRESULT CALLBACK
  19. MainWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  20. {
  21. /* The window handle for the "Click Me" button. */
  22. static HWND hwndButton = 0;
  23. static int cx, cy;/* Height and width of our button. */
  24.  
  25. HDC hdc;/* A device context used for drawing */
  26. PAINTSTRUCT ps;/* Also used during window drawing */
  27. RECT rc;/* A rectangle used during drawing */
  28.  
  29. /*
  30.  * Perform processing based on what kind of message we got.
  31.  */
  32. switch (nMsg)
  33. {
  34. case WM_CREATE:
  35. {
  36. /* The window is being created. Create our button
  37.  * window now. */
  38. TEXTMETRIC tm;
  39.  
  40. /* First we use the system fixed font size to choose
  41.  * a nice button size. */
  42. hdc = GetDC (hwnd);
  43. SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
  44. GetTextMetrics (hdc, &tm);
  45. cx = tm.tmAveCharWidth * 30;
  46. cy = (tm.tmHeight + tm.tmExternalLeading) * 2;
  47. ReleaseDC (hwnd, hdc);
  48.  
  49. /* Now create the button */
  50. hwndButton = CreateWindow (
  51. "button",/* Builtin button class */
  52. "Click Here",
  53. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  54. 0, 0, cx, cy,
  55. hwnd,/* Parent is this window. */
  56. (HMENU) 1,/* Control ID: 1 */
  57. ((LPCREATESTRUCT) lParam)->hInstance,
  58. NULL
  59. );
  60.  
  61. return 0;
  62. break;
  63. }
  64.  
  65. case WM_DESTROY:
  66. /* The window is being destroyed, close the application
  67.  * (the child button gets destroyed automatically). */
  68. PostQuitMessage (0);
  69. return 0;
  70. break;
  71.  
  72. case WM_PAINT:
  73. /* The window needs to be painted (redrawn). */
  74. hdc = BeginPaint (hwnd, &ps);
  75. GetClientRect (hwnd, &rc);
  76.  
  77. /* Draw "Hello, World" in the middle of the upper
  78.  * half of the window. */
  79. rc.bottom = rc.bottom / 2;
  80. DrawText (hdc, "Hello, World", -1, &rc,
  81. DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  82.  
  83. EndPaint (hwnd, &ps);
  84. return 0;
  85. break;
  86.  
  87. case WM_SIZE:
  88. /* The window size is changing. If the button exists
  89.  * then place it in the center of the bottom half of
  90.  * the window. */
  91. if (hwndButton &&
  92. (wParam == SIZEFULLSCREEN ||
  93.  wParam == SIZENORMAL)
  94.    )
  95. {
  96. rc.left = (LOWORD(lParam) - cx) / 2;
  97. rc.top = HIWORD(lParam) * 3 / 4 - cy / 2;
  98. MoveWindow (
  99. hwndButton,
  100. rc.left, rc.top, cx, cy, TRUE);
  101. }
  102. break;
  103.  
  104. case WM_COMMAND:
  105. /* Check the control ID, notification code and
  106.  * control handle to see if this is a button click
  107.  * message from our child button. */
  108. if (LOWORD(wParam) == 1 &&
  109.     HIWORD(wParam) == BN_CLICKED &&
  110.     (HWND) lParam == hwndButton)
  111. {
  112. /* Our button was clicked. Close the window. */
  113. DestroyWindow (hwnd);
  114. }
  115. return 0;
  116. break;
  117. }
  118.  
  119. /* If we don't handle a message completely we hand it to the system
  120.  * provided default window function. */
  121. return DefWindowProc (hwnd, nMsg, wParam, lParam);
  122. }
  123.  
  124.  
  125. int STDCALL
  126. WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
  127. {
  128. HWND hwndMain;/* Handle for the main window. */
  129. MSG msg;/* A Win32 message structure. */
  130. WNDCLASSEX wndclass;/* A window class structure. */
  131. char*szMainWndClass = "WinTestWin";
  132. /* The name of the main window class */
  133.  
  134. /*
  135.  * First we create a window class for our main window.
  136.  */
  137.  
  138. /* Initialize the entire structure to zero. */
  139. memset (&wndclass, 0, sizeof(WNDCLASSEX));
  140.  
  141. /* This class is called WinTestWin */
  142. wndclass.lpszClassName = szMainWndClass;
  143.  
  144. /* cbSize gives the size of the structure for extensibility. */
  145. wndclass.cbSize = sizeof(WNDCLASSEX);
  146.  
  147. /* All windows of this class redraw when resized. */
  148. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  149.  
  150. /* All windows of this class use the MainWndProc window function. */
  151. wndclass.lpfnWndProc = MainWndProc;
  152.  
  153. /* This class is used with the current program instance. */
  154. wndclass.hInstance = hInst;
  155.  
  156. /* Use standard application icon and arrow cursor provided by the OS */
  157. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  158. wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  159. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  160.  
  161. /* Color the background white */
  162. wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
  163.  
  164. /*
  165.  * Now register the window class for use.
  166.  */
  167. RegisterClassEx (&wndclass);
  168.  
  169. /*
  170.  * Create our main window using that window class.
  171.  */
  172. hwndMain = CreateWindow (
  173. szMainWndClass,/* Class name */
  174. "Hello",/* Caption */
  175. WS_OVERLAPPEDWINDOW,/* Style */
  176. CW_USEDEFAULT,/* Initial x (use default) */
  177. CW_USEDEFAULT,/* Initial y (use default) */
  178. CW_USEDEFAULT,/* Initial x size (use default) */
  179. CW_USEDEFAULT,/* Initial y size (use default) */
  180. NULL,/* No parent window */
  181. NULL,/* No menu */
  182. hInst,/* This program instance */
  183. NULL/* Creation parameters */
  184. );
  185.  
  186. /*
  187.  * Display the window which we just created (using the nShow
  188.  * passed by the OS, which allows for start minimized and that
  189.  * sort of thing).
  190.  */
  191. ShowWindow (hwndMain, nShow);
  192. UpdateWindow (hwndMain);
  193.  
  194. /*
  195.  * The main message loop. All messages being sent to the windows
  196.  * of the application (or at least the primary thread) are retrieved
  197.  * by the GetMessage call, then translated (mainly for keyboard
  198.  * messages) and dispatched to the appropriate window procedure.
  199.  * This is the simplest kind of message loop. More complex loops
  200.  * are required for idle processing or handling modeless dialog
  201.  * boxes. When one of the windows calls PostQuitMessage GetMessage
  202.  * will return zero and the wParam of the message will be filled
  203.  * with the argument to PostQuitMessage. The loop will end and
  204.  * the application will close.
  205.          */
  206. while (GetMessage (&msg, NULL, 0, 0))
  207. {
  208. TranslateMessage (&msg);
  209. DispatchMessage (&msg);
  210. }
  211. return msg.wParam;
  212. }
  213.  
  214.