📄 combolistctrl.cpp
字号:
// If combo box is not read only
// Create and show the in place edit control
else if (!IsReadOnly(iColumnIndex))
{
CInPlaceEdit* pInPlaceEdit = ShowInPlaceEdit(iRowIndex, iColumnIndex, strCurSelection);
}
}
}
}
}
void CComboListCtrl::OnRButtonDown(UINT iFlags, CPoint obPoint)
{
/*
int iColumnIndex = -1;
int iRowIndex = -1;
// Get the current column and row
if (!HitTestEx(obPoint, &iRowIndex, &iColumnIndex))
{
return;
}
CMenu PopupMenu;
PopupMenu.LoadMenu(IDR_USRE_OPERATION);
//ScreenToClient(&obPoint);
ClientToScreen(&obPoint);
PopupMenu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN, obPoint.x, obPoint.y, this);
*/
}
bool CComboListCtrl::HitTestEx(CPoint &obPoint, int* pRowIndex, int* pColumnIndex) const
{
if (!pRowIndex || !pColumnIndex)
{
return false;
}
// The index of the item at the position specified by pHitTestInfo,
//if any, or -1 otherwise
*pRowIndex = HitTest(obPoint, NULL);
//更改
if (-1 == *pRowIndex)
{
return false;
}
///
if (pColumnIndex)
{
*pColumnIndex = 0;
}
// Make sure that the ListView is in LVS_REPORT
if ((GetWindowLong(m_hWnd, GWL_STYLE) & LVS_TYPEMASK) != LVS_REPORT)
{
return false;
}
// Get the number of columns
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
int iColumnCount = pHeader->GetItemCount();
// Get bounding rect of item and check whether obPoint falls in it.
CRect obCellRect;
GetItemRect(*pRowIndex, &obCellRect, LVIR_BOUNDS);
if (obCellRect.PtInRect(obPoint))
{
// Now find the column
for (*pColumnIndex = 0; *pColumnIndex < iColumnCount; (*pColumnIndex)++)
{
int iColWidth = GetColumnWidth(*pColumnIndex);
if (obPoint.x >= obCellRect.left && obPoint.x <= (obCellRect.left + iColWidth))
{
return true;
}
obCellRect.left += iColWidth;
}
}
return false;
}
void CComboListCtrl::SetComboColumns(int iColumnIndex, bool bSet /*= true*/)
{
// If the Column Index is not present && Set flag is false
// Then do nothing
// If the Column Index is present && Set flag is true
// Then do nothing
POSITION Pos = m_ComboSupportColumnsList.Find(iColumnIndex);
// If the Column Index is not present && Set flag is true
// Then Add to list
if ((NULL == Pos) && bSet)
{
m_ComboSupportColumnsList.AddTail(iColumnIndex);
}
// If the Column Index is present && Set flag is false
// Then Remove from list
if ((NULL != Pos) && !bSet)
{
m_ComboSupportColumnsList.RemoveAt(Pos);
}
}
void CComboListCtrl::SetReadOnlyColumns(int iColumnIndex, bool bSet /*= true*/)
{
// If the Column Index is not present && Set flag is false
// Then do nothing
// If the Column Index is present && Set flag is true
// Then do nothing
POSITION Pos = m_ReadOnlyColumnsList.Find(iColumnIndex);
// If the Column Index is not present && Set flag is true
// Then Add to list
if ((NULL == Pos) && bSet)
{
m_ReadOnlyColumnsList.AddTail(iColumnIndex);
}
// If the Column Index is present && Set flag is false
// Then Remove from list
if ((NULL != Pos) && !bSet)
{
m_ReadOnlyColumnsList.RemoveAt(Pos);
}
}
bool CComboListCtrl::IsReadOnly(int iColumnIndex)
{
if (m_ReadOnlyColumnsList.Find(iColumnIndex))
{
return true;
}
return false;
}
bool CComboListCtrl::IsCombo(int iColumnIndex)
{
if (m_ComboSupportColumnsList.Find(iColumnIndex))
{
return true;
}
return false;
}
void CComboListCtrl::CalculateCellRect(int iColumnIndex, int iRowIndex, CRect& robCellRect)
{
GetItemRect(iRowIndex, &robCellRect, LVIR_BOUNDS);
CRect rcClient;
GetClientRect(&rcClient);
if (robCellRect.right > rcClient.right)
{
robCellRect.right = rcClient.right;
}
ScrollToView(iColumnIndex, robCellRect);
}
void CComboListCtrl::OnEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
// TODO: Add your control notification handler code here
SetItemText(pDispInfo->item.iItem, pDispInfo->item.iSubItem, pDispInfo->item.pszText);
TRACE("%s",pDispInfo->item.pszText);
// Update the item text with the new text
// GetParent()->SendMessage(WM_VALIDATE, GetDlgCtrlID(), (LPARAM)pDispInfo);
*pResult = 0;
}
void CComboListCtrl::SetValidEditCtrlCharacters(CString &rstrValidCharacters)
{
m_strValidEditCtrlChars = rstrValidCharacters;
}
void CComboListCtrl::SetColumnValidEditCtrlCharacters(CString &rstrValidCharacters,int column)
{
//
if(column>MAX_LISTCTRL_COLUMNS-1)
return;
if(column == -1)
{
for(int i=0;i<MAX_LISTCTRL_COLUMNS;i++)
{
m_strValidChars[i] = rstrValidCharacters;
}
}
else
m_strValidChars[column] = rstrValidCharacters;
}
void CComboListCtrl::EnableHScroll(bool bEnable /*= true*/)
{
if (bEnable)
{
m_dwDropDownCtrlStyle |= WS_HSCROLL;
}
else
{
m_dwDropDownCtrlStyle &= ~WS_HSCROLL;
}
}
void CComboListCtrl::EnableVScroll(bool bEnable /*= true*/)
{
if (bEnable)
{
m_dwDropDownCtrlStyle |= WS_VSCROLL;
}
else
{
m_dwDropDownCtrlStyle &= ~WS_VSCROLL;
}
}
void CComboListCtrl::ScrollToView(int iColumnIndex, /*int iOffSet, */CRect& robCellRect)
{
// Now scroll if we need to expose the column
CRect rcClient;
GetClientRect(&rcClient);
int iColumnWidth = GetColumnWidth(iColumnIndex);
// Get the column iOffset
int iOffSet = 0;
for (int iIndex_ = 0; iIndex_ < iColumnIndex; iIndex_++)
{
iOffSet += GetColumnWidth(iIndex_);
}
// If x1 of cell rect is < x1 of ctrl rect or
// If x1 of cell rect is > x1 of ctrl rect or **Should not ideally happen**
// If the width of the cell extends beyond x2 of ctrl rect then
// Scroll
CSize obScrollSize(0, 0);
//if 选择的comlunm的左边不在客户区里
//滚动到选择comlunm的左边
if (((iOffSet + robCellRect.left) < rcClient.left) ||
((iOffSet + robCellRect.left) > rcClient.right))
{
obScrollSize.cx = iOffSet + robCellRect.left;
Scroll(obScrollSize);
}
//更改
/* //if 选择的comlunm的左边在客户区里但是右边不在客户区
//滚动一个不在客户区那一部分的距离
else if ((iOffSet + robCellRect.left + iColumnWidth) > rcClient.right)
{
obScrollSize.cx = iOffSet + robCellRect.left + iColumnWidth - rcClient.right;
}
*/
//我屏蔽了 Scroll(obScrollSize);
// Set the width to the column width
robCellRect.left += iOffSet;
robCellRect.right = robCellRect.left + iColumnWidth;
}
void CComboListCtrl::OnBeginLabelEdit(NMHDR* pNMHDR, LRESULT* pResult)
{
// LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
// // TODO: Add your control notification handler code here
// if (IsReadOnly(pDispInfo->item.iSubItem))
// {
// *pResult = 1;
// return;
// }
//不能出现编辑框
*pResult = 1;
}
int CComboListCtrl::InsertColumn(int nCol,LPCTSTR lpszColumnHeading,int nFormat ,int nWidth,int nSubItem)
{
m_iColumnCounts++;
return CListCtrl::InsertColumn( nCol, lpszColumnHeading, nFormat, nWidth, nSubItem);
}
int CComboListCtrl::GetColumnCounts()
{
return m_iColumnCounts;
}
void CComboListCtrl::DeleteAllColumn()
{
for(int i=0;i<m_iColumnCounts;i++)
{
DeleteColumn(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -