Click here to Skip to main content
Licence 
First Posted 31 Aug 2004
Views 83,580
Bookmarked 44 times

A ListView Dialog using Win32 API

By | 31 Aug 2004 | Article
A Dialog using Listview in report style and icon style

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

About the Author

zjsmile



China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionMSDN ListView sample [modified] PinmemberSergey Chepurin23:57 20 Feb '12  
Generalits very useful PinmemberChenier2:10 23 Nov '08  
GeneralVery helpful! Pinmemberhoctro20:33 30 May '08  
Generalthat I am finding Pinmembersophiya chen20:34 24 Oct '07  
GeneralDeath to MFC Pinmemberjostmey14:12 23 Jul '05  
GeneralJust use MFC!! PinmemberMiguel Lopes6:26 4 Sep '04  
GeneralRe: Just use MFC!! PinmemberDante Shamest5:05 5 Sep '04  
GeneralRe: Just use MFC!! PinmemberMiguel Lopes10:16 5 Sep '04  
GeneralRe: Just use MFC!! PinPopularmemberDante Shamest17:56 5 Sep '04  
GeneralRe: Just use MFC!! Pinmemberme9408014:49 10 Oct '04  
GeneralRe: Just use MFC!! Pinmembereuacela10:40 12 Sep '04  
GeneralI like Win32 - better than MFC in many aspects Pinsussme94043-John14:38 10 Oct '04  
GeneralRe: Just use MFC!! PinmemberMsftone12:34 21 Nov '04  
GeneralRe: Just use MFC!! Pinmemberkfevac16:14 15 Apr '05  
GeneralRe: Just use MFC!! PinmemberMiguel Lopes4:07 17 Apr '05  
GeneralRe: Just use MFC!! PinmemberWooferByte8:46 14 May '05  
GeneralRe: Just use MFC!! PinsussJoe Reguiers19:59 14 May '05  
GeneralRe: Just use MFC!! PinmemberJohn Dixson6:06 25 May '05  
GeneralRe: Just use MFC!! Pinmemberfuckithollyshit22:19 26 Jun '05  
GeneralRe: Just use MFC!! Pinmemberfputil23:25 11 Jun '06  
GeneralThat's just rude PinmemberDavid Nash18:22 27 Dec '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 1 Sep 2004
Article Copyright 2004 by zjsmile
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid