📄 uictrl.cpp
字号:
}
else
{
LPTSTR pStr = ((CUIStrListCtrlData *)pData)->GetString(nSubItem);
if (pStr)
{
_tcsncpy(pitem->pszText,pStr,nLen);
nLen = min(lstrlen(pStr)+1,nLen);
pitem->pszText[nLen-1] = '\0';
return nLen;
}
}
return 0L;
}
int CUIODListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListCtrl::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
RegisterDropTarget();
return 0;
}
int CALLBACK CompareFunc(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort);
PFNLVCOMPARE CUIODListCtrl::GetCompareFunc()
{
return CompareFunc;
}
BOOL CUIODListCtrl::ColumnClick(NM_LISTVIEW* pNMListView)
{
if (pNMListView->iSubItem != m_nSortColumn)
m_bSortAscending = TRUE;
else
m_bSortAscending = !m_bSortAscending;
m_nSortColumn = pNMListView->iSubItem;
m_HeaderCtrl.SetSortImage(m_nSortColumn,m_bSortAscending);
ASSERT(m_pColTypes);
CUIODListCtrlSortInfo sortinfo(pNMListView->iSubItem,m_pColTypes[pNMListView->iSubItem],m_bSortAscending);
SortItems(GetCompareFunc(),(DWORD)&sortinfo);
int item = GetCurSel();
EnsureVisible(item,0);
return TRUE;
}
///////////////////////////////////////////////
// static callback for sorting when the header is clicked in report mode
int CALLBACK CompareFunc(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort)
{
CUIStrListCtrlData *pData1 = (CUIStrListCtrlData*)lParam1;
CUIStrListCtrlData *pData2 = (CUIStrListCtrlData*)lParam2;
ASSERT(pData1);
ASSERT(pData2);
ASSERT_KINDOF(CUIStrListCtrlData,pData1);
ASSERT_KINDOF(CUIStrListCtrlData,pData2);
CUIODListCtrlSortInfo *pSortInfo = (CUIODListCtrlSortInfo *)lParamSort;
int ret=0;
switch(pSortInfo->GetColType())
{
case CUIODListCtrl::e_DateTime:
{
COleDateTime time1;
CString str1(pData1->GetString(pSortInfo->GetColumn()));
if (!str1.IsEmpty())
time1.ParseDateTime(str1);
COleDateTime time2;
CString str2(pData2->GetString(pSortInfo->GetColumn()));
if (!str2.IsEmpty())
time2.ParseDateTime(str2);
if (time1 > time2)
ret = 1;
else if (time1 < time2)
ret = -1;
break;
}
case CUIODListCtrl::e_NumericFormat:
{
int num1 = _ttoi(CUIODListCtrl::StripNonNumeric((LPCTSTR)pData1->GetString(pSortInfo->GetColumn()),CUIODListCtrl::e_Numeric));
int num2 = _ttoi(CUIODListCtrl::StripNonNumeric((LPCTSTR)pData2->GetString(pSortInfo->GetColumn()),CUIODListCtrl::e_Numeric));
ret = num1-num2;
break;
}
case CUIODListCtrl::e_Numeric:
{
int num1 = _ttoi((LPCTSTR)pData1->GetString(pSortInfo->GetColumn()));
int num2 = _ttoi((LPCTSTR)pData2->GetString(pSortInfo->GetColumn()));
ret = num1-num2;
break;
}
case CUIODListCtrl::e_DoubleFormat:
{
double num1 = _tcstod(CUIODListCtrl::StripNonNumeric((LPCTSTR)pData1->GetString(pSortInfo->GetColumn()),CUIODListCtrl::e_Double),NULL);
double num2 = _tcstod(CUIODListCtrl::StripNonNumeric((LPCTSTR)pData2->GetString(pSortInfo->GetColumn()),CUIODListCtrl::e_Double),NULL);
if (num1 > num2)
ret = 1;
else if (num1 < num2)
ret = -1;
break;
}
case CUIODListCtrl::e_Double:
{
double num1 = _tcstod((LPCTSTR)pData1->GetString(pSortInfo->GetColumn()),NULL);
double num2 = _tcstod((LPCTSTR)pData2->GetString(pSortInfo->GetColumn()),NULL);
if (num1 > num2)
ret = 1;
else if (num1 < num2)
ret = -1;
break;
}
case CUIODListCtrl::e_Text:
default:
ret = CompareString(LOCALE_USER_DEFAULT,NORM_IGNORECASE | NORM_IGNOREKANATYPE,
(LPCTSTR)pData1->GetString(pSortInfo->GetColumn()),
pData1->GetStringLen(pSortInfo->GetColumn()),
(LPCTSTR)pData2->GetString(pSortInfo->GetColumn()),
pData2->GetStringLen(pSortInfo->GetColumn())) - 2;
break;
}
if (!pSortInfo->Ascending())
ret = -ret;
return ret;
}
void CUIODListCtrl::ReplaceString(CString &rStr,LPCTSTR pszOldText,LPCTSTR pszNewText)
{
int nPos = rStr.Find(pszOldText);
CString strLeft;
if (nPos != -1)
{
strLeft = rStr.Left(nPos);
CString strRight(rStr.Right(rStr.GetLength()-(nPos+lstrlen(pszOldText))));
strLeft += pszNewText;
strLeft += strRight;
}
rStr = strLeft;
}
void CUIODListCtrl::Sort()
{
ASSERT(m_pColTypes);
CUIODListCtrlSortInfo sortinfo(m_nSortColumn,m_pColTypes[m_nSortColumn],m_bSortAscending);
SortItems(GetCompareFunc(),(DWORD)&sortinfo);
m_HeaderCtrl.SetSortImage(m_nSortColumn,m_bSortAscending);
TRACE1("Sorting column %d\n",m_nSortColumn);
}
void CUIODListCtrl::SetColFont(int nRow,int nCol,CFont *pFont)
{
CUIListCtrlData* pListObj = GetListCtrlData(nRow);
if (pListObj)
{
ASSERT(pFont);
if (pFont)
pListObj->SetFont(pFont,nCol);
}
}
void CUIODListCtrl::SetRowFont(int nRow,CFont *pFont)
{
CUIListCtrlData* pListObj = GetListCtrlData(nRow);
if (pListObj)
{
ASSERT(pFont);
if (pFont)
pListObj->SetFont(pFont);
}
}
void CUIODListCtrl::SetColBold(int nRow,int nCol,BOOL bBold)
{
CUIListCtrlData* pListObj = GetListCtrlData(nRow);
if (pListObj)
{
if (!pListObj->IsFontSet(nCol))
{
pListObj->SetFont(GetFont(),nCol);
}
const CFont *pFont = pListObj->GetFont(nCol);
LOGFONT lf;
((CFont*)pFont)->GetLogFont(&lf);
lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
CFont font;
font.CreateFontIndirect(&lf);
pListObj->SetFont(&font,nCol);
CRect rect;
RedrawItems(nRow,nRow);
UpdateWindow();
}
}
void CUIODListCtrl::SetRowBold(int nRow,BOOL bBold)
{
CUIListCtrlData* pListObj = GetListCtrlData(nRow);
if (pListObj)
{
if (!pListObj->IsFontSet(-1))
{
pListObj->SetFont(GetFont());
}
const CFont *pFont = pListObj->GetFont();
LOGFONT lf;
((CFont*)pFont)->GetLogFont(&lf);
lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
CFont font;
font.CreateFontIndirect(&lf);
pListObj->SetFont(&font);
CRect rect;
RedrawItems(nRow,nRow);
UpdateWindow();
}
}
BOOL CUIODListCtrl::SubItemPostPaint(LPNMLVCUSTOMDRAW lplvcd,LRESULT *pResult)
{
if (m_hOrigFont)
{
CDC *pDC = CDC::FromHandle(lplvcd->nmcd.hdc);
pDC->SelectObject(CFont::FromHandle(m_hOrigFont));
m_hOrigFont = NULL;
}
return TRUE;
}
BOOL CUIODListCtrl::SubItemPrePaint(LPNMLVCUSTOMDRAW lplvcd,LRESULT *pResult)
{
*pResult = CDRF_DODEFAULT;
int nRow = lplvcd->nmcd.dwItemSpec;
int nCol = lplvcd->iSubItem;
CUIListCtrlData* pListObj = GetListCtrlData(nRow);
if (pListObj == NULL)
return FALSE;
*pResult = CDRF_NOTIFYPOSTPAINT;
lplvcd->clrText = pListObj->GetTextColor(nCol);
lplvcd->clrTextBk = pListObj->GetBkColor(nCol);
if (pListObj->IsFontSet(nCol))
{
CDC *pDC = CDC::FromHandle(lplvcd->nmcd.hdc);
CFont *pOldFont = pDC->SelectObject((CFont*)pListObj->GetFont(nCol));
m_hOrigFont = (HFONT)pOldFont;
*pResult |= CDRF_NEWFONT;
}
CTextProgressCtrl *pWnd = (CTextProgressCtrl*)pListObj->GetCtrl(nCol);
if (pWnd == NULL)
return TRUE;
CRect rcItem;
GetItemRect(nRow,rcItem,LVIR_LABEL);
rcItem.left = lplvcd->nmcd.rc.left;
rcItem.right = lplvcd->nmcd.rc.right;
CDC *pDC = CDC::FromHandle(lplvcd->nmcd.hdc);
pWnd->DoPaint(pDC,rcItem,GetItemState(nRow,LVIS_SELECTED) == LVIS_SELECTED);
*pResult = CDRF_SKIPDEFAULT;
return TRUE;
}
BOOL CUIODListCtrl::ItemPrePaint(LPNMLVCUSTOMDRAW lplvcd,LRESULT *pResult)
{
*pResult = CDRF_NOTIFYSUBITEMDRAW;
return TRUE;
}
BOOL CUIODListCtrl::ItemPostPaint(LPNMLVCUSTOMDRAW lplvcd,LRESULT *pResult)
{
*pResult = CDRF_DODEFAULT;
return TRUE;
}
///////////////////////////////////////////////
// Persistence
///////////////////////////////////////////////
void CUIODListCtrl::SetSection(LPCTSTR pszSection)
{
if (pszSection == NULL)
m_strSection.Empty();
else
{
SaveProfile();
m_strSection = _T("UIODListControl\\");
m_strSection += pszSection;
}
}
LPCTSTR CUIODListCtrl::GetSection() const
{
return m_strSection;
}
void CUIODListCtrl::LoadProfile()
{
if (m_strSection.IsEmpty())
return;
CWinApp *pApp = AfxGetApp();
if (pApp == NULL)
return;
LPCTSTR pszSection = GetSection();
m_strHeadings = pApp->GetProfileString(pszSection,szEntryHeadings);
m_dwExStyle = pApp->GetProfileInt(pszSection,szEntryStyle,m_dwExStyle);
m_bFullRowSel = pApp->GetProfileInt(pszSection,szEntryRowSel,m_bFullRowSel);
m_dwViewType = pApp->GetProfileInt(pszSection,szEntryViewType,m_dwViewType);
m_bColumnSizing = pApp->GetProfileInt(pszSection,szEntryColumnSizing,m_bColumnSizing);
m_nSortColumn = pApp->GetProfileInt(pszSection,szEntrySortColumn,m_nSortColumn);
int nSubItems = pApp->GetProfileInt(pszSection,szEntrySubItems,0);
if (nSubItems)
{
delete []m_pColOrder;
delete []m_pColWidths;
m_pColOrder = new int[nSubItems];
m_pColWidths = new int[nSubItems];
CString strEntry;
for(int i=0;i < nSubItems;i++)
{
strEntry.Format(_T("%s%d"),szEntryColOrder,i+1);
m_pColOrder[i] = pApp->GetProfileInt(pszSection,strEntry,0);
strEntry.Format(_T("%s%d"),szEntryColWidths,i+1);
m_pColWidths[i] = pApp->GetProfileInt(pszSection,strEntry,0);
}
}
}
void CUIODListCtrl::SaveProfile()
{
if (m_strSection.IsEmpty())
return;
CWinApp *pApp = AfxGetApp();
if (pApp == NULL)
return;
LPCTSTR pszSection = GetSection();
pApp->WriteProfileString(pszSection,szEntryHeadings,m_strHeadings);
pApp->WriteProfileInt(pszSection,szEntryStyle,SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE));
pApp->WriteProfileInt(pszSection,szEntryRowSel,m_bFullRowSel);
pApp->WriteProfileInt(pszSection,szEntryViewType,GetViewType());
pApp->WriteProfileInt(pszSection,szEntryColumnSizing,m_bColumnSizing);
pApp->WriteProfileInt(pszSection,szEntrySubItems,m_nSubItems);
pApp->WriteProfileInt(pszSection,szEntrySortColumn,m_nSortColumn);
if (m_nSubItems)
{
delete []m_pColOrder;
m_pColOrder = new int[m_nSubItems],0;
SendMessage(LVM_GETCOLUMNORDERARRAY,(WPARAM)m_nSubItems,(LPARAM)m_pColOrder);
CString strEntry;
for(int i=0;i < m_nSubItems;i++)
{
strEntry.Format(_T("%s%d"),szEntryColOrder,i+1);
pApp->WriteProfileInt(pszSection,strEntry,m_pColOrder[i]);
if (m_pColWidths)
{
strEntry.Format(_T("%s%d"),szEntryColWidths,i+1);
pApp->WriteProfileInt(pszSection,strEntry,m_pColWidths[i]);
}
}
}
}
void CUIODListCtrl::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << m_strHeadings;
ar << m_nSubItems;
m_pColOrder = new int[m_nSubItems];
SendMessage(LVM_GETCOLUMNORDERARRAY,(WPARAM)m_nSubItems,(LPARAM)m_pColOrder);
for(int i=0;i < m_nSubItems;i++)
{
ar << (int)m_pColOrder[i];
ar << (int)GetColumnWidth(i);
}
delete []m_pColOrder;
m_pColOrder = NULL;
ar << SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
ar << m_dwViewType;
ar << m_bFullRowSel;
ar << m_bColumnSizing;
}
else
{
ar >> m_strHeadings;
ar >> m_nSubItems;
delete []m_pColOrder;
delete []m_pColWidths;
m_pColOrder = new int[m_nSubItems];
m_pColWidths = new int[m_nSubItems];
for(int i=0;i < m_nSubItems;i++)
{
ar >> (int)m_pColOrder[i];
ar >> (int)m_pColWidths[i];
}
ar >> m_dwExStyle;
ar >> m_bFullRowSel;
ar >> m_dwViewType;
ar >> m_bColumnSizing;
}
}
/////////////////////////////////////////////////////////////////
// OLE drag and drop
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CUIODListCtrl message handlers
#include "UIDragImage.h"
BOOL CUIODListCtrl::OnBeginRDrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
*pResult = 0;
DoOleDrag(pNMListView,true);
return TRUE;
}
BOOL CUIODListCtrl::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
*pResult = 0;
DoOleDrag(pNMListView,false);
return TRUE;
}
void CUIODListCtrl::DoOleDrag(NM_LISTVIEW* pNMListView,bool bRightMenu)
{
int nSelected = GetSelectedCount();
if (nSelected == 0 || pNMListView->iItem == -1)
return;
BOOL bRet=TRUE;
if (bRet == FALSE)
return;
// View type LVS_ICON etc
int nType = GetViewType();
// 2 ints(nSelected,nType) + ImageData + CRect
HANDLE hData = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,sizeof(int)*2+sizeof(DD_ImageData)*nSelected+sizeof(CRect));
int *pData = (int*)::GlobalLock(hData);
*pData++ = nType;
*pData++ = nSelected;
// add the selections
DD_ImageData *pImageData = (DD_ImageData*)pData;
CRect rcItem;
CRect rcIcon;
CRect rcTotalItem;
int iItem = pNMListView->iItem;
GetItemRect(iItem,rcItem,LVIR_LABEL);
GetItemRect(iItem,rcIcon,LVIR_ICON);
rcTotalItem.left = rcIcon.left;
rcTotalItem.top = rcIcon.top;
rcTotalItem.right = rcIcon.right+rcItem.right;
rcTotalItem.bottom = 0;
CPoint point(pNMListView->ptAction);
CPoint ptHitTest;
UINT flag=0;
iItem = GetNextSel(-1);
int *pRows = new int[nSelected+1];
pRows[0] = iItem;
for(int i=1;iItem != -1;i++)
{
GetItemRect(iItem,rcItem,LVIR_LABEL);
GetItemRect(iItem,rcIcon,LVIR_ICON);
ptHitTest.x = rcItem.left;
ptHitTest.y = rcItem.top;
HitTest(ptHitTest,&flag);
if ((flag & LVHT_BELOW) != LVHT_BELOW && (flag & LVHT_ABOVE) != LVHT_ABOVE)
{
pImageData->m_rcItem = rcItem;
pImageData->m_rcIcon = rcIcon;
pImageData->m_ptDrag = point;
rcTotalItem.bottom += rcIcon.bottom;
pImageData++;
}
CCF_String ccfText(GetItemText(iItem,0));
CWDClipboardData::Instance()->SetData(&m_OleDataSource,&ccfText,CWDClipboardData::e_cfString);
iItem = GetNextSel(iItem);
pRows[i] = iItem;
}
pData = (int*)pImageData;
*pData++ = rcTotalItem.left;
*pData++ = rcTotalItem.right;
*pData++ = rcTotalItem.top;
*pData = rcTotalItem.bottom;
::GlobalUnlock (hData);
m_OleDataSource.CacheGlobalData(m_OleDropTarget.GetClipboardFormat(), hData);
CCF_RightMenu rm;
rm.SetRightDrag(bRightMenu);
CWDClipboardData::Instance()->SetData(&m_OleDataSource,&rm,CWDClipboardData::e_cfRightMenu);
DWORD dwDragEffect = SendMessage(WM_APP_OLE_DD_DODRAGDROP,(WPARAM)pRows,(LPARAM)&m_OleDataSource);
if (dwDragEffect == 0)
dwDragEffect = GetParent()->SendMessage(WM_APP_OLE_DD_DODRAGDROP,(WPARAM)pRows,(LPARAM)&m_OleDataSource);
if (dwDragEff
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -