首页 > 解决方案 > 在 MFC 中显示 .NET 图表控件

问题描述

我想在 MFC 对话框中显示一个 .NET 图表控件。我通过 CWinFormsControl 实例使用了托管控件。在 .NET 方面,我创建了一个带有 TextBox、Button 和 Chart 控件的 UserControl。

不幸的是,我无法设置图表控件的属性。尽管 Chart 控件本身已被识别,但它的任何属性或方法都无法识别/访问。

在表单/.NET 中:

namespace HostableUserControl
{
partial class UserControl1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
        System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
        this.button1 = new System.Windows.Forms.Button();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
        ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(39, 25);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(140, 163);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(98, 20);
        this.textBox1.TabIndex = 1;
        this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
        // 
        // chart1
        // 
        this.chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.FrameThin1;
        chartArea1.Name = "ChartArea1";
        this.chart1.ChartAreas.Add(chartArea1);
        this.chart1.Location = new System.Drawing.Point(140, 23);
        this.chart1.Name = "chart1";
        series1.ChartArea = "ChartArea1";
        series1.Name = "Series1";
        this.chart1.Series.Add(series1);
        this.chart1.Size = new System.Drawing.Size(251, 134);
        this.chart1.TabIndex = 2;
        this.chart1.Text = "chart1";
        // 
        // UserControl1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.Controls.Add(this.chart1);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.button1);
        this.Name = "UserControl1";
        this.Size = new System.Drawing.Size(394, 200);
        ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    public System.Windows.Forms.Button button1;
    public System.Windows.Forms.TextBox textBox1;
    public System.Windows.Forms.DataVisualization.Charting.Chart chart1;
}

}

//在 MFC(片段)中:

CWinFormsControl<HostableUserControl::UserControl1> m_ctrl1;

{
    m_ctrl1.GetControl()->textBox1->Text = "hello world";  // this works
    m_ctrl1.GetControl()->chart1->Text = "hello world";    // this generates compiler error
}

编译器错误(VS2017):

错误 C2039:“文本”:不是 System::Windows::Forms::DataVisualization::Charting::Chart 的成员

为 Chart 的所有公共成员生成此错误。

有什么建议么?

标签: c++.netwinformschartsmfc

解决方案


推荐阅读