WC_LISTVIEW意外行为

时间:2011-11-16 13:08:47

标签: winapi listview

我正在尝试使用win32 WC_LISTVIEW创建表:

    param_table = CreateWindowEx( 0, WC_LISTVIEW, 0, 
          LVS_REPORT | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES |
          WS_VISIBLE | WS_CHILD | CBS_AUTOHSCROLL | LBS_MULTICOLUMN, 
          310, 31, 252, 114, hWnd, ( HMENU )PARAM_TABLE, hInst, 0 );

    SendMessage(param_table,LVM_SETTEXTBKCOLOR, 0,(LPARAM)CLR_HILIGHT);
    SendMessage(param_table,LVM_SETEXTENDEDLISTVIEWSTYLE,
           0,LVS_EX_FULLROWSELECT); // Set style

    LVCOLUMN LvCol;
    memset(&LvCol,0,sizeof(LvCol));
    LvCol.mask=LVCF_TEXT|LVCF_WIDTH|LVCF_FMT|LVCF_SUBITEM; 
    LvCol.iSubItem = 0; 
    LvCol.pszText=_T("value");                     
    LvCol.cx=114;
    SendMessage(param_table,LVM_INSERTCOLUMN,0,(LPARAM)&LvCol); 
    LvCol.pszText=_T("parameter");                          
    LvCol.iSubItem = 1;
    SendMessage(param_table,LVM_INSERTCOLUMN,0,(LPARAM)&LvCol); 

    LVITEM LvItem;
    memset(&LvItem,0,sizeof(LvItem));
    LvItem.mask=LVIF_TEXT;
    LvItem.cchTextMax = 256;
    LvItem.iItem=0;    
    LvItem.iSubItem=0; 
    LvItem.pszText=_T("Item");
    SendMessage(param_table,LVM_INSERTITEM,0,(LPARAM)&LvItem);          
    LvItem.iSubItem=1;    
    LvItem.pszText=_T("subItem"); 
    SendMessage(param_table,LVM_INSERTITEM,0,(LPARAM)&LvItem);

但我无法得到我期望的结果。只添加了一列。未添加SubItem。

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我猜你也必须使用LVM_SETITEMTEXT。我是通过ListView方法完成的,但它应该是相同的

void addRow (HWND hwnd, vector<string> & colValues)
{
   if (colValues.size () < 1) return;
   int reto = 0;
   LVITEM ite = { 0 };

   ite.mask = LVIF_TEXT;
   ite.pszText = (LPSTR) colValues[0].c_str ();
   reto = ListView_InsertItem (hwnd, &ite);   
   if (reto >= 0)
      for (int cc = 1; cc < colValues.size (); cc ++)
      {
         ListView_SetItemText (hwnd, reto, cc, (LPSTR) colValues[cc].c_str ());
      }
}

如果决定使用该方法,这里是setColumns方法

void setColumns (HWND hwnd, vector<string> & colNames)
{
   LVCOLUMN col;
   int chx = LOWORD (GetDialogBaseUnits ()) ;

   for (int cc = 0; cc < colNames.size (); cc ++)
   {
      col.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
      col.fmt = LVCFMT_LEFT;
      col.cx = chx * 1.2 * colNames[cc].length ();
      col.pszText = (LPSTR) colNames[cc].c_str ();
      col.iSubItem = cc;
      ListView_InsertColumn(hwnd, cc, & col);
   }
}