共用方式為


如何將項目新增至標題控制項

本主題示範如何將項目新增至標頭控件。 標頭控件通常有數個標頭專案,可定義控件的數據行。 您可以將 HDM_INSERTITEM 訊息傳送至控制項,將項目新增至標頭控制項。

您需要知道的事項

技術

先決條件

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

說明

使用 HDM_INSERTITEM 訊息將項目新增至標頭控制項。 訊息必須包含 HDITEM 結構的位址。 此結構會定義標頭項目的屬性,其中包含字串、點陣圖影像、初始大小,以及應用程式定義的32位值。

下列範例說明如何使用 HDM_INSERTITEM 訊息和 HDITEM 結構,將專案新增至標頭控件。 新項目是由位於項目矩形內左對齊的字串組成。

// DoInsertItem - inserts an item into a header control. 
// Returns the index of the new item. 
// hwndHeader - handle to the header control. 
// iInsertAfter - index of the previous item. 
// nWidth - width of the new item. 
// lpsz - address of the item string. 
int DoInsertItem(HWND hwndHeader, int iInsertAfter, 
    int nWidth, LPTSTR lpsz) 
{ 
    HDITEM hdi; 
    int index; 
 
    hdi.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH; 
    hdi.cxy = nWidth; 
    hdi.pszText = lpsz; 
    hdi.cchTextMax = sizeof(hdi.pszText)/sizeof(hdi.pszText[0]); 
    hdi.fmt = HDF_LEFT | HDF_STRING; 
 
    index = SendMessage(hwndHeader, HDM_INSERTITEM, 
        (WPARAM) iInsertAfter, (LPARAM) &hdi); 
 
    return index; 
}

關於標頭控件的

標頭控件參考

使用標頭控件