如何初始化映像清單
樹視圖控件中的每個專案都可以有兩個相關聯的影像。 專案會在選取影像時顯示一個影像,另一個影像則不選取。 若要包含具有樹視圖專案的影像,請先使用 影像清單 函式來建立影像清單,並將影像新增至其中。 然後使用TVM_SETIMAGELIST訊息,將影像清單與樹視圖控件產生關聯。
您需要知道的事項
技術
必要條件
- C/C++
- Windows 使用者介面程序設計
指示
初始化映像清單
下列範例會建立影像清單、將三個位圖新增至清單,並將影像清單與樹視圖控件產生關聯。
// InitTreeViewImageLists - creates an image list, adds three bitmaps
// to it, and associates the image list with a tree-view control.
// Returns TRUE if successful, or FALSE otherwise.
// hwndTV - handle to the tree-view control.
//
// Global variables and constants:
// g_hInst - the global instance handle.
// g_nOpen, g_nClosed, and g_nDocument - global indexes of the images.
// CX_BITMAP and CY_BITMAP - width and height of an icon.
// NUM_BITMAPS - number of bitmaps to add to the image list.
// IDB_OPEN_FILE, IDB_CLOSED_FILE, IDB_DOCUMENT -
// resource identifiers of the bitmaps.
BOOL InitTreeViewImageLists(HWND hwndTV)
{
HIMAGELIST himl; // handle to image list
HBITMAP hbmp; // handle to bitmap
// Create the image list.
if ((himl = ImageList_Create(CX_BITMAP,
CY_BITMAP,
FALSE,
NUM_BITMAPS, 0)) == NULL)
return FALSE;
// Add the open file, closed file, and document bitmaps.
hbmp = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_OPEN_FILE));
g_nOpen = ImageList_Add(himl, hbmp, (HBITMAP)NULL);
DeleteObject(hbmp);
hbmp = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_CLOSED_FILE));
g_nClosed = ImageList_Add(himl, hbmp, (HBITMAP)NULL);
DeleteObject(hbmp);
hbmp = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_DOCUMENT));
g_nDocument = ImageList_Add(himl, hbmp, (HBITMAP)NULL);
DeleteObject(hbmp);
// Fail if not all of the images were added.
if (ImageList_GetImageCount(himl) < 3)
return FALSE;
// Associate the image list with the tree-view control.
TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
return TRUE;
}
相關主題