📄 xlistctrl.cpp
字号:
if (nIDEvent == 1) // timer set when combo button is clicked
{
if (m_bComboIsClicked)
{
POINT point;
::GetCursorPos(&point);
ScreenToClient(&point);
if (!m_rectComboButton.PtInRect(point))
{
UnpressComboButton();
}
}
else if (m_pListBox)
{
m_pListBox->SetActive(11);
}
else
{
KillTimer(nIDEvent);
}
}
else if (nIDEvent == 2) // close combo listbox
{
KillTimer(nIDEvent);
if (m_pListBox)
{
m_pListBox->DestroyWindow();
delete m_pListBox;
}
m_pListBox = NULL;
}
else if (nIDEvent == 3) // get combo listbox selection, then close combo listbox
{
KillTimer(nIDEvent);
if (m_pListBox)
{
CString str;
int i = m_pListBox->GetCurSel();
if (i != LB_ERR)
{
m_pListBox->GetText(i, str);
if ((m_nComboItem >= 0 && m_nComboItem < GetItemCount()) &&
(m_nComboSubItem >= 0 && m_nComboSubItem < GetColumns()))
{
SetItemText(m_nComboItem, m_nComboSubItem, str);
UpdateSubItem(m_nComboItem, m_nComboSubItem);
CWnd *pWnd = GetParent();
if (!pWnd)
pWnd = GetOwner();
if (pWnd && ::IsWindow(pWnd->m_hWnd))
pWnd->SendMessage(WM_XLISTCTRL_COMBO_SELECTION,
m_nComboItem, m_nComboSubItem);
}
}
m_pListBox->DestroyWindow();
delete m_pListBox;
}
m_pListBox = NULL;
}
else if (nIDEvent == 4) // get combo listbox selection
{
KillTimer(nIDEvent);
if (m_pListBox)
{
CString str;
int i = m_pListBox->GetCurSel();
if (i != LB_ERR)
{
m_pListBox->GetText(i, str);
if ((m_nComboItem >= 0 && m_nComboItem < GetItemCount()) &&
(m_nComboSubItem >= 0 && m_nComboSubItem < GetColumns()))
{
SetItemText(m_nComboItem, m_nComboSubItem, str);
UpdateSubItem(m_nComboItem, m_nComboSubItem);
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// OnComboEscape
LRESULT CXListCtrl::OnComboEscape(WPARAM, LPARAM)
{
KillTimer(1);
SetTimer(2, 50, NULL);
// restore original string
SetItemText(m_nComboItem, m_nComboSubItem, m_strInitialComboString);
UpdateSubItem(m_nComboItem, m_nComboSubItem);
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// OnComboReturn
LRESULT CXListCtrl::OnComboReturn(WPARAM, LPARAM)
{
TRACE(_T("in CXListCtrl::OnComboReturn\n"));
KillTimer(1);
SetTimer(3, 50, NULL);
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// OnComboLButtonUp
LRESULT CXListCtrl::OnComboLButtonUp(WPARAM, LPARAM)
{
TRACE(_T("in CXListCtrl::OnComboLButtonUp\n"));
KillTimer(1);
SetTimer(3, 50, NULL);
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// OnComboKeydown
LRESULT CXListCtrl::OnComboKeydown(WPARAM, LPARAM)
{
SetTimer(4, 50, NULL);
return 0;
}
#endif
#ifndef NO_XLISTCTRL_TOOL_TIPS
///////////////////////////////////////////////////////////////////////////////
// OnToolHitTest
int CXListCtrl::OnToolHitTest(CPoint point, TOOLINFO * pTI) const
{
LVHITTESTINFO lvhitTestInfo;
lvhitTestInfo.pt = point;
int nItem = ListView_SubItemHitTest(this->m_hWnd, &lvhitTestInfo);
int nSubItem = lvhitTestInfo.iSubItem;
TRACE(_T("in CToolTipListCtrl::OnToolHitTest: %d,%d\n"), nItem, nSubItem);
UINT nFlags = lvhitTestInfo.flags;
// nFlags is 0 if the SubItemHitTest fails
// Therefore, 0 & <anything> will equal false
if (nFlags & LVHT_ONITEMLABEL)
{
// If it did fall on a list item,
// and it was also hit one of the
// item specific subitems we wish to show tool tips for
// get the client (area occupied by this control
RECT rcClient;
GetClientRect(&rcClient);
// fill in the TOOLINFO structure
pTI->hwnd = m_hWnd;
pTI->uId = (UINT) (nItem * 1000 + nSubItem + 1);
pTI->lpszText = LPSTR_TEXTCALLBACK;
pTI->rect = rcClient;
return pTI->uId; // By returning a unique value per listItem,
// we ensure that when the mouse moves over another
// list item, the tooltip will change
}
else
{
//Otherwise, we aren't interested, so let the message propagate
return -1;
}
}
///////////////////////////////////////////////////////////////////////////////
// OnToolTipText
BOOL CXListCtrl::OnToolTipText(UINT /*id*/, NMHDR * pNMHDR, LRESULT * pResult)
{
UINT nID = pNMHDR->idFrom;
TRACE(_T("in CXListCtrl::OnToolTipText: id=%d\n"), nID);
// check if this is the automatic tooltip of the control
if (nID == 0)
return TRUE; // do not allow display of automatic tooltip,
// or our tooltip will disappear
// handle both ANSI and UNICODE versions of the message
TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
*pResult = 0;
// get the mouse position
const MSG* pMessage;
pMessage = GetCurrentMessage();
ASSERT(pMessage);
CPoint pt;
pt = pMessage->pt; // get the point from the message
ScreenToClient(&pt); // convert the point's coords to be relative to this control
// see if the point falls onto a list item
LVHITTESTINFO lvhitTestInfo;
lvhitTestInfo.pt = pt;
int nItem = SubItemHitTest(&lvhitTestInfo);
int nSubItem = lvhitTestInfo.iSubItem;
UINT nFlags = lvhitTestInfo.flags;
// nFlags is 0 if the SubItemHitTest fails
// Therefore, 0 & <anything> will equal false
if (nFlags & LVHT_ONITEMLABEL)
{
// If it did fall on a list item,
// and it was also hit one of the
// item specific subitems we wish to show tooltips for
CString strToolTip;
strToolTip = _T("");
XLISTCTRLDATA *pXLCD = (XLISTCTRLDATA *) CListCtrl::GetItemData(nItem);
if (pXLCD)
{
strToolTip = pXLCD[nSubItem].strToolTip;
}
if (!strToolTip.IsEmpty())
{
// If there was a CString associated with the list item,
// copy it's text (up to 80 characters worth, limitation
// of the TOOLTIPTEXT structure) into the TOOLTIPTEXT
// structure's szText member
#ifndef _UNICODE
if (pNMHDR->code == TTN_NEEDTEXTA)
lstrcpyn(pTTTA->szText, strToolTip, 80);
else
_mbstowcsz(pTTTW->szText, strToolTip, 80);
#else
if (pNMHDR->code == TTN_NEEDTEXTA)
_wcstombsz(pTTTA->szText, strToolTip, 80);
else
lstrcpyn(pTTTW->szText, strToolTip, 80);
#endif
return FALSE; // we found a tool tip,
}
}
return FALSE; // we didn't handle the message, let the
// framework continue propagating the message
}
///////////////////////////////////////////////////////////////////////////////
// SetItemToolTipText
BOOL CXListCtrl::SetItemToolTipText(int nItem, int nSubItem, LPCTSTR lpszToolTipText)
{
ASSERT(nItem >= 0);
ASSERT(nItem < GetItemCount());
if ((nItem < 0) || nItem >= GetItemCount())
return FALSE;
ASSERT(nSubItem >= 0);
ASSERT(nSubItem < GetColumns());
if ((nSubItem < 0) || nSubItem >= GetColumns())
return FALSE;
XLISTCTRLDATA *pXLCD = (XLISTCTRLDATA *) CListCtrl::GetItemData(nItem);
if (!pXLCD)
{
return FALSE;
}
pXLCD[nSubItem].strToolTip = lpszToolTipText;
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// GetItemToolTipText
CString CXListCtrl::GetItemToolTipText(int nItem, int nSubItem)
{
CString strToolTip;
strToolTip = _T("");
ASSERT(nItem >= 0);
ASSERT(nItem < GetItemCount());
if ((nItem < 0) || nItem >= GetItemCount())
return strToolTip;
ASSERT(nSubItem >= 0);
ASSERT(nSubItem < GetColumns());
if ((nSubItem < 0) || nSubItem >= GetColumns())
return strToolTip;
XLISTCTRLDATA *pXLCD = (XLISTCTRLDATA *) CListCtrl::GetItemData(nItem);
if (pXLCD)
{
strToolTip = pXLCD[nSubItem].strToolTip;
}
return strToolTip;
}
///////////////////////////////////////////////////////////////////////////////
// DeleteAllToolTips
void CXListCtrl::DeleteAllToolTips()
{
int nRow = GetItemCount();
int nCol = GetColumns();
for (int nItem = 0; nItem < nRow; nItem++)
{
XLISTCTRLDATA *pXLCD = (XLISTCTRLDATA *) CListCtrl::GetItemData(nItem);
if (pXLCD)
for (int nSubItem = 0; nSubItem < nCol; nSubItem++)
pXLCD[nSubItem].strToolTip = _T("");
}
}
#endif
int CXListCtrl::InsertItemEx( int nItem )
{
int cntItem = 0;
int nResult = -1;
CString csNO;
csNO.Format( "%d", maxid+1);
nResult = InsertItem( nItem, csNO );
maxid++;
return nResult;
}
void CXListCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if ( GetFocus() != this )
SetFocus();
CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CXListCtrl::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if ( GetFocus() != this )
SetFocus();
CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
}
CEdit* CXListCtrl::EditSubItem( int nItem, int nSubItem )
{
if ( nItem < 0 || nSubItem < 0 )
return NULL;
int nItems = GetItemCount();
if ( nItem >= nItems )
InsertItemEx( nItem );
if ( !EnsureVisible( nItem, TRUE ) )
{
InsertItemEx( nItem );
if ( !EnsureVisible( nItem, TRUE ) )
return NULL;
}
// Make sure that nSubItem is valid
CHeaderCtrl* pHeader = GetHeaderCtrl();
int nColumns = pHeader->GetItemCount();
if ( nSubItem >= nColumns )//|| GetColumnWidth(nSubItem) < 10 )
return NULL; // If column width less than 10 then it didn't be edited
// Retrieve the text of the selected subItem from the list
CRect rectEdit; // Get sub item rectangle that was clicked
CRect rectClient; // Get client rectangle of list control
Text = GetItemText( nItem, nSubItem );
GetSubItemRect( nItem, nSubItem, LVIR_BOUNDS, rectEdit );
GetClientRect( &rectClient );
if ( nSubItem == 0 && nColumns > 1) // The width is full item width
{
CRect rectSub1;
GetSubItemRect( nItem, 1, LVIR_BOUNDS, rectSub1 );
rectEdit.right = rectSub1.left;
}
if ( rectEdit.right > rectClient.right ) // Out of client
rectEdit.right = rectClient.right;
// Create edit cell
DWORD dwStyle;
if(nSubItem==3&&flag==1)
dwStyle = WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL|ES_PASSWORD;
else if(nSubItem==2&&flag==2)
dwStyle = WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL|ES_PASSWORD;
else
dwStyle = WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL;
bool doubleLimited = false;
//if (nSubItem == 4 || nSubItem == 5) doubleLimited = true;
CEdit* pEdit = new CYHEditCell( this, nItem, nSubItem, Text,doubleLimited);
if ( pEdit->Create( dwStyle, rectEdit, this, IDC_EDIT_CELL ) == 0 )
return NULL; // Crate edit control failed !
return pEdit;
}
void CXListCtrl::updateStringItem(int nItem, int nSubItem, LPCTSTR dval)
{
if (nSubItem ==0 ) return;
if(strcmp(Text,dval)==0) return;
else
{
if(flag==1)
{
switch (nSubItem)
{
case 1:
{
UserInfo[nItem].sign[0]=1;
UserInfo[nItem].fname = dval;
UserInfo[nItem].fname = UserInfo[nItem].fname.Left(20);
break;
}
case 2:
{
UserInfo[nItem].sign[1]=1;
UserInfo[nItem].lname = dval;
UserInfo[nItem].lname = UserInfo[nItem].lname.Left(10);
break;
}
case 3:
{
UserInfo[nItem].sign[2]=1;
UserInfo[nItem].code = dval;
UserInfo[nItem].code =UserInfo[nItem].code.Left(8);
break;
}
case 5:
{
UserInfo[nItem].sign[4]=1;
UserInfo[nItem].des = dval;
UserInfo[nItem].des = UserInfo[nItem].des.Left(40);
break;
}
default:
break;
}
}
else
{
switch (nSubItem)
{
case 1:
{
GroupInfo[nItem].flag1=1;
GroupInfo[nItem].qname = dval;
GroupInfo[nItem].qname = GroupInfo[nItem].qname.Left(30);
break;
}
case 2:
{
GroupInfo[nItem].flag2=1;
GroupInfo[nItem].qcode = dval;
GroupInfo[nItem].qcode = GroupInfo[nItem].qcode.Left(8);
break;
}
case 3:
{
GroupInfo[nItem].flag3=1;
GroupInfo[nItem].qdes = dval;
GroupInfo[nItem].qdes =GroupInfo[nItem].qdes.Left(40);
break;
}
default:
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -