home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / GDIINPUT.PAK / GDIINPUT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  10.5 KB  |  376 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: gdiinput.c
  9. //
  10. //  PURPOSE: Handles the main window for the GDI Input sample.
  11. //
  12. //  FUNCTIONS:
  13. //    WndProc       - Processes messages for the main window.
  14. //    MsgCommand    - Handle the WM_COMMAND messages for the main window.
  15. //    MsgCreate     - Handles the WM_CREATE message.  Creates the toolbar,
  16. //                    statusbar, and client windows.
  17. //    MsgSize       - Handles the WM_SIZE message by passing the message
  18. //                    on to the toolbar and statusbar controls and then
  19. //                    resizing the client window between them.
  20. //    MsgDestroy    - Handles the WM_DESTROY message by calling
  21. //                    PostQuitMessage().
  22. //    CmdRefresh    - Forces client window to repaint.
  23. //    CmdExit       - Handles the file exit command by calling destroy
  24. //                    window on the main window.
  25. //
  26. //  COMMENTS:
  27. //    Message dispatch table -
  28. //      For every message to be handled by the main window procedure
  29. //      place the message number and handler function pointer in
  30. //      rgmsd (the message dispatch table).  Place the prototype
  31. //      for the function in globals.h and the definition of the
  32. //      function in the appropriate module.
  33. //    Command dispatch table -
  34. //      For every command to be handled by the main window procedure
  35. //      place the command number and handler function pointer in
  36. //      rgcmd (the command dispatch table).  Place the prototype
  37. //      for the function in globals.h and the definition of the
  38. //      function in the appropriate module.
  39. //    Globals.h Contains the definitions of the structures and dispatch.c
  40. //      contains the functions that use these structures.
  41. //
  42.  
  43. #include <windows.h>            // required for all Windows applications
  44. #include <windowsx.h>
  45. #include <commctrl.h>           // prototypes and defs for common controls
  46. #include "globals.h"            // prototypes specific to this application
  47. #include "resource.h"
  48. #include "toolbar.h"            // prototypes for the tool bar
  49. #include "statbar.h"            // prototypes for the status bar
  50.  
  51. // Global variables
  52. HWND hWndClient;
  53.  
  54.  
  55. // Main window message table definition.
  56. MSD rgmsd[] =
  57. {
  58.     {WM_MENUSELECT, MsgMenuSelect},
  59.     {WM_COMMAND,    MsgCommand},
  60.     {WM_NOTIFY,     MsgNotify},
  61.     {WM_TIMER,      MsgTimer},
  62.     {WM_SIZE,       MsgSize},
  63.     {WM_CREATE,     MsgCreate},
  64.     {WM_DESTROY,    MsgDestroy}
  65. };
  66.  
  67. MSDI msdiMain =
  68. {
  69.     sizeof(rgmsd) / sizeof(MSD),
  70.     rgmsd,
  71.     edwpWindow
  72. };
  73.  
  74.  
  75. // Main window command table definition.
  76. CMD rgcmd[] =
  77. {
  78.     {IDM_FILENEW,     CmdFileNew},
  79.     {IDM_EXIT,        CmdExit},
  80.  
  81.     {IDM_PIXEL,       CmdDrawMode},
  82.     {IDM_LINE,        CmdDrawMode},
  83.     {IDM_RECT,        CmdDrawMode},
  84.     {IDM_ELLIPSE,     CmdDrawMode},
  85.     {IDM_BEZIER,      CmdDrawMode},
  86.     {IDM_FILL,        CmdFill},
  87.     {IDM_NOFILL,      CmdFill},
  88.     {IDM_CREATEPEN,   CmdCreatePen},
  89.     {IDM_CREATEBRUSH, CmdCreateBrush},
  90.     {IDM_REFRESH,     CmdRefresh},
  91.  
  92.     {IDM_ABOUT,       CmdAbout}
  93. };
  94.  
  95. CMDI cmdiMain =
  96. {
  97.     sizeof(rgcmd) / sizeof(CMD),
  98.     rgcmd,
  99.     edwpWindow
  100. };
  101.  
  102.  
  103. //
  104. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  105. //
  106. //  PURPOSE:  Processes messages for the main window.
  107. //
  108. //  PARAMETERS:
  109. //    hwnd     - window handle
  110. //    uMessage - message number
  111. //    wparam   - additional information (dependant on message number)
  112. //    lparam   - additional information (dependant on message number)
  113. //
  114. //  RETURN VALUE:
  115. //    The return value depends on the message number.  If the message
  116. //    is implemented in the message dispatch table, the return value is
  117. //    the value returned by the message handling function.  Otherwise,
  118. //    the return value is the value returned by the default window procedure.
  119. //
  120. //  COMMENTS:
  121. //    Call the DispMessage() function with the main window's message dispatch
  122. //    information (msdiMain) and the message specific information.
  123. //
  124.  
  125. LRESULT CALLBACK WndProc(HWND   hwnd, 
  126.                          UINT   uMessage, 
  127.                          WPARAM wparam, 
  128.                          LPARAM lparam)
  129. {
  130.     return DispMessage(&msdiMain, hwnd, uMessage, wparam, lparam);
  131. }
  132.  
  133.  
  134. //
  135. //  FUNCTION: MsgCommand(HWND, UINT, WPARAM, LPARAM)
  136. //
  137. //  PURPOSE: Handle the WM_COMMAND messages for the main window.
  138. //
  139. //  PARAMETERS:
  140. //    hwnd     - window handle
  141. //    uMessage - WM_COMMAND (Unused)
  142. //    GET_WM_COMMAND_ID(wparam, lparam)   - Command identifier
  143. //    GET_WM_COMMAND_HWND(wparam, lparam) - Control handle
  144. //
  145. //  RETURN VALUE:
  146. //    The return value depends on the message number.  If the message
  147. //    is implemented in the message dispatch table, the return value is
  148. //    the value returned by the message handling function.  Otherwise,
  149. //    the return value is the value returned by the default window procedure.
  150. //
  151. //  COMMENTS:
  152. //    Call the DispCommand() function with the main window's command dispatch
  153. //    information (cmdiMain) and the command specific information.
  154. //
  155.  
  156. #pragma argsused
  157. LRESULT MsgCommand(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  158. {
  159.     return DispCommand(&cmdiMain, hwnd, wparam, lparam);
  160. }
  161.  
  162.  
  163. //
  164. //  FUNCTION: MsgCreate(HWND, UINT, WPARAM, LPARAM)
  165. //
  166. //  PURPOSE: Handle the WM_CREATE messages for the main window.
  167. //           and call InitCommonControls() API to initialize the
  168. //           common control library. 
  169. //
  170. //  PARAMETERS:
  171. //    hwnd     - window handle
  172. //
  173. //  RETURN VALUE:
  174. //    Return TRUE if the StatusBar and ToolBar Windows could be created
  175. //    successfully. 
  176. //
  177. //  COMMENTS:
  178. //    Call the CreateTSBars function with the main window's window handle
  179. //    information (msdiMain).
  180. //
  181.  
  182. #pragma argsused
  183. LRESULT MsgCreate(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  184. {
  185.     int nRet = -1;                              // Assume failure
  186.  
  187.     InitCommonControls();   // Initialize the common control library.
  188.  
  189.     if(CreateTBar(hwnd) && CreateSBar(hwnd))    // 1st create tool/status bars
  190.      {
  191.         hWndClient = CreateClientWindow(hwnd);  // then create client window
  192.         if (hWndClient != NULL)
  193.             nRet = 0;                           // Indicate success
  194.     }
  195.  
  196.     // Initialize some menu options...
  197.  
  198.     CmdDrawMode(hwnd, IDM_LINE, 0, NULL);   // Set initial draw mode to Line
  199.  
  200.     CmdFill(hwnd, IDM_NOFILL, 0, NULL);     // Turn fill mode OFF
  201.     CmdFill(hwnd, IDM_FILL, 0, NULL);       // Toggle fill state (ON)
  202.  
  203.      return nRet;
  204. }
  205.  
  206.  
  207. //
  208. //  FUNCTION: MsgSize(HWND, UINT, WPARAM, LPARAM)
  209. //
  210. //  PURPOSE:  This function resizes the toolbar and statusbar controls
  211. //    and the Client window which covers the rest of the main window
  212. //    client area.
  213. //
  214. //
  215. //  PARAMETERS:
  216. //
  217. //    hwnd      - Window handle  (Used)
  218. //    uMessage  - Message number (Used)
  219. //    wparam    - Extra data     (Used)
  220. //    lparam    - Extra data     (Used)
  221. //
  222. //  RETURN VALUE:
  223. //
  224. //    Always returns 0 - Message handled
  225. //
  226. //  COMMENTS:
  227. //
  228. //    When the window procdure that has the status and tool bar controls
  229. //    receives the WM_SIZE message, it has to pass the message on to these 
  230. //    controls so that these controls can adjust their size accordingly. 
  231. //   
  232. //
  233.  
  234. LRESULT MsgSize(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam) 
  235. {
  236.     SendMessage(hWndToolbar, uMessage, wparam, lparam);
  237.     SendMessage(hWndStatusbar,  uMessage, wparam, lparam);
  238.  
  239.     // Re-position the panes in the status bar
  240.     InitializeStatusBar(hwnd);
  241.  
  242.     // Re-size client window relative to the tool/status bars
  243.  
  244.     if (wparam != SIZE_MINIMIZED)
  245.     {
  246.         RECT rc, rcClient;
  247.  
  248.         GetClientRect(hwnd, &rcClient);
  249.  
  250.         GetWindowRect(hWndToolbar, &rc);
  251.         ScreenToClient(hwnd, ((LPPOINT)&rc) + 1);
  252.         rcClient.top = rc.bottom;
  253.  
  254.         GetWindowRect(hWndStatusbar, &rc);
  255.         ScreenToClient(hwnd, (LPPOINT)&rc);
  256.         rcClient.bottom = rc.top;
  257.  
  258.         MoveWindow(hWndClient,
  259.                    rcClient.left,
  260.                    rcClient.top,
  261.                    rcClient.right-rcClient.left,
  262.                    rcClient.bottom-rcClient.top,
  263.                    FALSE);
  264.     }
  265.  
  266.     return 0;
  267. }
  268.  
  269.  
  270. //
  271. //  FUNCTION: MsgDestroy(HWND, UINT, WPARAM, LPARAM)
  272. //
  273. //  PURPOSE: Calls PostQuitMessage().
  274. //
  275. //  PARAMETERS:
  276. //
  277. //    hwnd      - Window handle  (Unused)
  278. //    uMessage  - Message number (Unused)
  279. //    wparam    - Extra data     (Unused)
  280. //    lparam    - Extra data     (Unused)
  281. //
  282. //  RETURN VALUE:
  283. //
  284. //    Always returns 0 - Message handled
  285. //
  286. //  COMMENTS:
  287. //
  288. //
  289.  
  290. #pragma argsused
  291. LRESULT MsgDestroy(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  292. {
  293.      PostQuitMessage(0);
  294.     return 0;
  295. }
  296.  
  297.  
  298. //
  299. //  FUNCTION: CmdFileNew(HWND, WORD, WORD, HWND)
  300. //
  301. //  PURPOSE: Erases current drawing
  302. //
  303. //  PARAMETERS:
  304. //    hwnd     - The main window.
  305. //    wCommand - IDM_FILENEW (unused)
  306. //    wNotify  - Notification number (unused)
  307. //    hwndCtrl - NULL (unused)
  308. //
  309. //  RETURN VALUE:
  310. //    Always returns 0 - command handled.
  311. //
  312. //  COMMENTS:
  313. //
  314. //
  315.  
  316. #pragma argsused
  317. LRESULT CmdFileNew(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  318. {
  319.      // Tell client window to purge itself
  320.     ClientNewDrawing();
  321.     return 0;
  322. }
  323.  
  324.  
  325. //
  326. //  FUNCTION: CmdRefresh(HWND, WORD, WORD, HWND)
  327. //
  328. //  PURPOSE: Forces repaint of entire drawing.
  329. //
  330. //  PARAMETERS:
  331. //    hwnd     - The main window.
  332. //    wCommand - IDM_REFRESH (unused)
  333. //    wNotify  - Notification number (unused)
  334. //    hwndCtrl - NULL (unused)
  335. //
  336. //  RETURN VALUE:
  337. //    Always returns 0 - command handled.
  338. //
  339. //  COMMENTS:
  340. //
  341. //
  342.  
  343. #pragma argsused
  344. LRESULT CmdRefresh(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  345. {
  346.     // Force client to repaint.
  347.     InvalidateRect(hWndClient, NULL, TRUE);
  348.     return 0;
  349. }
  350.  
  351.  
  352. //
  353. //  FUNCTION: CmdExit(HWND, WORD, WORD, HWND)
  354. //
  355. //  PURPOSE: Exit the application.
  356. //
  357. //  PARAMETERS:
  358. //    hwnd     - The window.
  359. //    wCommand - IDM_EXIT (unused)
  360. //    wNotify  - Notification number (unused)
  361. //    hwndCtrl - NULL (unused)
  362. //
  363. //  RETURN VALUE:
  364. //    Always returns 0 - command handled.
  365. //
  366. //  COMMENTS:
  367. //
  368. //
  369.  
  370. #pragma argsused
  371. LRESULT CmdExit(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  372. {
  373.     DestroyWindow(hwnd);
  374.     return 0;
  375. }
  376.