📄 combolistctrl.cpp
字号:
#include "stdafx.h"
#include "..\Resource.h"
#include "..\comboxlist\ComboListCtrl.h"
#include "InPlaceCombo.h"
#include "InPlaceEdit.h"
//#include "..\Data.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//#defines
#define FIRST_COLUMN 0
#define MIN_COLUMN_WIDTH 10
#define MAX_DROP_DOWN_ITEM_COUNT 10
/////////////////////////////////////////////////////////////////////////////
// CComboListCtrl
CComboListCtrl::CComboListCtrl()
{
m_iColumnCounts = 0;
m_ComboSupportColumnsList.RemoveAll();
m_ReadOnlyColumnsList.RemoveAll();
m_strValidEditCtrlChars.Empty();
m_dwEditCtrlStyle = ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_LEFT | ES_NOHIDESEL;
m_dwDropDownCtrlStyle = WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_AUTOVSCROLL |
CBS_DROPDOWNLIST | CBS_DISABLENOSCROLL;
for ( int i = 0; i < MAX_LISTCTRL_LINES; i++)
{
CheckArray[i][0] = 0;
}
}
CComboListCtrl::~CComboListCtrl()
{
CInPlaceCombo::DeleteInstance();
CInPlaceEdit::DeleteInstance();
}
BEGIN_MESSAGE_MAP(CComboListCtrl, CListCtrl)
//{{AFX_MSG_MAP(CComboListCtrl)
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndLabelEdit)
ON_NOTIFY_REFLECT(LVN_BEGINLABELEDIT, OnBeginLabelEdit)
ON_WM_MEASUREITEM_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CComboListCtrl message handlers
void CComboListCtrl::MeasureItem ( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
{
lpMeasureItemStruct->itemHeight = 15;
}
void CComboListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (lpDrawItemStruct->CtlType != ODT_LISTVIEW)
return;
CRect rect1 = lpDrawItemStruct->rcItem;
//*///
//如果需要整个要画
if(lpDrawItemStruct->itemAction == ODA_DRAWENTIRE)
{
HDC hdc = lpDrawItemStruct->hDC;
CDC * pdc = CDC::FromHandle(hdc);
// ::SetBkColor(hdc, ::GetSysColor(isSelected ? COLOR_HIGHLIGHT : COLOR_WINDOW));
BOOL isSelected = lpDrawItemStruct->itemState & ODS_SELECTED;
CBrush* pBrush = new CBrush;
pBrush->CreateSolidBrush((lpDrawItemStruct->itemState&ODS_SELECTED)?
RGB(0,255,255):(::GetSysColor(COLOR_WINDOW)));
pdc->FillRect(&lpDrawItemStruct->rcItem,pBrush);
delete pBrush;
if (lpDrawItemStruct->itemState&ODS_SELECTED)
{
pdc->DrawFocusRect(rect1);
} //*///
int nCount = GetHeaderCtrl()->GetItemCount(); //得到子项数
CString strSubText;
CRect rectEntity;
CRect rectColumn;
int iIndexRow = lpDrawItemStruct->itemID;
int iWidthColumn = GetColumnWidth(0);
rectEntity = lpDrawItemStruct->rcItem;
rectColumn.left = rectEntity.left;
rectColumn.top = rectEntity.top;
rectColumn.bottom = rectEntity.bottom;
rectColumn.right = rectColumn.left + iWidthColumn; //初始化第一列矩形
CRect rectFirstCol = rectColumn; //第一列的矩形框
CRect rectCheck;
int m_iCheck;
//调整checkbox的大小
rectCheck.CopyRect(&rectFirstCol);
rectCheck.right = rectCheck.left + 15;
m_iCheck = CheckArray[iIndexRow][0];
if (m_iCheck == 2) //将要确定选择的
::DrawFrameControl(hdc, &rectCheck,
DFC_BUTTON, DFCS_FLAT | DFCS_BUTTONCHECK | DFCS_CHECKED);
else if (m_iCheck == 0) //还没有选择的
::DrawFrameControl(hdc, &rectCheck,
DFC_BUTTON, DFCS_FLAT | DFCS_BUTTONCHECK);
else //已经确认选择了的
{
CBrush brush(RGB(125,125,125));
pdc->FillRect(&rectCheck, &brush);
}
CString strColorValve; //界面上的颜色值
strColorValve = GetItemText(iIndexRow, 1);
int iFirstWidth = GetColumnWidth(0); //第一列的宽度
CRect rectText; //第一列文字所在的矩形
rectText.CopyRect(&rectFirstCol);
rectText.left = rectCheck.right + 1;
CString strText;
strText = GetItemText(iIndexRow, 0);
//画出第一列的文字
::ExtTextOut(hdc, rectText.left, rectText.top, ETO_CLIPPED, &rectText,
strText, strText.GetLength(), NULL);
for (int i = 0; i < nCount; i++)
{
//第2列的矩形
iWidthColumn = GetColumnWidth(i);
rectColumn.left = rectColumn.left + iWidthColumn;
rectColumn.right = rectColumn.left + GetColumnWidth(i + 1);
strText = GetItemText(iIndexRow ,i + 1);
::ExtTextOut(hdc, rectColumn.left + 1, rectColumn.top, ETO_CLIPPED, &rectColumn,
strText, strText.GetLength(), NULL);
}
}
}
CInPlaceCombo* CComboListCtrl::ShowInPlaceList(int iRowIndex, int iColumnIndex, CStringList& rComboItemsList,
CString strCurSelecetion /*= ""*/, int iSel /*= -1*/)
{
// The returned obPointer should not be saved
// Make sure that the item is visible
if (!EnsureVisible(iRowIndex, TRUE))
{
return NULL;
}
// Make sure that iColumnIndex is valid
CHeaderCtrl* pHeader = static_cast<CHeaderCtrl*> (GetDlgItem(FIRST_COLUMN));
int iColumnCount = pHeader->GetItemCount();
if (iColumnIndex >= iColumnCount || GetColumnWidth(iColumnIndex) < MIN_COLUMN_WIDTH)
{
return NULL;
}
// Calculate the rectangle specifications for the combo box
CRect obCellRect(0, 0, 0, 0);
CalculateCellRect(iColumnIndex, iRowIndex, obCellRect);
int iHeight = obCellRect.Height();
int iCount = (int )rComboItemsList.GetCount();
iCount = (iCount < MAX_DROP_DOWN_ITEM_COUNT) ?
iCount + MAX_DROP_DOWN_ITEM_COUNT : (MAX_DROP_DOWN_ITEM_COUNT + 1);
obCellRect.bottom += iHeight * iCount;
// Create the in place combobox
CInPlaceCombo* pInPlaceCombo = CInPlaceCombo::GetInstance();
//uiResourceID = 0;
pInPlaceCombo->ShowComboCtrl(m_dwDropDownCtrlStyle, obCellRect, this, 0, iRowIndex, iColumnIndex, &rComboItemsList,
strCurSelecetion, iSel);
return pInPlaceCombo;
}
CInPlaceEdit* CComboListCtrl::ShowInPlaceEdit(int iRowIndex, int iColumnIndex, CString& rstrCurSelection)
{
// Create an in-place edit control
CInPlaceEdit* pInPlaceEdit = CInPlaceEdit::GetInstance();
CRect obCellRect(0, 0, 0, 0);
CalculateCellRect(iColumnIndex, iRowIndex, obCellRect);
pInPlaceEdit->ShowEditCtrl(m_dwEditCtrlStyle, obCellRect, this, 0,
iRowIndex, iColumnIndex,
m_strValidEditCtrlChars, rstrCurSelection);
return pInPlaceEdit;
}
void CComboListCtrl::OnHScroll(UINT iSBCode, UINT iPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if (GetFocus() != this)
{
SetFocus();
}
this->Invalidate();
CListCtrl::OnHScroll(iSBCode, iPos, pScrollBar);
}
void CComboListCtrl::OnVScroll(UINT iSBCode, UINT iPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if (GetFocus() != this)
{
SetFocus();
}
CListCtrl::OnVScroll(iSBCode, iPos, pScrollBar);
}
void CComboListCtrl::SetCheck(int row ,int col,int check)
{
CheckArray[row][col] =check;
}
int CComboListCtrl::GetCheck(int row,int col)
{
return CheckArray[row][col];
}
void CComboListCtrl::OnLButtonDown(UINT iFlags, CPoint obPoint)
{
// TODO: Add your message handler code here and/or call default
int iColumnIndex = -1;
int iRowIndex = -1;
// Get the current column and row
if (!HitTestEx(obPoint, &iRowIndex, &iColumnIndex))
{
return;
}
CListCtrl::OnLButtonDown(iFlags, obPoint);
// If column is not read only then
// If the SHIFT or CTRL key is down call the base class
// Check the high bit of GetKeyState to determine whether SHIFT or CTRL key is down
if ((GetKeyState(VK_SHIFT) & 0x80) || (GetKeyState(VK_CONTROL) & 0x80))
{
return;
}
// Get the current selection before creating the in place combo box
CString strCurSelection = GetItemText(iRowIndex, iColumnIndex);
if (-1 != iRowIndex)
{
UINT flag = LVIS_FOCUSED;
if ((GetItemState(iRowIndex, flag ) & flag) == flag)
{
// Add check for LVS_EDITLABELS
if (GetWindowLong(m_hWnd, GWL_STYLE) & LVS_EDITLABELS)
{
// If combo box is supported
// Create and show the in place combo box
if (IsCombo(iColumnIndex))
{
CStringList obComboItemsList;
GetParent()->SendMessage(WM_SET_ITEMS, (WPARAM)iColumnIndex, (LPARAM)&obComboItemsList);
CInPlaceCombo* pInPlaceComboBox = ShowInPlaceList(iRowIndex, iColumnIndex, obComboItemsList, strCurSelection);
ASSERT(pInPlaceComboBox);
// Set the selection to previous selection
pInPlaceComboBox->SelectString(-1, strCurSelection);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -