如何在调用 DDX_Control 之前更改使用 DDX_Control 初始化的控件 (CListBox) 的样式
How can I change the style of a control (CListBox) initialized with DDX_Control, before DDX_Control is called
我正在修改一个现有项目,并且对话框包含控件,在某些情况下,我将以不同的方式对主题进行子类化(在其他情况下,我将完全不理会它)。 当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_OWNERDRAWFIXED
和LBS_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;
- 方法错误"not all control paths return a value"和方法不返回值
- Spin Control GetPos()值被延迟
- 警告:"Not all control paths return a value" c++
- 抑制非平凡无用的警告"control may reach end of non-void function"
- 我什么时候不希望在Visual Studio中启用“Control Flow Guard Microsoft
- Vim 如何"take control"终端并允许将其用作可修改的缓冲区?
- "Control reaches end on non-void function" with do { return result; } while(condition);
- 我在C++年写了一个findClosestPrime(),但Xcode抱怨"Control may reach end of non-void function"
- 收到此警告" Control may reach end of non-void function "
- MFC Rich Edit Control 2.0接收到单击事件
- 即使我已经涵盖了所有情况,也会收到"control reaches end of non-void function"警告
- 布尔函数"Control reaches end of non-void function"错误
- CUDA : 错误: 创建推力时"transfer of control bypasses initialization of"::d evice_ptr
- 更改Internet Explorer安全设置(initaize和Script ActiveX Control in n
- 困惑为什么"control may reach end of non-void function"
- 当调用带有警告"control reaches end of non-void function"的函数时,(实际上)会发生什么?
- "recursive on all control paths, function will cause runtime stack overflow"超载<<运算符
- DDX 数据绑定与 Windows 窗体找不到类方法
- "Control may reach end of non-void function"警告
- Clr dll .net control