Click here to Skip to main content
15,890,399 members
Articles / Programming Languages / C++
Article

A ListView Dialog using Win32 API

Rate me:
Please Sign up or sign in to vote.
3.44/5 (24 votes)
31 Aug 2004 141.7K   7.5K   54   22
A Dialog using Listview in report style and icon style

Image 1

Introduction

This program uses a listview in a dialog, by changing the tab, it can reappear in report style and icon style. It explains these problems in using listview:

  1. how can you change the listview's style
  2. how to get the data that item you clicked;
  3. how to remove the icon in every item
  4. how to change bk-color and text color in every item
  5. how to disable the mouse left and right key

Background

I received a project, and had to code a win32 dialog using listview. But when I searched the web, there are so few articles and code, which was using listview in win32. Almost of them are in MFC.

Using the code

//
BOOL CALLBACK TableProc(HWND hDlg, UINT message, 
  WPARAM wParam, LPARAM lParam)
{
  int iIndex;
  LPNMLISTVIEW pnm;
  TCHAR *pVarName = NULL;
  POINT pt;
  static RECT lstRect;
  switch(message)
  {
  case WM_INITDIALOG:
    SendMessage(hDlg, WM_SETREDRAW, FALSE, 0);
    hListTab = GetDlgItem(hDlg, IDC_LISTTAB);
    InitListTab(hListTab);
    hTableList = GetDlgItem(hDlg, IDC_TABLELIST);
    InitTableImageList(hTableList);
    InitTableList(hTableList);
    InitTableDlg(hDlg);
        SetFocus(hTableList);
    SendMessage(hTableList, WM_SETREDRAW, TRUE, 0);
    GetWindowRect(hTableList, &lstRect);
    return TRUE;
  case WM_COMMAND:
    if(LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    {
      PostQuitMessage(0);
      EndDialog(hDlg, 0);
      return TRUE;
    }
    break;
  case WM_NCHITTEST:
    pt.x = LOWORD(lParam);
    pt.y = HIWORD(lParam);
    if(pt.x >= lstRect.left && pt.x <= lstRect.right &&
       pt.y >= lstRect.top && pt.y <= lstRect.right)
    {
      return (LRESULT)HTERROR;
    }
    break;
  case WM_NOTIFY:
    switch(LOWORD(wParam))
    {
    case IDC_TABLELIST:
      pnm = (LPNMLISTVIEW)lParam;
            if(pnm->hdr.hwndFrom == hTableList &&pnm->hdr.code == NM_CUSTOMDRAW)
            {
                SetWindowLong(hDlg, DWL_MSGRESULT, (LONG)TableDraw(lParam));
                return TRUE;
            }
      if(((LPNMHDR)lParam)->code == NM_CLICK)
      {
        // 1. get current selection
        iIndex = (int)SendMessage(hTableList, LVM_GETNEXTITEM, -1, LVNI_FOCUSED);
        if(iIndex == -1)
          return FALSE;
        TCHAR itemTotle[MAX_PATH] = {0};
        GetItemText(hTableList, iIndex, itemTotle);
        return FALSE;
      }
      // here you must use LVN_ITEMCHANGED not LVN_ITEMCHANGING
      // because LVN_ITEMCHANGING is before focu on you clicked item;
      // LVN_ITEMCHANGED is after focu on you clicked item but before disapear it
      if(((LPNMHDR)lParam)->code == LVN_ITEMCHANGED)
      {
        iIndex = (int)SendMessage(hTableList, LVM_GETNEXTITEM, -1, LVNI_FOCUSED);
        if(iIndex == -1)
          return FALSE;
        ListView_SetItemState(hTableList, iIndex, 0, LVIS_SELECTED | LVIS_FOCUSED);
        return TRUE;
      }
      break;
    case IDC_LISTTAB:
      if(((LPNMHDR)lParam)->code == TCN_SELCHANGE)
      {
        OnSelchangeListCtrlMode(hDlg);
        return TRUE;
      }
      break;
    }
    break;
  }
  return FALSE;
}
//

Updates

I will keep a running update of any changes or improvements to this simple program. I think it must be useful to those who want to use listview in Win32.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhi!I hava a question about Sequence! Pin
forcj92813-Nov-13 15:17
forcj92813-Nov-13 15:17 
QuestionMSDN ListView sample Pin
Sergey Chepurin20-Feb-12 23:57
Sergey Chepurin20-Feb-12 23:57 
Generalits very useful Pin
Chenier23-Nov-08 2:10
Chenier23-Nov-08 2:10 
GeneralVery helpful! Pin
hoctro30-May-08 20:33
hoctro30-May-08 20:33 
Generalthat I am finding Pin
Sophiya Chen24-Oct-07 20:34
Sophiya Chen24-Oct-07 20:34 
GeneralDeath to MFC Pin
jostmey23-Jul-05 14:12
jostmey23-Jul-05 14:12 
GeneralJust use MFC!! Pin
Miguel Lopes4-Sep-04 6:26
Miguel Lopes4-Sep-04 6:26 
Yes, MFC CListCtrl has a lot of bugs, but you can easly derive your version of CListCtrl with corrected bugs.

DONT REINVENT THE WHEEL!!!
GeneralRe: Just use MFC!! Pin
Dante Shamest5-Sep-04 5:05
Dante Shamest5-Sep-04 5:05 
GeneralRe: Just use MFC!! Pin
Miguel Lopes5-Sep-04 10:16
Miguel Lopes5-Sep-04 10:16 
GeneralRe: Just use MFC!! Pin
Dante Shamest5-Sep-04 17:56
Dante Shamest5-Sep-04 17:56 
GeneralRe: Just use MFC!! Pin
me-mtview10-Oct-04 14:49
me-mtview10-Oct-04 14:49 
GeneralRe: Just use MFC!! Pin
gamitech12-Sep-04 10:40
gamitech12-Sep-04 10:40 
GeneralI like Win32 - better than MFC in many aspects Pin
me-mtview10-Oct-04 14:38
me-mtview10-Oct-04 14:38 
GeneralRe: Just use MFC!! Pin
Msftone21-Nov-04 12:34
Msftone21-Nov-04 12:34 
GeneralRe: Just use MFC!! Pin
kfevac15-Apr-05 16:14
kfevac15-Apr-05 16:14 
GeneralRe: Just use MFC!! Pin
Miguel Lopes17-Apr-05 4:07
Miguel Lopes17-Apr-05 4:07 
GeneralRe: Just use MFC!! Pin
WooferByte14-May-05 8:46
WooferByte14-May-05 8:46 
GeneralRe: Just use MFC!! Pin
Joe Reguiers14-May-05 19:59
Joe Reguiers14-May-05 19:59 
GeneralRe: Just use MFC!! Pin
csharpsucks25-May-05 6:06
csharpsucks25-May-05 6:06 
GeneralRe: Just use MFC!! Pin
fuckithollyshit26-Jun-05 22:19
fuckithollyshit26-Jun-05 22:19 
GeneralRe: Just use MFC!! Pin
fputil11-Jun-06 23:25
fputil11-Jun-06 23:25 
GeneralThat's just rude Pin
David Nash27-Dec-06 18:22
David Nash27-Dec-06 18:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.