2019年4月16日 星期二

Windows C++ MFC Tiles-View List Control

http://codexpert.ro/blog/2013/10/05/tiles-view-list-control/

Introduction


In Visual Studio Community 2017, create a new project of Visual C++ > MFC/ATL > MFC Application > MFCApplication1.

Choose "Single Document", Advanced features: uncheck all except "Common Control Manifest", uncheck all "Advnaced frame panes", choose Generated Classes's Be class: CListView.










F


Core Code:



private:
CImageList m_imageList;
CImageList m_imageListSmall;
BOOL _SetTilesViewLinesCount(int nCount) {
CListCtrl& listCtrl = GetListCtrl();

LVTILEVIEWINFO lvtvwi = { 0 };
lvtvwi.cbSize = sizeof(LVTILEVIEWINFO);
lvtvwi.dwMask = LVTVIM_COLUMNS;
lvtvwi.cLines = nCount;

return listCtrl.SetTileViewInfo(&lvtvwi);
}
BOOL _SetTilesViewTileFixedWidth(int nWidth) {
CListCtrl& listCtrl = GetListCtrl();

LVTILEVIEWINFO lvtvwi = { 0 };
lvtvwi.cbSize = sizeof(LVTILEVIEWINFO);
lvtvwi.dwMask = LVTVIM_TILESIZE;

lvtvwi.dwFlags = LVTVIF_FIXEDWIDTH;
lvtvwi.sizeTile.cx = nWidth;
lvtvwi.sizeTile.cy = 0;

return listCtrl.SetTileViewInfo(&lvtvwi);
}
BOOL _SetItemTileLines(int iItem, UINT* parrColumns, UINT nCount) {
CListCtrl& listCtrl = GetListCtrl();

LVTILEINFO lvti = { 0 };
lvti.cbSize = sizeof(LVTILEINFO);
lvti.cColumns = nCount;
lvti.iItem = iItem;
lvti.puColumns = parrColumns;

return listCtrl.SetTileInfo(&lvti);
}

void CMFCApplication1View::OnInitialUpdate()
{
CListView::OnInitialUpdate();

CListCtrl& listCtrl = GetListCtrl();
listCtrl.SetExtendedStyle(LVS_EX_GRIDLINES);

ASSERT(NULL == m_imageList.m_hImageList);      // init only once
ASSERT(NULL == m_imageListSmall.m_hImageList); // init only once

CWinApp* pApp = AfxGetApp();
VERIFY(m_imageList.Create(128, 128, ILC_COLOR32, 0, 0));
VERIFY(m_imageListSmall.Create(48, 48, ILC_COLOR32, 0, 0));

m_imageList.Add(pApp->LoadIcon(IDR_MAINFRAME));
m_imageList.Add(pApp->LoadIcon(IDR_MAINFRAME));
m_imageList.Add(pApp->LoadIcon(IDR_MAINFRAME));

m_imageListSmall.Add(pApp->LoadIcon(IDR_MAINFRAME));
m_imageListSmall.Add(pApp->LoadIcon(IDR_MAINFRAME));
m_imageListSmall.Add(pApp->LoadIcon(IDR_MAINFRAME));

listCtrl.SetImageList(&m_imageList, LVSIL_NORMAL);
listCtrl.SetImageList(&m_imageListSmall, LVSIL_SMALL);
listCtrl.SetExtendedStyle(LVS_EX_GRIDLINES);

listCtrl.InsertColumn(0, _T("Name"), LVCFMT_LEFT, 100, 0);
listCtrl.InsertColumn(1, _T("Age"), LVCFMT_RIGHT, 100, 1);
listCtrl.InsertColumn(2, _T("Owner"), LVCFMT_LEFT, 150, 2);
listCtrl.InsertColumn(3, _T("City, Country"), LVCFMT_LEFT, 200, 3);

listCtrl.InsertItem(0, _T("Martafoi"), 0);
listCtrl.InsertItem(1, _T("Zdreanta"), 1);
listCtrl.InsertItem(2, _T("Jumbo"), 2);

listCtrl.SetItemText(0, 1, _T("8 months"));
listCtrl.SetItemText(1, 1, _T("7 years"));
listCtrl.SetItemText(2, 1, _T("35 years"));

listCtrl.SetItemText(0, 2, _T("John Doe"));
listCtrl.SetItemText(1, 2, _T("Brigitte Bardot"));
listCtrl.SetItemText(2, 2, _T("Hannibal Barcas"));

listCtrl.SetItemText(0, 3, _T("New York, USA"));
listCtrl.SetItemText(1, 3, _T("Paris, France"));
listCtrl.SetItemText(2, 3, _T("Barcelona, Spain"));

VERIFY(_SetTilesViewLinesCount(3));
VERIFY(_SetTilesViewTileFixedWidth(250));

UINT arrColumns[3] = { 1, 2, 3 };
VERIFY(_SetItemTileLines(0, arrColumns, 3));
VERIFY(_SetItemTileLines(1, arrColumns, 3));
VERIFY(_SetItemTileLines(2, arrColumns, 3));

listCtrl.SetView(LV_VIEW_TILE);
}

Optional Code


// Message mandlers
protected:
DECLARE_MESSAGE_MAP()
    afx_msg void OnViewLargeicon() {
GetListCtrl().SetView(LV_VIEW_ICON);
}
    afx_msg void OnViewSmallicon() {
GetListCtrl().SetView(LV_VIEW_SMALLICON);
}
    afx_msg void OnViewList() {
GetListCtrl().SetView(LV_VIEW_LIST);
}
    afx_msg void OnViewDetails() {
GetListCtrl().SetView(LV_VIEW_DETAILS);
}
    afx_msg void OnViewTile() {
GetListCtrl().SetView(LV_VIEW_TILE);
}
    afx_msg void OnUpdateViewLargeicon(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(LV_VIEW_ICON == GetListCtrl().GetView());
}
    afx_msg void OnUpdateViewSmallicon(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(LV_VIEW_SMALLICON == GetListCtrl().GetView());
}
    afx_msg void OnUpdateViewList(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(LV_VIEW_LIST == GetListCtrl().GetView());
}
    afx_msg void OnUpdateViewDetails(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(LV_VIEW_DETAILS == GetListCtrl().GetView());
}
    afx_msg void OnUpdateViewTile(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(LV_VIEW_TILE == GetListCtrl().GetView());
}

BEGIN_MESSAGE_MAP(CMFCApplication1View, CListView)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_COMMAND(ID_VIEW_LARGEICON, &CDemoListView::OnViewLargeicon)
ON_COMMAND(ID_VIEW_SMALLICON, &CDemoListView::OnViewSmallicon)
ON_COMMAND(ID_VIEW_LIST, &CDemoListView::OnViewList)
ON_COMMAND(ID_VIEW_DETAILS, &CDemoListView::OnViewDetails)
ON_COMMAND(ID_VIEW_TILE, &CDemoListView::OnViewTile)
ON_UPDATE_COMMAND_UI(ID_VIEW_LARGEICON, &CDemoListView::OnUpdateViewLargeicon)
ON_UPDATE_COMMAND_UI(ID_VIEW_SMALLICON, &CDemoListView::OnUpdateViewSmallicon)
ON_UPDATE_COMMAND_UI(ID_VIEW_LIST, &CDemoListView::OnUpdateViewList)
ON_UPDATE_COMMAND_UI(ID_VIEW_DETAILS, &CDemoListView::OnUpdateViewDetails)
ON_UPDATE_COMMAND_UI(ID_VIEW_TILE, &CDemoListView::OnUpdateViewTile)
END_MESSAGE_MAP()


MFCApplication1.rc


ID_VIEW_LARGEICON
ID_VIEW_SMALLICON
ID_VIEW_LIST
ID_VIEW_DETAILS
ID_VIEW_TILE









Result




FAQ


Question: What happens when below function calls return FALSE to indicate failure?

CListCtrl::SetTileViewInfo()
CListCtrl::SetTileInfo()

Answer: Using "Common Control Manifest" is a must for successful function calls above.

沒有留言:

張貼留言

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...