【问题标题】:C++ can't display ActiveX control in the dialog CFormView, control variable is NULLC++无法在对话框CFormView中显示ActiveX控件,控件变量为NULL
【发布时间】:2018-01-01 11:30:05
【问题描述】:

我在我的项目(Visual Studio 2015、MFC、C++)中使用了SmartGraph ActiveX 控件。它已成功注册。 我尝试将带有此控件的对话框放入 CFormView

MyAppView.h:

#pragma once
#include "SmartGraph.h"
#include "afxwin.h"

class CMyAppView : public CFormView
{
protected: // create from serialization only
    CMyAppView();
    DECLARE_DYNCREATE(CMyAppView)

    enum { IDD = IDD_DIALOG1 };

    CSmartGraph m_Graph; //!!!!! ActiveX control variable
    CButton m_ctrlOK;
....
}

MyAppView.cpp:

....

void CMyAppView::DoDataExchange(CDataExchange* pDX)
{
    CFormView::DoDataExchange(pDX);
    DDX_Control(pDX, IDOK, m_ctrlOK);
    DDX_Control(pDX, IDC_SMARTGRAPH1, m_Graph);
}

void CMyAppView::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();
    ResizeParentToFit();

    m_Graph.SetParentWnd(this->m_hWnd);
    m_Graph.SetPlotType(0);
    m_Graph.put_xLable(_T("Time"));
    m_Graph.put_yLable(_T("Amplitude"));
    m_Graph.put_Title(_T("Graph Test"));
}
...

因此 m_Graph 为 NULL 并且 SmartGraph 不会显示在对话框中。同时 OK 按钮变量不是 NULL 并且正确显示。 我做错了什么?

【问题讨论】:

    标签: c++ null mfc activex cview


    【解决方案1】:

    您需要创建此对象的一个​​实例。 m_Graph.CreateControl(...);

    【讨论】:

    • 在“SmartGraph.h”中有两种方法返回 CreateControl(),但在作者的工作演示项目中,他根本没有使用这种方法。
    • 我已经尝试过这种方法 - 在 occsite.cpp Line 212 中调试断言失败
    • 如果控件是在模板中隐式创建的,那么工作演示可能会使用对话框模板...无论如何您需要创建对象!
    • @xMRi:据我记忆,m_Graph 的控件应该是由 DDX_Control 创建的。对 Nika_Rika:在 DDX_Control 上下一个断点,单步执行,看看是否创建成功(即 m_Graph.m_hWnd 不为 NULL),如果没有,尝试在那里确定原因。
    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多