vs DuiLib 如何在 CListUI加CCheckBoxUI
时间: 2024-02-20 11:56:43 浏览: 29
在 Visual Studio 中使用 DuiLib 在 CListUI 中添加 CCheckBoxUI 的步骤如下:
1. 在 List 控件的 XML 文件中添加 CheckBox 控件的代码,例如:
```xml
<CheckBox name="check" normalimage="check.png" />
```
2. 在 CListUI 的派生类中,覆盖 CListUI::GetItemCheckBoxRect 方法,以便为每个项提供一个 CheckBox 的位置和大小。例如:
```cpp
CRect MyListUI::GetItemCheckBoxRect(CControlUI* pItem)
{
if (pItem == NULL) return CRect();
CRect rcItem = pItem->GetPos();
return CRect(rcItem.left + 10, rcItem.top + (rcItem.GetHeight() - 16) / 2, rcItem.left + 26, rcItem.top + (rcItem.GetHeight() + 16) / 2);
}
```
3. 在 OnItemActivate 事件处理程序中,切换 CheckBox 控件的选中状态。例如:
```cpp
void MyListUI::OnItemActivate(TNotifyUI& msg)
{
if (msg.pSender == this)
{
int nIndex = GetCurSel();
if (nIndex >= 0 && nIndex < GetCount())
{
CControlUI* pItem = GetItemAt(nIndex);
CCheckBoxUI* pCheckBox = dynamic_cast<CCheckBoxUI*>(pItem->FindSubControl(L"check"));
if (pCheckBox != NULL)
{
pCheckBox->Selected(!pCheckBox->IsSelected());
}
}
}
}
```
4. 在资源文件中添加 CheckBox 控件的图片资源。
5. 在 CListUI 的派生类中,覆盖 CListUI::DrawItem 方法,以便在每个项上绘制 CheckBox 控件。例如:
```cpp
void MyListUI::DrawItem(HDC hDC, CControlUI* pItem)
{
if (pItem == NULL) return;
CCheckBoxUI* pCheckBox = dynamic_cast<CCheckBoxUI*>(pItem->FindSubControl(L"check"));
if (pCheckBox != NULL)
{
CRect rcCheckBox = GetItemCheckBoxRect(pItem);
pCheckBox->SetPos(rcCheckBox);
pCheckBox->DoPaint(hDC, rcCheckBox);
}
CListUI::DrawItem(hDC, pItem);
}
```
这样,你就可以在 DuiLib 中的 CListUI 控件中添加 CCheckBoxUI 控件了。