{"id":180,"date":"2007-01-21T00:59:51","date_gmt":"2007-01-20T22:59:51","guid":{"rendered":"https:\/\/alax.info\/blog\/180"},"modified":"2012-07-24T11:59:51","modified_gmt":"2012-07-24T09:59:51","slug":"smart-atlwtl-combo-box-and-list-view-classes-to-cast-and-optionally-manage-typed-control-item-data","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/180","title":{"rendered":"Smart ATL\/WTL combo box and list view classes to cast and optionally manage typed control item data"},"content":{"rendered":"<p>Inspired by the <a href=\"http:\/\/tech.groups.yahoo.com\/group\/wtl\/message\/14196\">post on Yahoo&#8217;s WTL group<\/a> (actually, I have been going to post on this topic for a couple of weeks already).<\/p>\n<p>Standard Windows controls (including common controls) that have multiple items (list box, combo box, list view, tree view etc.) have an option to attach user defined value of type DWORD_PTR (LPARAM) to each item. This is quite sufficient when using controls, however type cast to and from DWORD_PTR is required with most control item data operations.<\/p>\n<p><em>CAiComboBoxT<\/em> and <em>CAiListViewT<\/em> classes templates redefine <em>GetItemData<\/em>\/<em>SetItemData<\/em> methods to perform automatic type casts. An optional template class <em>CAiListControlDataTraitsT<\/em> does even more: it manages per-item definted item data class in contained CAtlList collection and attached a pointer to item data class to each control item.<\/p>\n<p>The classes may be used as follows:<\/p>\n<blockquote><p>CAiComboBoxT &lt; CComboBoxItemData* &gt; m_ComboBox;<br \/>\nCAiListViewT &lt; CListViewItemData, CAiListControlDataTraitsT &gt; m_ListView;<\/p><\/blockquote>\n<p>m_ComboBox is a combo box window class (CComboBox descendant) with CComboBoxItemData pointer defined as control item data.<\/p>\n<p>m_ListView is a list view class (CListViewCtrl descendant) with CListViewItemData defined as control item data. CListViewItemData instances are managed by control class using CAtlList collection (and a position in the collection is actually casted to DWORD_PTR and set as lower level control item data).<\/p>\n<p><img decoding=\"async\" id=\"image179\" src=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2007\/01\/20-image001.png\" alt=\"20-image001.png\" \/><\/p>\n<p><!--more-->To access item class data, you need the following lines of code:<\/p>\n<blockquote><p>CComboBoxItemData* pComboBoxItemData = m_ComboBox.GetItemData(nItem); \/\/ INT nItem<br \/>\nCListViewItemData&amp; ListViewItemData = m_ListView.GetItemData(nItem); \/\/ INT nItem<\/p><\/blockquote>\n<p>Sample project below illustrates basic usage of the classes:<\/p>\n<blockquote><p>BEGIN_MSG_MAP_EX(CMainDialog)<br \/>\nMSG_WM_INITDIALOG(OnInitDialog)<br \/>\n&#8230;<br \/>\nCOMMAND_HANDLER_EX(IDC_COMBOBOX, CBN_SELENDOK, OnComboBoxSelEndOk)<br \/>\nMSG_LVN_GETDISPINFO(IDC_LISTVIEW, OnListGetDispInfo)<br \/>\nMSG_LVN_DBLCLK(IDC_LISTVIEW, OnListViewDblClk)<\/p><\/blockquote>\n<p>OnInitDialog initializes classes including control item data initialization, OnComboBoxSelEndOk obtains currently selected combo box item data class pointer (not managed by the window class), OnListGetDispInfo accesses managed control item data pointer, OnListViewDblClk retrieves first selected control item data class pointer to show message box.<\/p>\n<p>Keywords: DWORD_PTR, <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms673164.aspx\">CB_GETITEMDATA<\/a>, <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms673214.aspx\">CB_SETITEMDATA<\/a>, <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms670761.aspx\">LVM_GETITEM<\/a>, <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms670845.aspx\">LVM_SETITEM<\/a><\/p>\n<p>Visual C++.NET 2005 source code can be downloaded <a id=\"p113\" href=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2007\/01\/listviewitemdata.zip\">here<\/a>, compiled binary &#8211; <a id=\"p114\" href=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2007\/01\/listviewitemdataexe.zip\">here<\/a>.<\/p>\n<p><strong>Update 2012-07-24<\/strong>: <\/p>\n<p>Visual C++ 2010 .h file: <a href=\"https:\/\/www.alax.info\/trac\/public\/browser\/trunk\/Common\/alax.info\/rowtlcrack.h\" rel=\"nofollow\">\/trac\/public\/browser\/&#8230;\/rowtlcrack.h<\/a><\/p>\n<p>Code snippet:<\/p>\n<p>https:\/\/www.alax.info\/trac\/public\/browser\/trunk\/Utilities\/LogProcessExceptions\/MainWizard.h#L104<\/p>\n<pre style='color:#000000;background:#ffffff;'>BEGIN_MSG_MAP_EX<span style='color:#808030; '>(<\/span>CProcessPropertyPage<span style='color:#808030; '>)<\/span>\r\n    CHAIN_MSG_MAP<span style='color:#808030; '>(<\/span>CWizardPropertyPageT<span style='color:#800080; '>&lt;<\/span>CProcessPropertyPage<span style='color:#800080; '>><\/span><span style='color:#808030; '>)<\/span>\r\n    MSG_WM_INITDIALOG<span style='color:#808030; '>(<\/span>OnInitDialog<span style='color:#808030; '>)<\/span>\r\n    MSG_LVN_GETDISPINFO<span style='color:#808030; '>(<\/span>IDC_MAIN_PROCESSES<span style='color:#808030; '>,<\/span> OnProcessListViewGetDispInfo<span style='color:#808030; '>)<\/span>\r\n    MSG_LVN_GETINFOTIP<span style='color:#808030; '>(<\/span>IDC_MAIN_PROCESSES<span style='color:#808030; '>,<\/span> OnProcessListViewGetInfoTip<span style='color:#808030; '>)<\/span>\r\n    MSG_LVN_ITEMCHANGED<span style='color:#808030; '>(<\/span>IDC_MAIN_PROCESSES<span style='color:#808030; '>,<\/span> OnProcessListItemChanged<span style='color:#808030; '>)<\/span>\r\n    MSG_LVN_DBLCLK<span style='color:#808030; '>(<\/span>IDC_MAIN_PROCESSES<span style='color:#808030; '>,<\/span> OnProcessListViewDblClk<span style='color:#808030; '>)<\/span>\r\n    COMMAND_ID_HANDLER_EX<span style='color:#808030; '>(<\/span>IDC_MAIN_REFRESHPROCESSES<span style='color:#808030; '>,<\/span> OnRefreshButtonClicked<span style='color:#808030; '>)<\/span>\r\n    REFLECT_NOTIFICATIONS<span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span>\r\nEND_MSG_MAP<span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span>\r\n<\/pre>\n<p>https:\/\/www.alax.info\/trac\/public\/browser\/trunk\/Utilities\/LogProcessExceptions\/MainWizard.h#L579<\/p>\n<pre style='color:#000000;background:#ffffff;'><span style='color:#603000; '>LRESULT<\/span> OnProcessListViewGetInfoTip<span style='color:#808030; '>(<\/span>NMLVGETINFOTIP<span style='color:#808030; '>*<\/span> pHeader<span style='color:#808030; '>)<\/span>\r\n<span style='color:#800080; '>{<\/span>\r\n    <span style='color:#800000; font-weight:bold; '>const<\/span> CProcessData<span style='color:#808030; '>&amp;<\/span> ProcessData <span style='color:#808030; '>=<\/span> m_ProcessDataList<span style='color:#808030; '>.<\/span>GetAt<span style='color:#808030; '>(<\/span>m_ProcessListView<span style='color:#808030; '>.<\/span>GetItemData<span style='color:#808030; '>(<\/span>pHeader<span style='color:#808030; '>-<\/span><span style='color:#808030; '>><\/span>iItem<span style='color:#808030; '>)<\/span><span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    <span style='color:#603000; '>CString<\/span><span style='color:#808030; '>&amp;<\/span> sTextBuffer <span style='color:#808030; '>=<\/span> m_ProcessListView<span style='color:#808030; '>.<\/span>GetTextBufferString<span style='color:#808030; '>(<\/span>TRUE<span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    sTextBuffer<span style='color:#808030; '>.<\/span>AppendFormat<span style='color:#808030; '>(<\/span>_T<span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>File Name: <\/span><span style='color:#0f69ff; '>%s<\/span><span style='color:#0f69ff; '>\\r<\/span><span style='color:#0f69ff; '>\\n<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>,<\/span> ProcessData<span style='color:#808030; '>.<\/span>GetFileName<span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    sTextBuffer<span style='color:#808030; '>.<\/span>AppendFormat<span style='color:#808030; '>(<\/span>_T<span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>Identifier: <\/span><span style='color:#0f69ff; '>%d<\/span><span style='color:#0000e6; '> (0x<\/span><span style='color:#0f69ff; '>%x<\/span><span style='color:#0000e6; '>)<\/span><span style='color:#0f69ff; '>\\r<\/span><span style='color:#0f69ff; '>\\n<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>,<\/span> ProcessData<span style='color:#808030; '>.<\/span>m_nIdentifier<span style='color:#808030; '>,<\/span> ProcessData<span style='color:#808030; '>.<\/span>m_nIdentifier<span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    <span style='color:#603000; '>BOOL<\/span> bIs64Bit <span style='color:#808030; '>=<\/span> FALSE<span style='color:#800080; '>;<\/span>\r\n<span style='color:#004a43; '>&#xa0;&#xa0;&#xa0;&#xa0;#<\/span><span style='color:#004a43; '>if<\/span><span style='color:#004a43; '> <\/span><span style='color:#808030; '>!<\/span><span style='color:#004a43; '>defined<\/span><span style='color:#808030; '>(<\/span><span style='color:#004a43; '>_WIN64<\/span><span style='color:#808030; '>)<\/span>\r\n    <span style='color:#800000; font-weight:bold; '>if<\/span><span style='color:#808030; '>(<\/span>SafeIsWow64Process<span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>)<\/span>\r\n<span style='color:#004a43; '>&#xa0;&#xa0;&#xa0;&#xa0;#<\/span><span style='color:#004a43; '>endif<\/span><span style='color:#004a43; '> <\/span><span style='color:#696969; '>\/\/ !defined(_WIN64)<\/span>\r\n    bIs64Bit <span style='color:#808030; '>=<\/span> <span style='color:#808030; '>!<\/span>ProcessData<span style='color:#808030; '>.<\/span>m_bIsWow64<span style='color:#800080; '>;<\/span>\r\n    sTextBuffer<span style='color:#808030; '>.<\/span>AppendFormat<span style='color:#808030; '>(<\/span>_T<span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>Type: <\/span><span style='color:#0f69ff; '>%s<\/span><span style='color:#0f69ff; '>\\r<\/span><span style='color:#0f69ff; '>\\n<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>,<\/span> <span style='color:#808030; '>!<\/span>bIs64Bit <span style='color:#800080; '>?<\/span> _T<span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>32-bit<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>)<\/span> <span style='color:#800080; '>:<\/span> _T<span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>64-bit<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    <span style='color:#800000; font-weight:bold; '>if<\/span><span style='color:#808030; '>(<\/span><span style='color:#808030; '>!<\/span>ProcessData<span style='color:#808030; '>.<\/span>m_sAccount<span style='color:#808030; '>.<\/span>IsEmpty<span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>)<\/span>\r\n    sTextBuffer<span style='color:#808030; '>.<\/span>AppendFormat<span style='color:#808030; '>(<\/span>_T<span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>Account: <\/span><span style='color:#0f69ff; '>%s<\/span><span style='color:#0f69ff; '>\\r<\/span><span style='color:#0f69ff; '>\\n<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>,<\/span> ProcessData<span style='color:#808030; '>.<\/span>m_sAccount<span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    sTextBuffer<span style='color:#808030; '>.<\/span>AppendFormat<span style='color:#808030; '>(<\/span>_T<span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>Window Caption: <\/span><span style='color:#0f69ff; '>%s<\/span><span style='color:#0f69ff; '>\\r<\/span><span style='color:#0f69ff; '>\\n<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>,<\/span> ProcessData<span style='color:#808030; '>.<\/span>m_sWindowCaption<span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    <span style='color:#696969; '>\/\/sTextBuffer.AppendFormat(_T(\"Image File Path: %s\\r\\n\"), ProcessData.GetImageFilePath());<\/span>\r\n    sTextBuffer<span style='color:#808030; '>.<\/span>AppendFormat<span style='color:#808030; '>(<\/span>_T<span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>Working Set Size: <\/span><span style='color:#0f69ff; '>%s<\/span><span style='color:#0000e6; '> MB<\/span><span style='color:#0f69ff; '>\\r<\/span><span style='color:#0f69ff; '>\\n<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>,<\/span> _StringHelper<span style='color:#800080; '>::<\/span>FormatNumber<span style='color:#808030; '>(<\/span><span style='color:#808030; '>(<\/span><span style='color:#603000; '>LONG<\/span><span style='color:#808030; '>)<\/span> ProcessData<span style='color:#808030; '>.<\/span>GetMemoryCounters<span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>.<\/span>WorkingSetSize <span style='color:#808030; '>><\/span><span style='color:#808030; '>><\/span> <span style='color:#008c00; '>20<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    sTextBuffer<span style='color:#808030; '>.<\/span>AppendFormat<span style='color:#808030; '>(<\/span>_T<span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>Private Memory: <\/span><span style='color:#0f69ff; '>%s<\/span><span style='color:#0000e6; '> MB<\/span><span style='color:#0f69ff; '>\\r<\/span><span style='color:#0f69ff; '>\\n<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>,<\/span> _StringHelper<span style='color:#800080; '>::<\/span>FormatNumber<span style='color:#808030; '>(<\/span><span style='color:#808030; '>(<\/span><span style='color:#603000; '>LONG<\/span><span style='color:#808030; '>)<\/span> ProcessData<span style='color:#808030; '>.<\/span>GetMemoryCounters<span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>.<\/span>PrivateUsage <span style='color:#808030; '>><\/span><span style='color:#808030; '>><\/span> <span style='color:#008c00; '>20<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    sTextBuffer<span style='color:#808030; '>.<\/span>TrimRight<span style='color:#808030; '>(<\/span>_T<span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0f69ff; '>\\t<\/span><span style='color:#0f69ff; '>\\n<\/span><span style='color:#0f69ff; '>\\r<\/span><span style='color:#0000e6; '> .<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    _tcsncpy_s<span style='color:#808030; '>(<\/span>pHeader<span style='color:#808030; '>-<\/span><span style='color:#808030; '>><\/span>pszText<span style='color:#808030; '>,<\/span> pHeader<span style='color:#808030; '>-<\/span><span style='color:#808030; '>><\/span>cchTextMax<span style='color:#808030; '>,<\/span> m_ProcessListView<span style='color:#808030; '>.<\/span>GetTextBuffer<span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>,<\/span> _TRUNCATE<span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\r\n    <span style='color:#800000; font-weight:bold; '>return<\/span> <span style='color:#008c00; '>0<\/span><span style='color:#800080; '>;<\/span>\r\n<span style='color:#800080; '>}<\/span>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Inspired by the post on Yahoo&#8217;s WTL group (actually, I have been going to post on this topic for a couple of weeks already). Standard Windows controls (including common controls) that have multiple items (list box, combo box, list view, tree view etc.) have an option to attach user defined value of type DWORD_PTR (LPARAM)&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/180\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,13,20],"tags":[],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-atl","category-source","category-wtl"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=180"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}