home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / INITREE.PAK / INITREE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  13.9 KB  |  486 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:   initree.c
  9. //
  10. //  PURPOSE:  Implement the windows procedure for the main application
  11. //    windows.  Also implement the generic message and command dispatchers.
  12. //
  13. //  FUNCTIONS:
  14. //    WndProc         - Processes messages for the main window.
  15. //    MsgCommand      - Handle the WM_COMMAND messages for the main window.
  16. //    MsgDestroy      - Handles the WM_DESTROY message by calling 
  17. //                      PostQuitMessage().
  18. //    CmdExit         - Handles the file exit command by calling destory 
  19. //                      window on the main window.
  20. //    CreateImageList - Create an Image List.
  21. //    CreateTreeView  - Create a TreeViewControl.
  22. //
  23. //  COMMENTS:
  24. //
  25.  
  26. #include <windows.h>                // required for all Windows applications.
  27. #include <windowsx.h>
  28. #include <commctrl.h>               // TreeView declarations live here.
  29. #include "globals.h"                // prototypes specific to this application.
  30. #include "resource.h"
  31.  
  32. HWND        hwndTreeView;           // hwnd of TreeView Control.
  33. HIMAGELIST  hImageList;             // Handle to ImageList.
  34.  
  35. int         iINIFileImage;          // Index of "INI" Image in ImageList.
  36. int         iSectionKeyImage;       // Index of "Folder" Image in ImageList.
  37. int         iSelectedEntryImage;    // Index of Selected Entry Image 
  38.                                     // in ImageList.
  39. int         iNonSelectedEntryImage; // Index of Non-Selected Entry Image 
  40.                                     // in ImageList.
  41.  
  42. // FUNCTION PROTOTYPES
  43.  
  44. HWND CreateTreeView(HWND hwndParent, int iID, DWORD dwStyle);
  45.  
  46. HIMAGELIST CreateImageList(PINT piINIFileImage,
  47.     PINT piSectionKeyImage,
  48.     PINT piSelectedEntryImage,
  49.     PINT piNonSelectedEntryImage);
  50.  
  51. extern void INI_FillTreeView(HWND hwndTreeView, 
  52.     int iINIFileImage,
  53.     int iSectionKeyImage,
  54.     int iSelectedEntryImage,
  55.     int iNonSelectedEntryImage);
  56.  
  57. // Main window message table definition.
  58. MSD rgmsd[] =
  59. {
  60.     {WM_COMMAND,    MsgCommand},
  61.     {WM_DESTROY,    MsgDestroy},
  62.     {WM_CREATE,     MsgCreate},
  63.     {WM_SIZE,       MsgSize}
  64. };
  65.  
  66. MSDI msdiMain =
  67. {
  68.     sizeof(rgmsd) / sizeof(MSD),
  69.     rgmsd,
  70.     edwpWindow
  71. };
  72.  
  73.  
  74. // Main window command table definition.
  75. CMD rgcmd[] =
  76. {
  77.     {IDM_EXIT,  CmdExit},
  78.     {IDM_ABOUT, CmdAbout},
  79.     {IDM_FILL,  CmdFill}
  80. };
  81.  
  82. CMDI cmdiMain =
  83. {
  84.     sizeof(rgcmd) / sizeof(CMD),
  85.     rgcmd,
  86.     edwpWindow
  87. };
  88.  
  89.  
  90. //
  91. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  92. //
  93. //  PURPOSE:  Processes messages for the main window.
  94. //
  95. //  PARAMETERS:
  96. //    hwnd     - window handle
  97. //    uMessage - message number
  98. //    wparam   - additional information (dependant on message number)
  99. //    lparam   - additional information (dependant on message number)
  100. //
  101. //  RETURN VALUE:
  102. //    The return value depends on the message number.  If the message
  103. //    is implemented in the message dispatch table, the return value is
  104. //    the value returned by the message handling function.  Otherwise,
  105. //    the return value is the value returned by the default window procedure.
  106. //
  107. //  COMMENTS:
  108. //    Call the DispMessage() function with the main window's message dispatch
  109. //    information (msdiMain) and the message specific information.
  110. //
  111.  
  112. LRESULT CALLBACK WndProc(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  113. {
  114.     return DispMessage(&msdiMain, hwnd, uMessage, wparam, lparam);
  115. }
  116.  
  117.  
  118. //
  119. //  FUNCTION: MsgCommand(HWND, UINT, WPARAM, LPARAM)
  120. //
  121. //  PURPOSE: Handle the WM_COMMAND messages for the main window.
  122. //
  123. //  PARAMETERS:
  124. //    hwnd     - window handle
  125. //    uMessage - WM_COMMAND (Unused)
  126. //    GET_WM_COMMAND_ID(wparam, lparam)   - Command identifier
  127. //    GET_WM_COMMAND_HWND(wparam, lparam) - Control handle
  128. //
  129. //  RETURN VALUE:
  130. //    The return value depends on the message number.  If the message
  131. //    is implemented in the message dispatch table, the return value is
  132. //    the value returned by the message handling function.  Otherwise,
  133. //    the return value is the value returned by the default window procedure.
  134. //
  135. //  COMMENTS:
  136. //    Call the DispCommand() function with the main window's command dispatch
  137. //    information (cmdiMain) and the command specific information.
  138. //
  139.  
  140. #pragma argsused
  141. LRESULT MsgCommand(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  142. {
  143.     return DispCommand(&cmdiMain, hwnd, wparam, lparam);
  144. }
  145.  
  146. //
  147. //  FUNCTION: MsgCreate(HWND, UINT, WPARAM, LPARAM)
  148. //
  149. //  PURPOSE: Creates TreeView Control, and posts "Fill" message.
  150. //
  151. //  PARAMETERS:
  152. //
  153. //    hwnd      - Window handle  (Unused)
  154. //    uMessage  - Message number (Unused)
  155. //    wparam    - not used
  156. //    lparam    - not used
  157. //
  158. //  RETURN VALUE:
  159. //
  160. //    Always returns 0 - Message handled
  161. //
  162. //  COMMENTS:
  163. //
  164. //
  165.  
  166. #pragma argsused
  167. LRESULT MsgCreate(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  168. {
  169.     // Create the TreeView control, with lines and buttons, even at
  170.     // the root level.
  171.  
  172.     hwndTreeView = CreateTreeView(hwnd, 
  173.                                   0, 
  174.                                              TVS_HASLINES | TVS_HASBUTTONS |
  175.                                   TVS_LINESATROOT | TVS_HASLINES |
  176.                                   WS_VISIBLE | WS_CHILD | WS_BORDER);
  177.  
  178.     // Post a message to fill the control, or nix app if
  179.     // the creation failed.
  180.  
  181.     if (hwndTreeView)
  182.         PostMessage(hwnd, WM_COMMAND, IDM_FILL, 0L );
  183.     else
  184.         PostMessage(hwnd, WM_COMMAND, IDM_EXIT, 0L );
  185.  
  186.      return 0;
  187. }
  188.  
  189. //
  190. //  FUNCTION: MsgDestroy(HWND, UINT, WPARAM, LPARAM)
  191. //
  192. //  PURPOSE: Calls PostQuitMessage(), cleans up image list.
  193. //
  194. //  PARAMETERS:
  195. //
  196. //    hwnd      - Window handle  (Unused)
  197. //    uMessage  - Message number (Unused)
  198. //    wparam    - Extra data     (Unused)
  199. //    lparam    - Extra data     (Unused)
  200. //
  201. //  RETURN VALUE:
  202. //
  203. //    Always returns 0 - Message handled
  204. //
  205. //  COMMENTS:
  206. //
  207. //
  208.  
  209. #pragma argsused
  210. LRESULT MsgDestroy(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  211. {
  212.      // Clean up the image list.
  213.  
  214.      if (hImageList)
  215.           ImageList_Destroy(hImageList);
  216.  
  217.      PostQuitMessage(0);
  218.     return 0;
  219. }
  220.  
  221. //
  222. //  FUNCTION: MsgSize(HWND, UINT, WPARAM, LPARAM)
  223. //
  224. //  PURPOSE: Resizes TreeView Control
  225. //
  226. //  PARAMETERS:
  227. //
  228. //    hwnd      - Window handle  (Unused)
  229. //    uMessage  - Message number (Unused)
  230. //    wparam    - Not used.
  231. //    lparam    - Not used.
  232. //
  233. //  RETURN VALUE:
  234. //
  235. //    Always returns 0 - Message handled
  236. //
  237. //  COMMENTS:
  238. //
  239. //
  240.  
  241. #pragma argsused
  242. LRESULT MsgSize(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  243. {
  244.      RECT rc;    // Client area rect
  245.  
  246.     // Figure the new client area
  247.  
  248.     GetClientRect(hwnd, &rc);
  249.  
  250.     // Resize the TreeView control
  251.  
  252.     if (hwndTreeView)
  253.         SetWindowPos(hwndTreeView,
  254.                      NULL,
  255.                      0, 0,
  256.                      rc.right, rc.bottom,
  257.                             SWP_NOZORDER);
  258.  
  259.     return 0;
  260. }
  261.  
  262. //
  263. //  FUNCTION: CmdFill
  264. //
  265. //  PURPOSE: Fill TreeView Control will the INI files.
  266. //
  267. //  PARAMETERS:
  268. //    hwnd     - window handle
  269. //    wCommand - IDM_FILL
  270. //    wNotify  - Not Used.
  271. //    hwndCtrl - Not Used.
  272. //
  273. //  RETURN VALUE:
  274. //    Always 0.
  275. //
  276. //  COMMENTS:
  277. //    This calls the INI_FillTreeView function in INI.C to fill the
  278. //    TreeView control. The message to invoke this function is posted
  279. //    at startup in MsgCreate.
  280.  
  281. #pragma argsused
  282. LRESULT CmdFill(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  283. {
  284.      INI_FillTreeView(hwndTreeView,
  285.                             iINIFileImage,
  286.                             iSectionKeyImage,
  287.                             iSelectedEntryImage,
  288.                      iNonSelectedEntryImage);
  289.                      
  290.     return 0;
  291. }
  292.  
  293. //
  294. //  FUNCTION: CmdExit(HWND, WORD, WORD, HWND)
  295. //
  296. //  PURPOSE: Exit the application.
  297. //
  298. //  PARAMETERS:
  299. //    hwnd     - The window.
  300. //    wCommand - IDM_EXIT (unused)
  301. //    wNotify  - Notification number (unused)
  302. //    hwndCtrl - NULL (unused)
  303. //
  304. //  RETURN VALUE:
  305. //    Always returns 0 - command handled.
  306. //
  307. //  COMMENTS:
  308. //
  309. //
  310.  
  311. #pragma argsused
  312. LRESULT CmdExit(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  313. {
  314.     DestroyWindow(hwnd);
  315.     return 0;
  316. }
  317.  
  318. //
  319. //  FUNCTION: CreateImageList(PINT piINIFileImage,
  320. //                            PINT piSectionKeyImage,
  321. //                            PINT piSelectedEntryImage,
  322. //                            PINT piNonSelectedEntryImage)
  323. //
  324. //  PURPOSE: Create an ImageList for use with the TreeView.
  325. //
  326. //  PARAMETERS:
  327. //    piINIFileImage           - Ptr to Index of "INI" Image in ImageList.
  328. //    piSectionKeyImage        - Ptr to Index of "Folder" Image in ImageList.
  329. //    piSelectedEntryImage     - Ptr to Index of Selected Entry Image 
  330. //                               in ImageList.
  331. //    piNonSelectedEntryImage  - Ptr to Index of Non-Selected Entry Image 
  332. //                               in ImageList.
  333. //  RETURN VALUE:
  334. //    On Success: Handle to the Image List
  335. //    On Failure: NULL
  336. //
  337. //  COMMENTS:
  338. //    The bitmaps for the image list are stored in the resources
  339. //    of this application. For this example, each bitmap is a
  340. //    separate bitmap. The background (mask) color of the bitmaps
  341. //    is white - RGB (255,255,255), which is what the image list
  342. //    will automagically use as a mask as explained below.
  343. //
  344. //    Even though the bitmaps in the resources are 32x32 pixels,
  345. //    we are only using the upper left 20x12 portion, as this is the
  346. //    minimum bounding rectangle to fit the little teensy pictures.
  347. //
  348. //    To change the size of the images, edit both the bitmaps,
  349. //    and change the parameters to ImageList_Create.
  350. //
  351.  
  352. HIMAGELIST CreateImageList(PINT piINIFileImage,
  353.                            PINT piSectionKeyImage,
  354.                            PINT piSelectedEntryImage,
  355.                            PINT piNonSelectedEntryImage)
  356. {
  357.     HIMAGELIST hIL;       // Handle to ImageList
  358.     HBITMAP    hBitmap;   // Handle to a bitmap
  359.  
  360.     // Create the Image List
  361.  
  362.     hIL = ImageList_Create(20,     // X size of one image
  363.                            12,     // Y size of one image
  364.                            TRUE,   // Masked images
  365.                            4,      // Four images in this list
  366.                            0);     // No "growing" bitmaps in this list.
  367.  
  368.     if (!hIL) return NULL;
  369.  
  370.     // Add Each bitmap to the ImageList.
  371.     //
  372.     // ImageList_AddMasked will add the bitmap, and treat every
  373.     // pixel that is (in this example) white as a "transparent" pixel,
  374.     // since we specified TRUE for fMask in the above call to
  375.     // ImageList_Create.
  376.  
  377.     hBitmap = LoadBitmap(hInst, "INI");
  378.     if (hBitmap)
  379.     {
  380.         *piINIFileImage = ImageList_AddMasked(hIL, hBitmap, RGB(255,255,255));
  381.         DeleteObject (hBitmap);
  382.     }
  383.  
  384.     hBitmap = LoadBitmap(hInst, "KEY");
  385.     if (hBitmap)
  386.     {
  387.         *piSectionKeyImage = ImageList_AddMasked(hIL, hBitmap, RGB(255,255,255));
  388.         DeleteObject (hBitmap);
  389.     }
  390.  
  391.     hBitmap = LoadBitmap(hInst, "ENTRYS");
  392.     if (hBitmap)
  393.     {
  394.         *piSelectedEntryImage = ImageList_AddMasked(hIL, hBitmap, RGB(255,255,255));
  395.         DeleteObject (hBitmap);
  396.     }
  397.  
  398.     hBitmap = LoadBitmap(hInst, "ENTRYN");
  399.     if (hBitmap)
  400.     {
  401.         *piNonSelectedEntryImage = ImageList_AddMasked(hIL, hBitmap, RGB(255,255,255));
  402.         DeleteObject (hBitmap);
  403.     }
  404.  
  405.     // Paranoid checking to make sure everything was added successfully.
  406.  
  407.     if (ImageList_GetImageCount(hIL) < 3)
  408.     {
  409.         ImageList_Destroy(hIL);
  410.         hIL = NULL;
  411.         *piINIFileImage          = 0;
  412.         *piSectionKeyImage       = 0;
  413.         *piSelectedEntryImage    = 0;
  414.         *piNonSelectedEntryImage = 0;
  415.     }
  416.  
  417.     // Success! 
  418.  
  419.     return hIL;
  420. }
  421.  
  422. //
  423. //  FUNCTION: CreateTreeView(HWND hwndParent, int iID, DWORD dwStyle)
  424. //
  425. //  PURPOSE: Create a TreeView control
  426. //
  427. //  PARAMETERS:
  428. // 
  429. //    hwndParent - Parent of the TreeView.
  430. //    iID        - ID of the TreeView.
  431. //    dwStyle    - Style bits of the TreeView.
  432. //
  433. //  RETURN VALUE:
  434. //    On Success: Handle to the TreeView
  435. //    On Failure: NULL
  436. //
  437. //  COMMENTS:
  438. //
  439. //    The call to InitCommonControls is required once per application
  440. //    lifetime. It is OK to move this to init.c, but for the
  441. //    cut-and-pastibility of this sample, the call is placed here.
  442. //
  443.  
  444. HWND CreateTreeView(HWND hwndParent, int iID, DWORD dwStyle)
  445. {
  446.     RECT rc;      // Client rect of parent
  447.     HWND hwnd;    // Handle to TreeView
  448.  
  449.     // This registers the TreeView class.
  450.  
  451.     InitCommonControls();
  452.  
  453.     // Get the client area of the parent.
  454.  
  455.     GetClientRect(hwndParent, &rc);
  456.  
  457.     // Create the TreeView control.
  458.  
  459.     hwnd = CreateWindowEx(0,                        // Ex style
  460.                           WC_TREEVIEW,              // Class
  461.                           "",                       // Dummy Text
  462.                           dwStyle,                  // Style
  463.                           0, 0,                     // Use all of
  464.                           rc.right, rc.bottom,      // client area.
  465.                           hwndParent,               // Parent
  466.                           (HMENU)iID,               // ID
  467.                           hInst,                    // Instance
  468.                           NULL);                    // No extra
  469.  
  470.     // Create the ImageList for this TreeView
  471.  
  472.     hImageList = CreateImageList(&iINIFileImage,
  473.                                  &iSectionKeyImage,
  474.                                  &iSelectedEntryImage,
  475.                                  &iNonSelectedEntryImage);
  476.  
  477.     // Attach ImageList to TreeView
  478.  
  479.     if (hImageList && hwnd)
  480.         (void) TreeView_SetImageList(hwnd, hImageList, 0);
  481.  
  482.     // Return the window handle
  483.  
  484.     return hwnd;
  485. }
  486.