📄 treeoptionsctrl.cpp
字号:
if (bEnable)
VERIFY(SetItemImage(hChild, 2, 2));
else
VERIFY(SetItemImage(hChild, 6, 6));
}
//Move on to the next item
hChild = GetNextItem(hChild, TVGN_NEXT);
}
//Reset the redraw flag
SetRedraw(TRUE);
return TRUE;
}
BOOL CTreeOptionsCtrl::GetRadioButton(HTREEITEM hParent, int& nIndex, HTREEITEM& hCheckItem) const
{
ASSERT(IsGroup(hParent)); //Parent item must be a group item
//Iterate through the child items and turn on the specified one and turn off all the other ones
HTREEITEM hChild = GetNextItem(hParent, TVGN_CHILD);
ASSERT(hChild); //Must have some child items
//Find the checked item
nIndex = 0;
BOOL bFound = FALSE;
while (!bFound)
{
if (!IsRadioButton(hChild)) // Handle multiple groups
nIndex = 0;
BOOL bSuccess = GetRadioButton(hChild, bFound);
ASSERT(bSuccess);
//Move on to the next sibling if not found
if (!bFound)
{
hChild = GetNextItem(hChild, TVGN_NEXT);
ASSERT(hChild);
++nIndex;
}
else
{
hCheckItem = hChild;
break; // This group is done
}
}
return TRUE;
}
BOOL CTreeOptionsCtrl::GetRadioButton(HTREEITEM hItem, BOOL& bCheck) const
{
ASSERT(IsRadioButton(hItem)); //Must be a radio item to check it
int nImage;
int nSelectedImage;
BOOL bSuccess = GetItemImage(hItem, nImage, nSelectedImage);
ASSERT(bSuccess);
bCheck = (nImage == 3 || nImage == 7);
return bSuccess;
}
BOOL CTreeOptionsCtrl::SetGroupEnable(HTREEITEM hItem, BOOL bEnable)
{
//Allows you to quickly enable / disable all the items in a group
ASSERT(IsGroup(hItem)); //Must be group item
//Iterate through the child items and enable / disable all the items
HTREEITEM hChild = GetNextItem(hItem, TVGN_CHILD);
//Turn of redraw to Q all the changes we're going to make here
SetRedraw(FALSE);
while (hChild)
{
if (IsRadioButton(hChild))
{
int nImage;
int nSelectedImage;
VERIFY(GetItemImage(hChild, nImage, nSelectedImage));
BOOL bCheck = (nImage == 3 || nImage == 7);
if (bCheck)
{
if (bEnable)
VERIFY(SetItemImage(hChild, 3, 3));
else
VERIFY(SetItemImage(hChild, 7, 7));
}
else
{
if (bEnable)
VERIFY(SetItemImage(hChild, 2, 2));
else
VERIFY(SetItemImage(hChild, 6, 6));
}
}
else if (IsCheckBox(hChild))
VERIFY(SetCheckBoxEnable(hChild, bEnable));
else
ASSERT(FALSE);
//Move onto the next child
hChild = GetNextItem(hChild, TVGN_NEXT);
}
//Reset the redraw flag
SetRedraw(TRUE);
return TRUE;
}
BOOL CTreeOptionsCtrl::GetSemiCheckBox(HTREEITEM hItem, BOOL& bSemi) const
{
ASSERT(IsCheckBox(hItem)); //Must be a check box
int nImage;
int nSelectedImage;
BOOL bSuccess = GetItemImage(hItem, nImage, nSelectedImage);
ASSERT(bSuccess);
bSemi = (nImage == 8 || nImage == 9);
return bSuccess;
}
BOOL CTreeOptionsCtrl::SetCheckBoxEnable(HTREEITEM hItem, BOOL bEnable)
{
ASSERT(IsCheckBox(hItem)); //Must be a check box
BOOL bSuccess = FALSE;
if (bEnable)
{
BOOL bCheck;
VERIFY(GetCheckBox(hItem, bCheck));
BOOL bSemi;
VERIFY(GetSemiCheckBox(hItem, bSemi));
if (bCheck)
{
if (bSemi)
bSuccess = SetItemImage(hItem, 8, 8);
else
bSuccess = SetItemImage(hItem, 1, 1);
}
else
bSuccess = SetItemImage(hItem, 0, 0);
}
else
{
BOOL bCheck;
VERIFY(GetCheckBox(hItem, bCheck));
BOOL bSemi;
VERIFY(GetSemiCheckBox(hItem, bSemi));
if (bCheck)
{
if (bSemi)
bSuccess = SetItemImage(hItem, 9, 9);
else
bSuccess = SetItemImage(hItem, 5, 5);
}
else
bSuccess = SetItemImage(hItem, 4, 4);
}
return bSuccess;
}
BOOL CTreeOptionsCtrl::SetRadioButtonEnable(HTREEITEM hItem, BOOL bEnable)
{
ASSERT(IsRadioButton(hItem)); //Must be a radio button
BOOL bSuccess = FALSE;
if (bEnable)
{
BOOL bCheck;
VERIFY(GetRadioButton(hItem, bCheck));
if (bCheck)
bSuccess = SetItemImage(hItem, 3, 3);
else
bSuccess = SetItemImage(hItem, 2, 2);
}
else
{
BOOL bCheck;
VERIFY(GetRadioButton(hItem, bCheck));
if (bCheck)
bSuccess = SetItemImage(hItem, 7, 7);
else
bSuccess = SetItemImage(hItem, 6, 6);
}
return bSuccess;
}
BOOL CTreeOptionsCtrl::GetCheckBoxEnable(HTREEITEM hItem, BOOL& bEnable) const
{
ASSERT(IsCheckBox(hItem)); //Must be a check box
int nImage;
int nSelectedImage;
BOOL bSuccess = GetItemImage(hItem, nImage, nSelectedImage);
ASSERT(bSuccess);
bEnable = (nImage == 0 || nImage == 1 || nImage == 8);
return bSuccess;
}
BOOL CTreeOptionsCtrl::GetRadioButtonEnable(HTREEITEM hItem, BOOL& bEnable) const
{
ASSERT(IsRadioButton(hItem)); //Must be a radio button
int nImage;
int nSelectedImage;
BOOL bSuccess = GetItemImage(hItem, nImage, nSelectedImage);
ASSERT(bSuccess);
bEnable = (nImage == 2 || nImage == 3);
return bSuccess;
}
void CTreeOptionsCtrl::OnCreateImageList()
{
//Loadup the image list
VERIFY(m_ilTree.Create(m_nilID, 16, 1, RGB(255, 0, 255)));
}
void CTreeOptionsCtrl::PreSubclassWindow()
{
//Let the parent class do its thing
CTreeCtrl::PreSubclassWindow();
//Call the virtual function to setup the image list
OnCreateImageList();
//Hook it up to the tree control
SetImageList(&m_ilTree, TVSIL_NORMAL);
}
BOOL CTreeOptionsCtrl::AddComboBox(HTREEITEM hItem, CRuntimeClass* pRuntimeClass, DWORD dwItemData)
{
ASSERT(pRuntimeClass);
//Delete the old item data in the item if there is one already
CTreeOptionsItemData* pOldItemData = (CTreeOptionsItemData*) GetItemData(hItem);
if (pOldItemData)
delete pOldItemData;
//A pointer to the runtime class is stored in the Item data which itself is an
//internal structure we maintain per tree item
CTreeOptionsItemData* pItemData = new CTreeOptionsItemData;
pItemData->m_dwItemData = dwItemData;
pItemData->m_pRuntimeClass1 = pRuntimeClass;
pItemData->m_Type = CTreeOptionsItemData::ComboBox;
return SetItemData(hItem, (DWORD) pItemData);
}
CString CTreeOptionsCtrl::GetComboText(HTREEITEM hItem) const
{
CString sText = GetItemText(hItem);
int nSeparator = sText.Find(m_sSeparator);
CString sComboText;
if (nSeparator != -1)
sComboText = sText.Right(sText.GetLength() - nSeparator - m_sSeparator.GetLength());
return sComboText;
}
void CTreeOptionsCtrl::RemoveChildControlText(HTREEITEM hItem)
{
CString sText = GetItemText(hItem);
int nSeparator = sText.Find(m_sSeparator);
if (nSeparator != -1)
sText = sText.Left(nSeparator);
SetItemText(hItem, sText);
}
void CTreeOptionsCtrl::SetComboText(HTREEITEM hItem, const CString& sComboText)
{
CString sText = GetItemText(hItem);
int nSeparator = sText.Find(m_sSeparator);
if (nSeparator == -1)
{
sText += m_sSeparator;
sText += sComboText;
}
else
{
sText = sText.Left(nSeparator + m_sSeparator.GetLength());
sText += sComboText;
}
SetItemText(hItem, sText);
}
void CTreeOptionsCtrl::DestroyOldChildControl()
{
if (m_pCombo)
{
m_pCombo->DestroyWindow();
delete m_pCombo;
m_pCombo = NULL;
}
if (m_pEdit)
{
m_pEdit->DestroyWindow();
delete m_pEdit;
m_pEdit = NULL;
}
if (m_pSpin)
{
m_pSpin->DestroyWindow();
delete m_pSpin;
m_pSpin = NULL;
}
if (m_pButton)
{
m_pButton->DestroyWindow();
delete m_pButton;
m_pButton = NULL;
}
if (m_pDateTime)
{
m_pDateTime->DestroyWindow();
delete m_pDateTime;
m_pDateTime = NULL;
}
if (m_pIPAddress)
{
m_pIPAddress->DestroyWindow();
delete m_pIPAddress;
m_pIPAddress = NULL;
}
//Free up the font object we have been using
m_Font.DeleteObject();
m_hControlItem = NULL;
}
void CTreeOptionsCtrl::CreateNewChildControl(HTREEITEM hItem)
{
ASSERT(hItem);
m_hControlItem = hItem;
CTreeOptionsItemData* pItemData = (CTreeOptionsItemData*) GetItemData(hItem);
ASSERT(pItemData);
//Make a copy of the current font being used by the control
ASSERT(m_Font.m_hObject == NULL);
CFont* pFont = GetFont();
LOGFONT lf;
pFont->GetLogFont(&lf);
VERIFY(m_Font.CreateFontIndirect(&lf));
//Allocate the main control
ASSERT(pItemData->m_pRuntimeClass1);
CString sComboText;
CString sEditText;
if (pItemData->m_pRuntimeClass1->IsDerivedFrom(RUNTIME_CLASS(CTreeOptionsCombo)))
{
//Get the current text in the combo item
sComboText = GetComboText(hItem);
//Now that we have the current text remove it from the tree control text
RemoveChildControlText(hItem);
//Create the new combo box
m_pCombo = (CTreeOptionsCombo*) pItemData->m_pRuntimeClass1->CreateObject();
ASSERT(m_pCombo);
ASSERT(m_pCombo->IsKindOf(RUNTIME_CLASS(CTreeOptionsCombo))); //Your class must be derived from CTreeOptionsCombo
m_pCombo->SetTreeBuddy(this);
m_pCombo->SetTreeItem(hItem);
}
else if (pItemData->m_pRuntimeClass1->IsDerivedFrom(RUNTIME_CLASS(CTreeOptionsEdit)))
{
//Get the current text in the edit box item
sEditText = GetEditText(hItem);
//Now that we have the current text remove it from the tree control text
RemoveChildControlText(hItem);
//Create the new edit box
m_pEdit = (CTreeOptionsEdit*) pItemData->m_pRuntimeClass1->CreateObject();
ASSERT(m_pEdit);
ASSERT(m_pEdit->IsKindOf(RUNTIME_CLASS(CTreeOptionsEdit))); //Your class must be derived from CTreeOptionsEdit
m_pEdit->SetTreeBuddy(this);
m_pEdit->SetTreeItem(hItem);
}
else if (pItemData->m_pRuntimeClass1->IsDerivedFrom(RUNTIME_CLASS(CTreeOptionsDateCtrl)))
{
//Get the current text in the edit box item
sEditText = GetEditText(hItem);
//Now that we have the current text remove it from the tree control text
RemoveChildControlText(hItem);
//Create the new edit box
m_pDateTime = (CTreeOptionsDateCtrl*) pItemData->m_pRuntimeClass1->CreateObject();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -