ListCtrlBaseEx.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:12k
源码类别:
P2P编程
开发平台:
Visual C++
- // ListCtrlBaseEx.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "ListCtrlBaseEx.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define ID_MENUITEM_SHOW_COLUMN 52826
- const CString CListCtrlBaseEx::m_strColumnWidthKey = "width";
- const CString CListCtrlBaseEx::m_strColumnVisibleKey = "visible";
- /////////////////////////////////////////////////////////////////////////////
- // CListCtrlBaseEx
- CListCtrlBaseEx::CListCtrlBaseEx(CString strSection, bool bSortEnabled)
- {
- m_bSortEnabled = bSortEnabled;
- m_nSortColumn = 1;
- m_strSection = strSection;
- }
- CListCtrlBaseEx::~CListCtrlBaseEx()
- {
- }
- BEGIN_MESSAGE_MAP(CListCtrlBaseEx, CListCtrl)
- //{{AFX_MSG_MAP(CListCtrlBaseEx)
- ON_WM_CREATE()
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- ON_NOTIFY_REFLECT_EX(LVN_COLUMNCLICK, OnItemclickSort)
- ON_MESSAGE(LVM_SETIMAGELIST, OnSetImageList)
- ON_COMMAND_RANGE(ID_MENUITEM_SHOW_COLUMN, ID_MENUITEM_SHOW_COLUMN + 100, OnMenuitemShowColumn)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CListCtrlBaseEx message handlers
- void CListCtrlBaseEx::SetSortColumn(int nColumn)
- {
- m_nSortColumn = nColumn;
- SetSortIcon();
- }
- void CListCtrlBaseEx::AddColumn(CString strName, bool bNumeric, long lWidth, CString strToolTip)
- {
- m_vColumnInfo.push_back(CColumnInfo(strName, bNumeric, lWidth, true, strToolTip));
- }
- int CListCtrlBaseEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CListCtrl::OnCreate(lpCreateStruct) == -1)
- return -1;
- //
- // Create BitmapList
- //
- HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDB_BITMAP_HEADER_ARROW),
- IMAGE_BITMAP,
- 0,0, // cx,cy
- LR_CREATEDIBSECTION); // | LR_LOADMAP3DCOLORS );
- CBitmap bm;
- bm.Attach(hbm);
- BOOL bRet = m_ctlImage.Create(16, 16, ILC_COLOR8|ILC_MASK, 2, 4);
- ASSERT(bRet);
- m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
- m_ctlImage.SetBkColor(CLR_NONE); //::GetSysColor(COLOR_BTNFACE));
- LoadState();
- CHeaderCtrl* pHeader = GetHeaderCtrl();
- if (pHeader)
- {
- m_Tooltip.Create(pHeader);
- m_Tooltip.SetMaxTipWidth(1000);
- pHeader->EnableToolTips();
- // EnableToolTips();
- }
- int iInx = 0;
- for (int i=0; i<m_vColumnInfo.size(); i++)
- {
- if (m_vColumnInfo[i].m_bVisible)
- {
- int res = InsertColumn(i, m_vColumnInfo[i].m_strName,
- m_vColumnInfo[i].m_bNumeric ? LVCFMT_RIGHT : LVCFMT_LEFT, m_vColumnInfo[i].m_lWidth);
- if(res!=-1 && (GetStyle()&LVS_TYPEMASK)==LVS_REPORT)
- {
- if (pHeader)
- {
- CRect rcColumn;
- BOOL bRet = pHeader->GetItemRect(iInx, &rcColumn);
- ASSERT(bRet);
- // m_Tooltip.AddTool(pHeader, m_vColumnInfo[i].m_strToolTip, (LPCRECT)rcColumn, i+1);
- if (!m_Tooltip.AddTool(pHeader, m_vColumnInfo[i].m_strToolTip, (LPCRECT)rcColumn, i+1))
- {
- ASSERT(FALSE);
- }
- }
- }
- iInx ++;
- }
- }
- return 0;
- }
- void CListCtrlBaseEx::OnDestroy()
- {
- SaveState();
- CListCtrl::OnDestroy();
- }
- void CListCtrlBaseEx::LoadState()
- {
- CString strKey;
- for (int i=0; i<m_vColumnInfo.size(); i++)
- {
- long lWidth = m_vColumnInfo[i].m_lWidth;
- if (lWidth <= 0) lWidth = 100;
- strKey.Format("%s%d", m_strColumnWidthKey, i);
- m_vColumnInfo[i].m_lWidth = AfxGetApp()->GetProfileInt(m_strSection, strKey, lWidth);
- strKey.Format("%s%d", m_strColumnVisibleKey, i);
- m_vColumnInfo[i].m_bVisible = AfxGetApp()->GetProfileInt(m_strSection, strKey, true) != 0;
- }
- }
- void CListCtrlBaseEx::SaveState()
- {
- int nColumnCount = GetHeaderCtrl()->GetItemCount();
- int iColIdx = 0;
- CString strKey;
- for (int i=0; i<m_vColumnInfo.size(); i++)
- {
- if (m_vColumnInfo[i].m_bVisible)
- {
- strKey.Format("%s%d", m_strColumnWidthKey, i);
- AfxGetApp()->WriteProfileInt(m_strSection, strKey, GetColumnWidth(iColIdx));
- iColIdx ++;
- }
- }
- for (i=0; i<m_vColumnInfo.size(); i++)
- {
- strKey.Format("%s%d", m_strColumnVisibleKey, i);
- AfxGetApp()->WriteProfileInt(m_strSection, strKey, m_vColumnInfo[i].m_bVisible);
- }
- }
- int CListCtrlBaseEx::GetPhysicalIndex(int nColumnIndex) const
- {
- assert(nColumnIndex >= 0 && nColumnIndex < m_vColumnInfo.size());
- int nIndex = 0;
- for (int i = 0; i < nColumnIndex; i++)
- {
- if (m_vColumnInfo[i].m_bVisible)
- nIndex++;
- }
- return nIndex;
- }
- int CListCtrlBaseEx::GetLogicalIndex(int nPhysicalIndex) const
- {
- for (int i = 0; i < m_vColumnInfo.size(); i++)
- {
- if (m_vColumnInfo[i].m_bVisible)
- {
- if (i == nPhysicalIndex)
- return nPhysicalIndex;
- }
- else
- nPhysicalIndex++;
- }
- return -1;
- }
- void CListCtrlBaseEx::ShowColumn(int nColumn, bool bShowIt)
- {
- assert(nColumn >= 0 && nColumn < m_vColumnInfo.size());
- if (bShowIt)
- {
- if (!m_vColumnInfo[nColumn].m_bVisible)
- {
- m_vColumnInfo[nColumn].m_bVisible = true;
- int nPhysicalColumn = GetPhysicalIndex(nColumn);
- BOOL bRet = InsertColumn(nPhysicalColumn, m_vColumnInfo[nColumn].m_strName,
- m_vColumnInfo[nColumn].m_bNumeric ? LVCFMT_RIGHT : LVCFMT_LEFT, 100); // m_lArrColumnWidth[nColumn]);
- ASSERT(bRet);
- for (int i = GetItemCount(); --i >= 0;)
- {
- // ShowFileDBItemText(i);
- // ShowFileDBItemActive(i);
- OnShowColumn(i);
- }
- SetSortIcon();
- //
- // tooltip
- //
- CHeaderCtrl* pHeaderCtrl = GetHeaderCtrl();
- if (pHeaderCtrl)
- {
- CRect rcColumn;
- BOOL bRet = pHeaderCtrl->GetItemRect(nPhysicalColumn, &rcColumn);
- ASSERT(bRet);
- if (!m_Tooltip.AddTool(pHeaderCtrl, m_vColumnInfo[nColumn].m_strToolTip, (LPCRECT)rcColumn, nColumn+1))
- {
- ASSERT(FALSE);
- }
- RecalcToolRect();
- }
- }
- }
- else
- {
- if (m_vColumnInfo[nColumn].m_bVisible)
- {
- m_vColumnInfo[nColumn].m_bVisible = false;
- int nPhysicalColumn = GetPhysicalIndex(nColumn);
- BOOL bRet = DeleteColumn(nPhysicalColumn);
- ASSERT(bRet);
- //
- // tooltip
- //
- CHeaderCtrl* pHeaderCtrl = GetHeaderCtrl();
- if (pHeaderCtrl)
- {
- m_Tooltip.DelTool(pHeaderCtrl, nColumn + 1);
- RecalcToolRect();
- }
- else
- {
- ASSERT(FALSE);
- }
- }
- }
- }
- void CListCtrlBaseEx::OnShowColumn(int iItem)
- {
- assert(false);
- }
- bool CListCtrlBaseEx::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- if (pWnd == GetHeaderCtrl())
- {
- CMenu menu;
- if (!menu.CreatePopupMenu())
- return false;
- for (int i=1; i<m_vColumnInfo.size(); i++)
- {
- menu.AppendMenu(MF_STRING |
- (m_vColumnInfo[i].m_bVisible ? MF_CHECKED : MF_UNCHECKED),
- ID_MENUITEM_SHOW_COLUMN + i, m_vColumnInfo[i].m_strName);
- }
- menu.TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, point.x,
- point.y, this);
- return true;
- }
- CRect rc;
- GetClientRect(&rc);
- ClientToScreen(rc);
- if (!rc.PtInRect(point))
- {
- CListCtrl::OnContextMenu(pWnd, point);
- return true;
- }
- return false;
- }
- void CListCtrlBaseEx::OnMenuitemShowColumn(UINT uID)
- {
- int iColumn = uID - ID_MENUITEM_SHOW_COLUMN;
- ShowColumn(iColumn, !m_vColumnInfo[iColumn].m_bVisible);
- }
- void CListCtrlBaseEx::SetItemTextEx(int iItem, int iSubItem, CString strText)
- {
- assert(iSubItem>= 0 && iSubItem < m_vColumnInfo.size());
- if (m_vColumnInfo[iSubItem].m_bVisible)
- {
- SetItemText(iItem, GetPhysicalIndex(iSubItem), strText);
- }
- }
- void CListCtrlBaseEx::SelectAllItems()
- {
- for (int i=0; i<GetItemCount(); i++)
- SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
- SetFocus();
- }
- void CListCtrlBaseEx::ClearSelectItems()
- {
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int index = GetNextSelectedItem(pos);
- SetItemState(index, LVIS_FOCUSED | LVIS_SELECTED, 0);
- }
- }
- LRESULT CListCtrlBaseEx::OnSetImageList(WPARAM wParam, LPARAM lParam)
- {
- LRESULT dwResult = Default();
- if (wParam == LVSIL_SMALL && lParam)
- {
- // restore image list with sort icons because default behavior is that the
- // header control shares its image list with the small icon list of the
- // list control
- GetHeaderCtrl()->SetImageList(&m_ctlImage);
- }
- return dwResult;
- }
- void CListCtrlBaseEx::SetSortIcon()
- {
- if (!m_bSortEnabled) return;
- CHeaderCtrl* pHeaderCtrl = GetHeaderCtrl();
- ASSERT(pHeaderCtrl);
- for (int col = 0; col <m_vColumnInfo.size(); col++)
- {
- if (m_vColumnInfo[col].m_bVisible)
- {
- HDITEM hdrItem;
- int nPhysicalCol = GetPhysicalIndex(col);
- hdrItem.mask = HDI_FORMAT | HDI_IMAGE | HDI_HEIGHT;
- hdrItem.cxy = 25;
- pHeaderCtrl->GetItem(nPhysicalCol, &hdrItem);
- if ( m_nSortColumn != 0 && m_nSortColumn - 1 == col)
- {
- hdrItem.iImage = eArrowDown;
- hdrItem.fmt = hdrItem.fmt & HDF_JUSTIFYMASK |
- HDF_IMAGE | HDF_STRING | HDF_BITMAP_ON_RIGHT;
- }
- else if (m_nSortColumn != 0 && -m_nSortColumn - 1 == col)
- {
- hdrItem.iImage = eArrowUp;
- hdrItem.fmt = hdrItem.fmt & HDF_JUSTIFYMASK |
- HDF_IMAGE | HDF_STRING | HDF_BITMAP_ON_RIGHT;
- }
- else
- hdrItem.fmt = hdrItem.fmt & HDF_JUSTIFYMASK | HDF_STRING;
- pHeaderCtrl->SetItem(nPhysicalCol, &hdrItem);
- }
- }
- }
- void CListCtrlBaseEx::OnItemclickSort(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- if (!m_bSortEnabled) return;
- NMLISTVIEW* pNMListView = reinterpret_cast<NMLISTVIEW*>(pNMHDR);
- int iSubItem = GetLogicalIndex(pNMListView->iSubItem);
- if (iSubItem == -1)
- {
- assert(false);
- return;
- }
- int nAbsSortColumn = abs(m_nSortColumn);
- if (nAbsSortColumn > 0)
- {
- if (iSubItem == nAbsSortColumn-1)
- m_nSortColumn = -m_nSortColumn;
- else
- m_nSortColumn = iSubItem + 1;
- }
- else
- m_nSortColumn = iSubItem + 1;
- SetSortIcon();
- OnColumnSort(m_nSortColumn);
- }
- void CListCtrlBaseEx::OnColumnSort(int iColumn)
- {
- if (!m_bSortEnabled) return;
- assert(false);
- }
- /************************************************************************
- Tool tip
- ************************************************************************/
- /*
- void CListCtrlBaseEx::PreSubclassWindow()
- {
- CListCtrl::PreSubclassWindow();
- EnableToolTips(); // Enables list contrl ToolTip control
- int iRet = m_Header.SubclassDlgItem(0, this); // Replaces new header control with default list contrl header control.
- if (!iRet)
- {
- DWORD dwErr = GetLastError();
- ASSERT(FALSE);
- }
- }
- //*/
- //*
- // Detects CListCtrlBaseEx resizing.
- BOOL CListCtrlBaseEx::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
- {
- // TODO: Add your specialized code here and/or call the base class
- LPNMHEADER pNMHD = (LPNMHEADER)lParam;
- switch(pNMHD->hdr.code)
- {
- case HDN_ITEMCHANGEDA:
- case HDN_ITEMCHANGEDW:
- {
- BOOL bRes;
- bRes = CListCtrl::OnNotify(wParam, lParam, pResult);
- RecalcToolRect();
- return bRes;
- }
- }
- return CListCtrl::OnNotify(wParam, lParam, pResult);
- }
- //*/
- BOOL CListCtrlBaseEx::PreTranslateMessage(MSG* pMsg)
- {
- m_Tooltip.RelayEvent(pMsg);
- return CListCtrl::PreTranslateMessage(pMsg);
- }
- void CListCtrlBaseEx::RecalcToolRect()
- {
- // This prevent debug assertion fail on last WM_MOVE on app close.
- if(!::IsWindow(m_Tooltip.m_hWnd))
- return;
- CHeaderCtrl* pHeaderCtrl = GetHeaderCtrl();
- if (!pHeaderCtrl)
- {
- ASSERT(FALSE);
- return;
- }
- if((GetStyle()&LVS_TYPEMASK)!=LVS_REPORT)
- {
- ASSERT(FALSE);
- return;
- }
- int iInx = 0;
- for (int i=0; i<m_vColumnInfo.size(); i++)
- {
- if (m_vColumnInfo[i].m_bVisible)
- {
- if (pHeaderCtrl)
- {
- CRect rcColumn;
- BOOL bRet = pHeaderCtrl->GetItemRect(iInx, &rcColumn);
- ASSERT(bRet);
- // m_Tooltip.AddTool(pHeader, m_vColumnInfo[i].m_strToolTip, (LPCRECT)rcColumn, i+1);
- m_Tooltip.SetToolRect(pHeaderCtrl, i+1, (LPCRECT)rcColumn);
- }
- iInx ++;
- }
- }
- /*
- HDITEM hdCol;
- hdCol.mask = HDI_WIDTH | HDI_FORMAT;
- RECT rcColumn;
- GetClientRect(&rcColumn);
- // Get scroll position
- int nHScrollPos = GetScrollPos(SB_HORZ);
- rcColumn.left -= nHScrollPos;
- rcColumn.right -= nHScrollPos;
- CRect rectTool;
- int numcol = GetItemCount();
- for(int col=0; col<numcol; col++)
- {
- // Get column string extent
- pHeaderCtrl->GetItem(col, &hdCol);
- rcColumn.right = rcColumn.left + LOWORD(hdCol.cxy);
- // Calculate ToolTip hover rectangle
- rectTool = rcColumn;
- rectTool.OffsetRect(nHScrollPos, 0);
- m_Tooltip.SetToolRect(this, col+1, &rectTool);
- rcColumn.left = rcColumn.right;
- }
- //*/
- }