如何使用狀態影像索引

在樹視圖控件中設定和擷取狀態影像索引時,通常會造成混淆。 下列範例示範設定和擷取狀態映射索引的適當方法。 這些範例假設樹視圖控件中只有兩個狀態影像索引,但未核取和核取。 如果您的應用程式包含兩個以上的函式,則必須修改這些函式來處理該案例。

您需要知道的事項

技術

必要條件

  • C/C++
  • Windows 使用者介面程序設計

指示

設定樹檢視項目的檢查狀態

下列範例示範如何設定樹視圖項目的檢查狀態。

  BOOL TreeView_SetCheckState(HWND hwndTreeView, HTREEITEM hItem, BOOL fCheck)
  {
      TVITEM tvItem;

      tvItem.mask   = TVIF_HANDLE | TVIF_STATE;
      tvItem.hItem  = hItem;
      tvItem.stateMask  = TVIS_STATEIMAGEMASK;

      // Image 1 in the tree-view check box image list is the unchecked box. 
      // Image 2 is the checked box.

      tvItem.state = INDEXTOSTATEIMAGEMASK((fCheck ? 2 : 1));

      return TreeView_SetItem(hwndTreeView, &tvItem);
  }

擷取樹視圖項目的檢查狀態

下列範例示範如何擷取樹視圖專案的檢查狀態。

  BOOL TreeView_GetCheckState(HWND hwndTreeView, HTREEITEM hItem)
  {
      TVITEM tvItem;

      // Prepare to receive the desired information.
      tvItem.mask   = TVIF_HANDLE | TVIF_STATE;
      tvItem.hItem  = hItem;
      tvItem.stateMask  = TVIS_STATEIMAGEMASK;

      // Request the information.
      TreeView_GetItem(hwndTreeView, &tvItem);

      // Return zero if it's not checked, or nonzero otherwise.
      return ((BOOL)(tvItem.state >> 12) - 1);
  }

使用樹視圖控件

CustDTv 範例說明樹視圖控件中的自定義繪製