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

标签 c++ mfc

我通过 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/

相关文章:

C++通过指针算法获取父类

c++ - 考虑leap年,向COledateTime添加年份的正确方法是什么?

c++ - MFC - 控件不是直接创建的

c++ - IE BHO - 为页面加载调用 DISPID_FILEDOWNLOAD?

c++ - 使用 QtCreator 构建纯 C++

c++ - 为什么要释放DC()?

c++ - MFC 消息队列限制

c++ - 主框架中的 MFC GUI 和其他也需要 GUI 的 DLL

mfc 删除默认工具栏

c# - 如何将 Paypal 与 WinForm 桌面应用程序集成