home *** CD-ROM | disk | FTP | other *** search
- /*
- Copyright (c) 1993 by Barking Spider Software, Inc. All rights reserved
-
- Filename...: treedem1.c
-
- Version,,,,: 1.0
-
- Language...: Microsoft C/C++ 7.0
-
- Model......: Small
-
- Environment: Microsoft Windows 3.1
-
- Description: This source module demonstrates the BSS Tree Control. It
- is a very simple example. It just creates a tree and
- provides the minimum support. One top level window is
- created similar to the standard GENERIC app. In the
- handling of the WM_CREATE message in this window's
- associated window procedure, the BSS Tree Control is
- created and a root tree node is inserted into the empty
- tree along with five children to this node.
-
- The user is then able to double click on any of the nodes.
- Initially since all of the nodes are closed except the root,
- double clicking on any of the closed nodes will add five
- child nodes to the node which was selected.
-
- If the user clicks on a node which is opened (has children)
- then the five children of the selected node will be deleted.
-
- To close the application, the user selects the Close option
- in the system menu.
-
- The below code shows how to create a tree, add nodes, delete
- nodes and the clean-up process for destroying the tree.
-
- For more documentation on the BSS Tree Control APIs,
- structures, notifications, and error codes, refer to the
- header file BSTREE.H.
- Notes......:
-
- History....:
-
- Author.....: Peter J. Kaufman
- */
-
- #include <windows.h>
- #include "treedem1.h"
- #include "..\bscore.h"
- #include "..\bstree.h"
-
- static char szAppName[] = "TreeDem1" ;
- HANDLE hInst ;
-
-
- /*****************************************************************************
- int PASCAL WinMain ( HANDLE hInstance,
- HANDLE hPrevInstance,
- LPSTR lpszCmdLine,
- int nCmdShow);
-
- *****************************************************************************/
-
- int PASCAL WinMain ( HANDLE hInstance,
- HANDLE hPrevInstance,
- LPSTR lpszCmd,
- int nCmdShow)
- {
- HWND hwnd ;
- MSG msg ;
- WNDCLASS wndclass ;
-
- hInst = hInstance ;
-
- if (!hPrevInstance)
- {
- wndclass.style = CS_HREDRAW | CS_VREDRAW ;
- wndclass.lpfnWndProc = TreeDemo1WndProc ;
- wndclass.cbClsExtra = 0 ;
- wndclass.cbWndExtra = 0 ;
- wndclass.hInstance = hInst;
- wndclass.hIcon = LoadIcon (hInst,
- MAKEINTRESOURCE(IDR_TREEDEMO_ICON)) ;
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
- wndclass.hbrBackground = COLOR_APPWORKSPACE + 1;
- wndclass.lpszMenuName = NULL ;
- wndclass.lpszClassName = szAppName ;
-
- RegisterClass (&wndclass) ;
- }
-
- hwnd = CreateWindow (szAppName,
- "Barking Spider Software, Inc. Tree Demo 1",
- WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- NULL,
- NULL,
- hInstance,
- NULL);
-
- ShowWindow (hwnd, nCmdShow) ;
- UpdateWindow (hwnd) ;
-
- while (GetMessage (&msg, NULL, 0, 0))
- {
- TranslateMessage (&msg) ;
- DispatchMessage (&msg) ;
- }
- return msg.wParam ;
- }
-
-
-
- /*****************************************************************************
- long FAR PASCAL _export TreeDemo1WndProc ( HWND hwnd,
- UINT message,
- UINT wParam,
- LONG lParam)
-
-
- Shows the process of creating a BSS Tree Control, the adding and deleting of
- children, and the clean-up process when destroying the tree.
-
- The number of children a node can have is hardwired for simplicity and the
- text string for each tree node is almost identical except for the index
- number at the end of the string.
- *****************************************************************************/
-
- HWND hwndTree; // Handle of tree control.
- HBITMAP hbmClosedActive; // Bitmap handle of closed folder when the tree node
- // is highlighted.
- HBITMAP hbmClosed; // Bitmap handle of closed folder when the tree node
- // is NOT highlighted.
- HBITMAP hbmOpenActive; // Bitmap handle of open folder when the tree node
- // is highlighted.
- HBITMAP hbmOpen; // Bitmap handle of open folder when the tree node
- // is NOT highlighted.
-
- #define MAX_CHILDREN_PER_NODE 5
- #define MAX_SAMPLE_TEXT_LENGTH 512
- #define SAMPLE_TEXT "Sample Tree Node Text"
-
-
-
- long FAR PASCAL _export TreeDemo1WndProc ( HWND hwnd,
- UINT message,
- UINT wParam,
- LONG lParam)
- {
- LP_TREE_NODE lpParentTreeNode;// Pointer to tree node
- LP_TREE_NODE_DEF lpTreeNodeDef; // Pointer to tree node definition
- LP_TREE_NODE_DEF lpTreeNodeDef2;
- LP_SELECT_NOTIF lpSelectNotif; // Pointer to tree notification struct.
- WORD i;
- short cx, cy;
- WORD wErrCode;
- LONG lResult;
- WORD wMaxChildrenPerNode;
-
- switch (message)
- {
- case WM_CREATE:
- lResult = 0xFFFFFFFF;
-
- // BST_CreateTree ( ) creates a BSS Tree Control. The parameters are
- // similar to the Windows API CreateWindowEx ( ). Why have the call?
- // The API guarantees that the Window style will include
- // the scroll bars, clipping, and helps the programmer use the
- // right class. Notice I added a border to the style. Looks great!
- // Notice that the starting x, y, width, and height are
- // 0? This works because the WM_SIZE message handler of this window
- // procedure does a MoveWindow ( ) to the BSS Tree Control to keep
- // the tree control sized to the main window's client area.
-
- hwndTree = BST_CreateTree (hInst, hwnd, 0, 0, 0, 0, WS_BORDER, 0);
-
- if(hwndTree != NULL) // If the BST_CreateTree ( ) is successful...
- {
- // The routine CreateBackgroundMatchedBitmap ( ) is defined at
- // the end of this module. CreateBackgroundMatchedBitmap ( )
- // loads the initial folder bitmaps from the resource,
- // and converts the bitmap background color to match the system
- // window color. This is done for the closed and open
- // folder bitmaps. Since the folders are used for animation
- // and the shapes are non rectangular, the background of the
- // bitmap must match the window color otherwise the appearance
- // is undesirable.
- // CreateBackgroundMatchedBitmap ( ) only handles
- // 16 bit color bitmaps.
-
- // Notice that this procedure is also called for the
- // COLOR_HIGHLIGHT color. COLOR_HIGHLIGHT is the color used to
- // show the current selection (active node). Feel free to
- // include this and any other routine in your application if the
- // need for background masking is needed.
-
- hbmClosed = CreateBackgroundMatchedBitmap (
- GetSysColor (COLOR_WINDOW),
- IDR_CLOSED_FOLDER);
-
- hbmClosedActive = CreateBackgroundMatchedBitmap (
- GetSysColor (COLOR_HIGHLIGHT),
- IDR_CLOSED_FOLDER);
-
- hbmOpen = CreateBackgroundMatchedBitmap (
- GetSysColor (COLOR_WINDOW),
- IDR_OPEN_FOLDER);
-
- hbmOpenActive = CreateBackgroundMatchedBitmap (
- GetSysColor (COLOR_HIGHLIGHT),
- IDR_OPEN_FOLDER);
-
- // Bitmaps spaces are regions where bitmaps are displayed for the
- // tree node. Bitmap spaces encompass the bitmaps or icons.
- // The bitmap spaces are placed left of the text.
-
- // From left to right the left most bitmap space has an index
- // value of 0, the next bitmap space has an index value of 1,
- // and so on.
-
- // When a bitmap space is defined, it is defined for the entire
- // tree, for all tree nodes. For example, if the left most bitmap
- // space is defined to have a width of 30 pixels and a height of
- // 20 pixels, then all the bitmaps placed in this bitmap space
- // for all the tree nodes will have to fit into these dimensions.
- // The reasoning behind this, is that it forces column alignment
- // of the bitmaps for a given bitmap space and offers a
- // consistency in hit testing. Hit testing is performed on the
- // bitmap space, not just on the bitmap.
-
- // Below, the left most bitmap space is defined. The max width
- // and height are defined. The first argument is the BSS Tree
- // Control handle, the second is the bitmap space identifier or
- // index. The third and fourth arguments are the width and
- // height respectively. The last argument signals to the BSS Tree
- // Control whether to center or left justify the currently
- // assigned bitmap in this bitmap space. TRUE means center,
- // FALSE means left justify.
-
- // Instead of hard code values for the width and height, another
- // could be taken. If the item to be assigned to the below
- // bitmap space is a bitmap, then a call to
- // GetObject (hBitmap, sizeof(BITMAP), (LPSTR) &bm);
- // could be called and the dimensions of the bitmap could be
- // used along with some padding to define the width and height
- // of the bitmap space.
- // If the item to be assigned to the below bitmap space is an
- // icon, then the values from the calls to
- // GetSystemMetrics (SM_CXICON) and GetSystemMetrics (SM_CYICON)
- // could be used along with some padding to define the width
- // and height of the bitmap space. Since the Windows GDI
- // call DrawIcon ( ) is used to draw the icon by the tree
- // control, if a bitmap space is to contain an icon then the
- // width and height must be defined the described method above.
-
- BST_SetBitmapSpace( hwndTree, 0, 23, 17, TRUE);
-
- // Now, the fun part... Defining tree nodes.
- // First... define the root!
-
- // Allocate one (1) tree node definition for the root. It
- // is best if this memory is initialize to zero. Any members
- // of the TREE_NODE_DEF structure that are not defined must be
- // initialized with zero. The tree node
- // definition structure is the vehicle that the application uses
- // to define tree nodes to the BSS Tree Control. This memory is
- // the property of the application. It's contents are copied to
- // the tree control. Once the node is defined, this memory can
- // be freed.
-
- // Note. GlobalAlloc ( ) returns the segment and not the handle
- // of the allocated memory since the GMEM_FIXED flag is applied
- // which requests that the memory block is fixed in memory. Also,
- // since the GMEM_ZEROINIT flag is applied, the memory is zeroed.
- // MAKEP ( ) macro converts this segment into a long
- // pointer (segment:0). The GMEM_SHARE flag is used since
- // the pointer is passed to the tree control in the process
- // of defining new tree nodes.
-
- lpTreeNodeDef = (LP_TREE_NODE_DEF)
- MAKEP( GlobalAlloc (GMEM_FIXED | GMEM_ZEROINIT | GMEM_SHARE,
- MAKELONG( sizeof (TREE_NODE_DEF) * 1, 0)), 0);
-
-
- if( lpTreeNodeDef ) // If the above memory allocation is success...
- {
- // Assign the text that will be displayed for the tree node.
-
- // Normally, the tree control will allocate wTextLength + 1
- // worth of memory to store a copy of the string pointed to by
- // lpszText when the tree node is created since the supplied
- // pointer can become invalid if the string it points to is
- // moved or deleted in the future.
-
- lpTreeNodeDef->lpszText = "You cannot have a tree without a root.";
-
- // Assign the length of the above text to the wTextLength
- // member. This tells the tree control, how many character
- // to display.
-
- // Note. If the sign bit of the wTextLength member is set to 1
- // by the application then this signals the tree
- // control NOT to allocate memory for the string pointed to by
- // lpszText, above, but just store and use the supplied string
- // pointer directly to display the tree node's text.
- // This would work in the case of string assigned
- // to the lpszText member, above, because it is a constant and
- // is guaranteed to be in the application's data segment for
- // the life of the application. It will not be applied here
- // though, since this is suppose to be a simple demo.
- // The advantage of this technique is two fold. A double
- // allocation of the string is performed and the speed in
- // creating the new tree node is much faster since the
- // tree control does not have to allocate memory for the
- // string and copy it.
-
- lpTreeNodeDef->wTextLength = lstrlen(lpTreeNodeDef->lpszText);
-
- // Now it's time to assign bitmap handles created earlier to
- // the first bitmap space which was defined earlier as well.
- // Currently, there are three bitmap spaces allowed to be
- // defined per node. If more are needed, then the source can
- // be purchased and the number of bitmap spaces can be
- // increased. In most case only two bitmaps are good enough.
- // If any of the bitmap spaces are not used or the
- // corresponding bitmap/icon handle will be defined later, then
- // make sure that all unused hBitmap[] and hActiveBitmap[]
- // array elements are set to NULL. The tree control does
- // not check the validity of a bitmap/icon handle.
-
- // Bitmap/icon handles can be assigned to bitmap spaces, at
- // a later time by calling BST_SetBitmap ( ) and/or
- // BST_SetIcon ( ).
- lpTreeNodeDef->chBitmapTypeBits = 0x0000;
-
- lpTreeNodeDef->hBitmap[0] = hbmClosed;
-
- lpTreeNodeDef->hActiveBitmap[0] = hbmClosedActive;
-
- // Notice that the hActiveBitmap[] counterparts are not
- // set to NULL. The tree control will NOT reference
- // their associated hActiveBitmap[] if hBitmap[] is
- // set to NULL.
- lpTreeNodeDef->hBitmap[1] = NULL;
- lpTreeNodeDef->hBitmap[2] = NULL;
-
- // No user-defined data associated with the node.
- lpTreeNodeDef->lpUserData = NULL;
- lpTreeNodeDef->wUserDataSize = 0;
-
- // Since the tree node definition structure is defined then
- // the call to BST_AddChildrenToParent ( ) needs to be
- // performed. Notice the 0L value.
- // This is the indicator that this node is the root. The tree
- // can only have one root. If the root is NOT defined then
- // calling any other tree control exported API will be
- // ignored.
-
- wErrCode = BST_AddChildrenToParent(hwndTree, 0L,1,lpTreeNodeDef);
-
- // HandleAddChildrenError ( ) is defined below.
-
- if(HandleAddChildrenError( hwnd, wErrCode) == 0)
- {
- // No error so continue...
-
- // The lpTreeNode member of the TREE_NODE_DEF structure
- // is an important animal. When
- // BST_AddChildrenToParent ( ) is called with the tree node
- // definitions, the tree control internally allocates the
- // memory to store the tree node definition information, and
- // assigns the pointer to this memory to the tree node
- // definition member 'lpTreeNode'. Remember, the tree node
- // definition structure is shared memory between the
- // application and the tree control. The application will
- // use the 'lpTreeNode' value for future references to the
- // new node such as adding nodes as children.
-
- // Below, the 'lpTreeNode' member of the tree node definition
- // is saved. It will be used as a reference (parent) to add
- // children.
-
- lpParentTreeNode = lpTreeNodeDef->lpTreeNode;
-
-
- // Free the TREE_NODE_DEF allocation for the root since
- // it is not needed anymore.
-
- FREEP(lpTreeNodeDef);
-
- // Next... define the children of the root.
- // Allocate the memory (application grown and owned).
-
- // Note. GlobalAlloc ( ) returns the segment and not the
- // handle of the allocated memory since the GMEM_FIXED flag
- // is applied which requests that the memory block is fixed
- // in memory. Also, since the GMEM_ZEROINIT flag is applied,
- // the memory is zeroed. MAKEP ( ) macro converts this
- // segment into a long pointer (segment:0). The GMEM_SHARE
- // flag is used since the pointer is passed to the tree
- // control in the process of defining new tree nodes.
-
- wMaxChildrenPerNode = MAX_CHILDREN_PER_NODE;
-
- lpTreeNodeDef = (LP_TREE_NODE_DEF)
- MAKEP( GlobalAlloc ( GMEM_FIXED|GMEM_ZEROINIT|GMEM_SHARE,
- MAKELONG(sizeof(TREE_NODE_DEF)*
- wMaxChildrenPerNode ,0)),0);
-
- // Traverse the memory, initializing text, text length,
- // bitmaps, etc.
-
- if ( lpTreeNodeDef )
- {
- lpTreeNodeDef2 = lpTreeNodeDef;
-
- for (i = 0;
- i < wMaxChildrenPerNode;
- i++, lpTreeNodeDef2++)
- {
- if((lpTreeNodeDef2->lpszText =
- MAKEP(GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT|GMEM_SHARE,
- MAX_SAMPLE_TEXT_LENGTH),0)) != NULL)
- {
- // CAUTION: wsprintf has an internal buffer size
- // limit.
- wsprintf (lpTreeNodeDef2->lpszText, "%s %d.",
- (LPSTR) SAMPLE_TEXT, i);
- lpTreeNodeDef2->wTextLength
- = lstrlen(lpTreeNodeDef2->lpszText);
-
- lpTreeNodeDef2->chBitmapTypeBits = 0x0000;
-
- lpTreeNodeDef2->hBitmap[0] = hbmClosed;
- lpTreeNodeDef2->hActiveBitmap[0] = hbmClosedActive;
-
- lpTreeNodeDef2->hBitmap[1] = NULL;
- lpTreeNodeDef2->hBitmap[2] = NULL;
-
- // No user-defined data associated with the node.
- lpTreeNodeDef2->lpUserData = NULL;
- lpTreeNodeDef2->wUserDataSize = 0;
-
- }
- else
- {
- wMaxChildrenPerNode = i;
- MessageBox ( hwnd,
- "Out of memory.",
- "WM_CREATE",
- MB_OK);
- }
- }
-
- // Add the children to the parent 'lpTreeNode'.
- // We do not need to store the tree node pointers that
- // are returned in the 'lpTreeNode' member of the
- // TREE_NODE_DEF structures.
- // This application will rely on the notification message
- // that will be sent to the application as a result of
- // an event, such as a double click of the mouse. The
- // notification message will supply the pointer to the
- // tree node involved in the event.
-
- wErrCode = BST_AddChildrenToParent( hwndTree,
- lpParentTreeNode, // Parent which is the root
- wMaxChildrenPerNode,
- lpTreeNodeDef);
-
- if(HandleAddChildrenError( hwnd, wErrCode) == 0)
- {
- // No error so the parent tree node now
- // has children so open the folder.
- BST_SetBitmapAndActiveBitmap( hwndTree,
- 0,
- lpParentTreeNode,
- hbmOpen,
- hbmOpenActive);
-
- }
-
- // Traverse through the TREE_NODE_DEF structures,
- // freeing the string memory. Once this is completed
- // then the TREE_NODE_DEF array needs to be freed.
- lpTreeNodeDef2 = lpTreeNodeDef;
-
- for (i = 0;
- i < wMaxChildrenPerNode;
- i++, lpTreeNodeDef2++)
- {
- FREEP(lpTreeNodeDef2->lpszText);
- }
- FREEP(lpTreeNodeDef);
- }
- else
- {
- MessageBox (hwnd, "Out of memory.", "WM_CREATE",MB_OK);
- }
- // Set focus to the tree control, show it, and let
- // the user knock herself or himself out.
-
- SetFocus (hwndTree);
- ShowWindow (hwndTree, SW_SHOW);
- lResult = 0L;
- }
- }
- else
- {
- MessageBox (hwnd, "Out of memory.", "WM_CREATE",MB_OK);
- }
- }
- return lResult ;
-
-
- case WM_BST_SELECT_NOTIF_DBLCLK:
-
- // WM_BST_SELECT_NOTIF_DBLCLK is a notification message sent to
- // the application when the user:
- // 1) double clicks on a tree node,
- // 2) hits the carriage return (acts as if the currently highlighted
- // node was double clicked on.),
- // 3) hits the '+' key if the currently highlighted node has children,
- // 4) hits the '-' key if the currently highlighted node has no
- // children.
- // lParam is a pointer to a SELECT_NOTIF notification structure which
- // is owned by the tree control. DO NOT FREE THIS MEMORY OR ALL
- // HELL WILL BREAK LOOSE. The members of the SELECT_NOTIF structure
- // consist of the pointer to the tree node that was selected and a
- // flags member that describes what region of the tree node which
- // was clicked and if the tree node had children or not (OPEN or
- // CLOSED).
-
- lpSelectNotif = (LP_SELECT_NOTIF) lParam;
-
- // lpParentTreeNode is the pointer to the tree control owned
- // TREE_NODE structure that describes the tree node that was selected.
-
- lpParentTreeNode = lpSelectNotif->lpTreeNode;
-
- // Is the selected tree node OPENED? OPENED means that the
- // selected tree node has children. CLOSED means that the selected
- // tree node has no children.
-
- if(lpSelectNotif->wFlags & NODE_OPENED)
- {
- // Yes... then delete the children. wParam is the handle to
- // the tree control windows that generated this notification
- // message.
-
- BST_DeleteChildrenOfParent( (HWND)wParam, lpParentTreeNode);
-
- // Close the folder
- BST_SetBitmapAndActiveBitmap( wParam,
- 0,
- lpParentTreeNode,
- hbmClosed,
- hbmClosedActive);
- }
- else
- {
- // No... then open it and add children if this tree node gots them.
-
-
- // Define the children as in WM_CREATE: but use the
- // tree node pointer defined in the SELECT_NOTIF structure
- // member 'lpTreeNode' as the parent.
-
- wMaxChildrenPerNode = MAX_CHILDREN_PER_NODE;
-
- lpTreeNodeDef = (LP_TREE_NODE_DEF)
- MAKEP(GlobalAlloc( GPTR | GMEM_SHARE,
- MAKELONG(sizeof(TREE_NODE_DEF)* wMaxChildrenPerNode ,0)),0);
-
- if( lpTreeNodeDef )
- {
- lpTreeNodeDef2 = lpTreeNodeDef;
-
- for (i = 0;
- i < wMaxChildrenPerNode;
- i++, lpTreeNodeDef2++)
- {
- if((lpTreeNodeDef2->lpszText =
- MAKEP(GlobalAlloc(GPTR|GMEM_SHARE,
- MAX_SAMPLE_TEXT_LENGTH),0)) != NULL)
- {
- wsprintf (lpTreeNodeDef2->lpszText, "%s %d.",
- (LPSTR) SAMPLE_TEXT, i);
-
- lpTreeNodeDef2->wTextLength
- = lstrlen(lpTreeNodeDef2->lpszText);
-
- lpTreeNodeDef2->chBitmapTypeBits = 0x0000;
-
- lpTreeNodeDef2->hBitmap[0] = hbmClosed;
- lpTreeNodeDef2->hActiveBitmap[0] = hbmClosedActive;
-
- lpTreeNodeDef2->hBitmap[1] = NULL;
- lpTreeNodeDef2->hBitmap[2] = NULL;
-
- // No user-defined data associated with the node.
- lpTreeNodeDef2->lpUserData = NULL;
- lpTreeNodeDef2->wUserDataSize = 0;
- }
- else
- {
- wMaxChildrenPerNode = i;
- MessageBox ( hwnd,
- "Out of memory.",
- "WM_BST_SELECT_NOTIF_DBLCLK",
- MB_OK);
- }
- }
- // Add 'em ...
-
- wErrCode = BST_AddChildrenToParent( wParam,
- lpParentTreeNode,
- wMaxChildrenPerNode,
- lpTreeNodeDef);
-
- if(HandleAddChildrenError( wParam, wErrCode) == 0)
- {
- // If not error then open the folder
- BST_SetBitmapAndActiveBitmap( wParam,
- 0,
- lpParentTreeNode,
- hbmOpen,
- hbmOpenActive);
- }
- lpTreeNodeDef2 = lpTreeNodeDef;
-
- for (i = 0;
- i < wMaxChildrenPerNode;
- i++, lpTreeNodeDef2++)
- {
- FREEP(lpTreeNodeDef2->lpszText);
- }
- FREEP(lpTreeNodeDef);
- }
- else
- {
- MessageBox ( hwnd,
- "Out of memory.",
- "WM_BST_SELECT_NOTIF_DBLCLK",
- MB_OK);
- }
- }
- return 0;
-
-
- case WM_SYSCOLORCHANGE:
-
- // Sad but true... The WM_SYSCOLORCHANGE notification has be sent
- // to the tree control since only top level windows get this
- // notification. Since the colors are going to change in the
- // tree control, better make sure that the background color of
- // the bitmaps is correct.
-
- if(hwndTree)
- {
- ChangeBitmapToMatchBackground ( GetSysColor (COLOR_WINDOW),
- hbmClosed,
- IDR_CLOSED_FOLDER);
- ChangeBitmapToMatchBackground ( GetSysColor (COLOR_WINDOW),
- hbmOpen,
- IDR_OPEN_FOLDER);
- ChangeBitmapToMatchBackground ( GetSysColor (COLOR_HIGHLIGHT),
- hbmClosedActive,
- IDR_CLOSED_FOLDER);
- ChangeBitmapToMatchBackground ( GetSysColor (COLOR_HIGHLIGHT),
- hbmOpenActive,
- IDR_OPEN_FOLDER);
- SendMessage (hwndTree, message, wParam, lParam);
- }
- return 0;
-
- case WM_SETFOCUS:
- // Keep the focus on the tree control.
- SetFocus(hwndTree);
- break;
-
- case WM_SIZE:
- // Keep the tree control in the client area of the
- // MDI child window.
- cx = GetSystemMetrics ( SM_CXVSCROLL) / 2;
- cy = GetSystemMetrics ( SM_CYHSCROLL) / 2;
- MoveWindow ( hwndTree,
- cx,
- cy,
- max(min(450,LOWORD(lParam) - 2 * cx), 0),
- max(HIWORD(lParam) - 2 * cy, 0),
- TRUE);
- break;
-
- case WM_DESTROY:
- BST_EraseTree( hwndTree);
-
- DeleteObject(hbmClosed);
- DeleteObject(hbmClosedActive);
- DeleteObject(hbmOpen);
- DeleteObject(hbmOpenActive);
- PostQuitMessage (0) ;
- return 0 ;
- }
- return DefWindowProc (hwnd, message, wParam, lParam) ;
- }
-
-
-
-
- char * npszAddChildrenErrors [] =
- {
- "Memory allocation failure.",
- "Level limit exceeded.",
- "The number of nodes supported has been exceeded.",
- "Only on root per tree.",
- "Given parent node is invalid.",
- "Invalid error code"
- };
-
- WORD HandleAddChildrenError( HWND hwnd, WORD wErrCode)
- {
- WORD i;
-
- switch (wErrCode)
- {
- case BST_NO_ERROR:
- i = 0;
- break;
-
- case BST_ERR_MEMORY_ALLOC_FAILED:
- i = 1;
- break;
-
- case BST_ERR_LEVEL_LIMIT_EXCEEDED:
- i = 2;
- break;
-
- case BST_ERR_TOO_MANY_NODES:
- i = 3;
- break;
-
- case BST_ERR_ONLY_ONE_ROOT_ALLOWED:
- i = 4;
- break;
-
- case BST_ERR_INVALID_PARENT_FOR_INSERTION:
- i = 5;
- break;
-
- default:
- i = 6;
- break;
- }
- if ( i != 0)
- {
- MessageBox ( hwnd,
- npszAddChildrenErrors[i-1],
- "BST_AddChildrenToParent",
- MB_OK);
- }
- return i;
- }
-
-
-
-
-
- /****************************************************************************
- Below are support routines for the 16 color folder bitmaps used in the
- tree.
- Enjoy!
- ****************************************************************************/
- HBITMAP CreateBackgroundMatchedBitmap (DWORD dwrgbBackground, int nBitmapResouce)
- {
- HANDLE hBitmap;
- HANDLE hRes;
- HANDLE hResMem;
- LPBITMAPINFOHEADER lpbi;
- DWORD FAR * lpColorTable;
- LPSTR lpBits;
- int bc;
- HDC hdc;
-
- hRes = FindResource ( hInst,
- MAKEINTRESOURCE (nBitmapResouce),
- RT_BITMAP);
-
- hResMem = LoadResource (hInst, hRes);
- lpbi = (LPBITMAPINFOHEADER) LockResource (hResMem);
-
- lpColorTable = (DWORD FAR *)(lpbi + 1);
- lpBits = (LPSTR) (lpColorTable + 16);
-
- bc = (lpBits[0] & 0xF0) >> 4;
-
- lpColorTable [bc] = RGB( GetBValue (dwrgbBackground),
- GetGValue (dwrgbBackground),
- GetRValue (dwrgbBackground));
- hdc = GetDC(NULL);
- hBitmap = CreateDIBitmap ( hdc,
- lpbi,
- (DWORD) CBM_INIT,
- lpBits,
- (LPBITMAPINFO) lpbi,
- DIB_RGB_COLORS);
- ReleaseDC(NULL, hdc);
- UnlockResource (hResMem);
- FreeResource(hResMem);
- return hBitmap;
- }
-
-
- char rgchBits [2048];
-
- short ChangeBitmapToMatchBackground ( DWORD dwrgbBackground,
- HANDLE hBitmap,
- int nBitmapResouce)
- {
- int bc;
- HDC hdc;
- LPBITMAPINFO lpbi;
- HANDLE hRes;
- HANDLE hResMem;
-
- hRes = FindResource ( hInst,
- MAKEINTRESOURCE (nBitmapResouce),
- RT_BITMAP);
-
- hResMem = LoadResource (hInst, hRes);
- lpbi = ( LPBITMAPINFO ) LockResource (hResMem);
-
- hdc = GetDC (NULL);
-
- GetDIBits ( hdc,
- hBitmap,
- 0,
- LOWORD(lpbi->bmiHeader.biHeight),
- rgchBits,
- lpbi,
- DIB_RGB_COLORS);
-
- bc = (rgchBits[0] & 0xF0) >> 4;
-
- lpbi->bmiColors[bc].rgbBlue = GetBValue (dwrgbBackground);
- lpbi->bmiColors[bc].rgbGreen = GetGValue (dwrgbBackground);
- lpbi->bmiColors[bc].rgbRed = GetRValue (dwrgbBackground);
- lpbi->bmiColors[bc].rgbReserved = 0;
-
- SetDIBits(hdc,
- hBitmap,
- 0,
- LOWORD(lpbi->bmiHeader.biHeight),
- rgchBits,
- lpbi,
- DIB_RGB_COLORS);
-
- ReleaseDC(NULL, hdc );
- UnlockResource (hResMem);
- FreeResource(hResMem);
- return 0;
- }
-
- /*--------------------------------- EOF -----------------------------------*/
-
-