winapi listview检查项目c ++

时间:2013-04-03 12:14:03

标签: c++ winapi listview

我使用minGW32编译器。 我创建了一个带有如下复选框的ListView:

HWND hListView = CreateWindowExW(0, WC_LISTVIEWW, L"",
    WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
    10,10,500,500, hwnd, (HMENU)ID_LISTVIEW, GetModuleHandle(NULL), NULL);
    SendMessage(hListView, WM_SETFONT, (WPARAM) font, TRUE);

ListView_SetExtendedListViewStyle(hListView, LVS_EX_CHECKBOXES |  LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES/*LVSCW_AUTOSIZE_USEHEADER*/);

lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH  | LVCF_FMT;
lvc.fmt  = LVCFMT_LEFT;

我想检查何时选中任何复选框。例如。如果我选中任何复选框,请显示一个消息框:

I use this WM_NOTIFY:
case WM_NOTIFY:
 {
  switch (LOWORD(wParam))
  {
   case ID_LISTVIEW:
   {
   switch (((LPNMHDR) lParam)->code)
   {
    case LVN_ITEMCHANGED :
    MessageBoxW( NULL, (LPCWSTR)lpMsgBuf, L"Error", MB_OK | MB_ICONERROR ); 
    }
    break; 
   }
  break;
  }
  break;
 }
 break;

但是这件事发生了。单击ListView的空白区域或单击任何项​​目时。但是,我希望仅在复选框更改时运行消息框。

1 个答案:

答案 0 :(得分:3)

LPNMLISTVIEW pnmv = (LPNMLISTVIEW) lParam; 

if(pnmv->uChanged & LVIF_STATE) // item state has been changed
{
   switch(pnmv->uNewState & LVIS_OVERLAYMASK)
   {
   case INDEXTOSTATEIMAGEMASK(2):
      // pnmv->iItem was checked
      break;
   case INDEXTOSTATEIMAGEMASK(1):
      //pnmv->iItem was unchecked
      break;
   }
}