gpt4 book ai didi

c++ - 无法获取可停靠 Pane C++ 的句柄

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:58 24 4
gpt4 key购买 nike

我通过 MFC 向导创建了一个基于 MFC 应用程序的多文档功能区。我试图获取 m_wndFileView 的句柄以更新其 View 。我知道有几种方法可以做到这一点,但我不明白为什么我使用的方法不起作用。开始吧

class CMainFrame : public CMDIFrameWndEx
{
...
CFileView m_wndFileView;
CPropertiesWnd m_wndProperties;
...
}

class CFileView : public CDockablePane
{
...
protected:
CViewTree m_wndFileView;
...
};

class CPropertiesWnd : public CDockablePane
{
...
protected:
CMFCPropertyGridCtrl m_wndPropList;
...
};

主框架是从 MAINAPPLICATION.cpp 创建的

// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
{
delete pMainFrame;
return FALSE;
}
m_pMainWnd = pMainFrame;

// call DragAcceptFiles only if there's a suffix
// In an MDI app, this should occur immediately after setting m_pMainWnd
// Enable drag/drop open
m_pMainWnd->DragAcceptFiles();

// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

// Enable DDE Execute open
EnableShellOpen();
RegisterShellFileTypes(TRUE);


// Dispatch commands specified on the command line. Will return FALSE if
// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The main window has been initialized, so show and update it
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();

MainFrm.cpp 创建这两个 Pane :

// Create file view
CString strFileView;
bNameValid = strFileView.LoadString(IDS_FILE_VIEW);
ASSERT(bNameValid);
if (!m_wndFileView.Create(strFileView, this, CRect(0, 0, 200, 200), TRUE, ID_VIEW_FILEVIEW, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT| CBRS_FLOAT_MULTI))
{
TRACE0("Failed to create File View window\n");
return FALSE; // failed to create
}


// Create properties window
CString strPropertiesWnd;
bNameValid = strPropertiesWnd.LoadString(IDS_PROPERTIES_WND);
ASSERT(bNameValid);
if (!m_wndProperties.Create(strPropertiesWnd, this, CRect(0, 0, 200, 200), TRUE, ID_VIEW_PROPERTIESWND, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_RIGHT | CBRS_FLOAT_MULTI))
{
TRACE0("Failed to create Properties window\n");
return FALSE; // failed to create
}

在 MAINAPPLICATION.cpp 中,我可以通过以下方式访问属性面板

CWnd * pwnd = ((CWnd*)(AfxGetApp()->m_pMainWnd));
CPropertiesWnd * pPropertiesWnd = (CPropertiesWnd*)pwnd->GetDlgItem(ID_VIEW_PROPERTIESWND);
CMFCPropertyGridCtrl * m_wndPropList = (CMFCPropertyGridCtrl *)pPropertiesWnd->GetDlgItem(2);

但由于某些原因我无法访问文件 View Pane

CWnd * pwnd = ((CWnd*)(AfxGetApp()->m_pMainWnd));
CFileView * pFileViewWnd = (CFileView*)pwnd->GetDlgItem(ID_VIEW_FILEVIEW);
CViewTree * m_wndFileView= (CViewTree*)pFileViewWnd ->GetDlgItem(4);

(CFileView*)pwnd->GetDlgItem(ID_VIEW_FILEVIEW);返回 NULL

请帮忙。这真让我抓狂。最后我可以修改 m_wndPropList 但不能修改 m_wndFileView,因为我无法获得 pFileViewWnd 的句柄。不能以相同的方式访问以相同方式创建的两个 Pane 。为什么?如果需要更多代码,请告诉我。谢谢。

最佳答案

您应该像这样在 CMainFrame 类中创建一个内联 getter:

CFileView& GetFileViewPane()
{
return m_wndFileView;
}

CPropertiesWnd& GetPropsPane()
{
return m_wndProperties;
}

之后你可以像这样访问那些窗口:

CMainFrame* pMainFrame = DYNAMIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
if (pMainFrame && pMainFrame->GetSafeHwnd()) // sanity check
{
pMainFrame->GetFileViewPane().DoStuff();
}

关于c++ - 无法获取可停靠 Pane C++ 的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29597174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com