Tree control is a control that does not support self-painting. This makes it difficult to display background images. But in any case, it can be done, and we will discuss how to do it. A basic method is to draw the tree control on a memory device and overwrite the background image transparently. The result is that the background image is displayed in the Control Service area. Of course, there are few such materials.
Tree control uses background bitmap. A good use is to display company trademarks. In addition, we must ensure that the bitmap used does not affect our reading.
First, we require that the image has been added to the project as a resource. We can useBitmap of 256 colors (16 colors should also be supported ). If the added image is smaller than the control area, the image is tiled in the Control Service area.
Procedure1: Add bitmap to resource
Use the import function to add bitmap to the resource editor.
Procedure2: Add variable
Protected:
CPalette m_pal;
CBitmap m_bitmap;
Int m_cxBitmap, m_cyBitmap;
Procedure3: Add a member function to set the background image.
We have added two overload functions to set the background image. These two functions should be public functions. The first function parameter is the resource.ID. The second function parameter is the resource name.
These functions can be called to change a specified bitmap. First, they will deleteExisting bitmap and palette on the GDI object. Then they load bitmaps and attach them to the CBitmap object. We call the global function: LoadImage () instead of CBitmap: LoadBitmap (). The reason for this is that we need to access the DIBSECTION of the bitmap. Why do we need to access the DIBSECTION of the bitmap? This is because we need to create a logical palette that matches the bitmap color. If you do not set or use this logical palette, the image looks very DULL. We also save the size of the bitmap for future use.
If the bitmap is256 colors or less. We will create a color palette. We allocate enough space to save the bitmap color table and call the: GetDIBColorTable () function to obtain it from the bitmap. We allocate enough memory to create the color palette and obtain the color palette information from the bitmap color table. The color palette version should be 0x300.
BOOL CTreeCtrlX: SetBkImage (UINT nIDResource)
{
Return SetBkImage (LPCTSTR) nIDResource );
}
BOOL CTreeCtrlX: SetBkImage (LPCTSTR lpszResourceName)
{
// If this is not the first call then Delete GDI objects
If (m_bitmap.m_hObject! = NULL)
M_bitmap.DeleteObject ();
If (m_pal.m_hObject! = NULL)
M_pal.DeleteObject ();
HBITMAP hBmp = (HBITMAP): LoadImage (AfxGetInstanceHandle (),
LpszResourceName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
If (hBmp = NULL)
Return FALSE;
M_bitmap.Attach (hBmp );
BITMAP bm;