如何在调用 DDX_Control 之前更改使用 DDX_Control 初始化的控件 (CListBox) 的样式

How can I change the style of a control (CListBox) initialized with DDX_Control, before DDX_Control is called

本文关键字:Control DDX 样式 初始化 控件 CListBox 调用      更新时间:2023-10-16

我正在修改一个现有项目,并且对话框包含控件,在某些情况下,我将以不同的方式对主题进行子类化(在其他情况下,我将完全不理会它)。 当DDX_Control()DoDataExchange()期间被调用时,ListBox的 hwnd 已经应用了样式。 具体来说,此时即使我SetWindowLongPtr()LBS_OWNERDRAWFIXED也不起作用。 通过"不起作用",我的意思是,尽管应用了样式,但 CListBox 不会收到所有者绘制消息。

相反,如果我避免DDX_Control()而只是执行创建,则 ListBox 确实会收到消息并且可以由所有者绘制。 但是如果我这样做,现在有两个 HWND,其中只有一个是由GetDlgItem()返回的。 我相信如有必要,我可以完成这项工作,但我想知道是否有秘密可以拦截对话框中控件的 HWND 创建(实际上是 CPropertyPage)。

下面是不起作用的代码,如果可能的话,还有更多"有效"但不是我希望它工作方式的注释代码。

void CMyPropertySheet::DoDataExchange(CDataExchange* pDX)
{
HWND hWndCtrl;
pDX->m_pDlgWnd->GetDlgItem(IDC_LIST1, &hWndCtrl);
if (themed) {
DWORD style = GetWindowLongPtr(hWndCtrl, GWL_STYLE) | LBS_OWNERDRAWFIXED;
SetWindowLongPtr(hWndCtrl, GWL_STYLE, style);
DDX_Control(pDX, IDC_LIST1, m_listbox);
//RECT wr;
//::GetWindowRect(hWndCtrl, &wr);
//m_listbox.Create(style, wr, this, IDC_LIST1);
} else {
DDX_Control(pDX, IDC_LIST1, m_listbox);
}

我可能应该补充一点,我尝试对窗口进行子类化,但它没有帮助,而且CMyPropertySheet::PreSubclassWindow也不够快。

某些创建标志(如LBS_OWNERDRAWFIXEDLBS_SORT)被缓存,之后修改它们不起作用。您必须更改模板,或者只是复制列表框。复制旧列表框的样式,然后隐藏该列表框,更改其 ID,并基于旧列表框创建新列表框。然后,您必须删除DDX_Control(pDX, IDC_LIST1, m_listbox)

下面的示例从设置了排序标志的标准列表开始。它会复制列表框并禁用排序选项。

为简单起见,此示例避免了LBS_OWNERDRAWFIXED,而是使用LBS_SORT

class CMyPropertyPage : public CPropertyPage
{
public:
CListBox m_listbox;
int m_listbox_index;
CMyPropertyPage(int idd) : CPropertyPage(idd)
{
m_listbox_index = 1;
}   
void DoDataExchange(CDataExchange* pDX)
{
//This function is automatically called before 
//CPropertyPage::OnInitDialog is complete
//On the first call, IDC_LIST1 will point to the template listbox
if(m_listbox.m_hWnd)
{
//m_listbox is ready, 
//IDC_LIST1 will refer to the new listbox
DDX_LBIndex(pDX, IDC_LIST1, m_listbox_index);
}
}
BOOL OnInitDialog()
{
CPropertyPage::OnInitDialog();
CListBox* old_listbox = (CListBox*)GetDlgItem(IDC_LIST1);
if(old_listbox)
{
DWORD style = ~LBS_SORT & GetWindowLongPtr(old_listbox->m_hWnd, GWL_STYLE);
CRect rc;
old_listbox->GetWindowRect(&rc);
ScreenToClient(&rc);
old_listbox->SetDlgCtrlID(0);//change the old ID to something unused
old_listbox->ShowWindow(SW_HIDE); //hide the old listbox
m_listbox.Create(style | WS_BORDER, rc, this, IDC_LIST1);
m_listbox.SetFont(GetFont());
}
ASSERT(m_listbox.GetDlgCtrlID() == IDC_LIST1);
m_listbox.AddString(L"2");
m_listbox.AddString(L"1");
m_listbox.AddString(L"0");
UpdateData(FALSE);
return TRUE;
}
};
class CMyWinApp : public CWinApp
{
BOOL InitInstance()
{
CWinApp::InitInstance();
CPropertySheet sh;
CMyPropertyPage page(IDD_PAGE1);
sh.AddPage(&page);
sh.DoModal();
return TRUE;
}
} myapp;

好的,我不确定我向任何人推荐这个,但我终于找到了修改模板的方法。 我不得不使用VirtualProtect来解锁模板的内存。

for (int i = 0; i < m_pages.GetSize(); i++) {
CPropertyPage* pPage = GetPage(i);
PROPSHEETPAGE* tpsp = &pPage->m_psp;
const DLGTEMPLATE* pTemplate;
if (tpsp->dwFlags & PSP_DLGINDIRECT) {
pTemplate = tpsp->pResource;
} else {
HRSRC hResource = ::FindResource(tpsp->hInstance, tpsp->pszTemplate, RT_DIALOG);
if (hResource == NULL) return false;
HGLOBAL hTemplate = LoadResource(tpsp->hInstance, hResource);
if (hTemplate == NULL) return false;
pTemplate = (LPCDLGTEMPLATE)LockResource(hTemplate);
if (pTemplate == NULL) return false;
}
if (afxOccManager != NULL) {
DLGITEMTEMPLATE *pItem = _AfxFindFirstDlgItem(pTemplate);
DLGITEMTEMPLATE *pNextItem;
BOOL bDialogEx = IsDialogEx(pTemplate);
int iItem, iItems = DlgTemplateItemCount(pTemplate);
for (iItem = 0; iItem < iItems; iItem++) {
pNextItem = _AfxFindNextDlgItem(pItem, bDialogEx);
DWORD dwOldProtect, tp;
if (bDialogEx) {
_DialogSplitHelper::DLGITEMTEMPLATEEX *pItemEx = (_DialogSplitHelper::DLGITEMTEMPLATEEX *)pItem;
if (pItemEx->id == IDC_LIST1) {
if (VirtualProtect(&pItemEx->style, sizeof(pItemEx->style), PAGE_READWRITE, &dwOldProtect)) {
pItemEx->style |= LBS_OWNERDRAWFIXED;
VirtualProtect(&pItemEx->style, sizeof(pItemEx->style), dwOldProtect, &tp);
}
}
} else {
if (pItem->id == IDC_LIST1) {
if (VirtualProtect(&pItem->style, sizeof(pItem->style), PAGE_READWRITE, &dwOldProtect)) {
pItem->style |= LBS_OWNERDRAWFIXED;
VirtualProtect(&pItem->style, sizeof(pItem->style), dwOldProtect, &tp);
}
}
}
pItem = pNextItem;
}
}
}
return true;