65.9K
CodeProject 正在变化。 阅读更多。
Home

GDI+ 绘图 ActiveX 控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.89/5 (9投票s)

2009年7月19日

LGPL3

2分钟阅读

viewsIcon

97478

downloadIcon

12191

GDI+ 2D 绘图 ActiveX 控件

GitHub 

最新的源代码可以从 GitHub 检出 (https://github.com/xiangzhai/gdiplusplot)。

引言

这是一个简单的 OCX 控件,允许您绘制二维数据。 除了 Gigasoft Co., Ltd 开发的 ProEssentials 之外,没有现成的控件可以提供基于 GDI+ 的简单直接的 2D 数据可视化。 Nikolai Teofilov 的 NtGraph ActiveX 控件 (https://codeproject.org.cn/KB/miscctrl/ntgraph_activex.aspx) 启发我编写自己的控件,主要是因为我想在需要时自定义源代码。 随着时间的推移,ActiveX 控件的功能变得更加精细,最终我决定在 LGPL v3 许可下发布我手头上的东西。

它能做什么?

该控件能够绘制大量的点,并使用新数据更新图形上的一个或多个绘图,用新绘图替换旧绘图。 可以在运行时自定义具有颜色、可见性等单独属性的多个绘图。

如何使用控件 

要使用此 OCX 控件,请将其嵌入到支持使用 OCX 控件的应用程序中。 Microsoft Visual Basic 应用程序、Office 应用程序和使用 Microsoft Developer Studio 的 AppWizard 创建的应用程序都可以支持使用 OCX 控件。 使用此控件需要两个文件。 它们是

  • GDIPlusPlot.ocx——GDIPlusPlot 控件代码和数据
  • gdiplus.dll——Microsoft 的 GDI+ 动态链接库

在 ActiveX 控件可以在您的应用程序中使用之前,必须将其注册为系统注册表中的 COM 组件。 这是一个自注册控件。 这意味着要在系统注册表中注册该控件,您只需要让应用程序加载该控件并调用该控件的导出函数 DllRegisterServer。 您可以使用 REGSVR32 实用程序或让您的安装程序执行此操作。

如何使用 REGSVR32 实用程序?

GDIPlusPlot.ocx 复制到您的目录并键入
regsvr32 GDIPlusPlot.ocx
regsvr32 /u GDIPlusPlot.ocx (注销服务器)

自定义控件

您可以通过按照 ActiveX 控件的标准步骤将控件包含在您的项目中

  1. 使用从 CFormView 派生的 View 类创建 MFC 对话框项目或 MDI/SDI 项目
  2. 选择菜单 项目|添加到项目|组件和控件...
  3. 打开注册的 ActiveX 控件库
  4. 选择 GDIPlusPlot 控件并单击插入 
  5. Visual C++ 将生成类 CGDIPlusPlot
  6. 然后您可以将类型定义为 CGDIPlusPlot 的变量

Using the Code

HelloWorld 示例(这里 m_GDIPlusPlot1 是即时实时控件,m_GDIPlusPlot2static 控件)

BOOL CHelloWorldDlg::OnInitDialog()
{
    int RANGE_MIN = -10;
    int RANGE_MAX = 10;
    unsigned int i;

    ......
    	
    // TODO: Initial realtime GDIPlusPlot
    m_GDIPlusPlot1.SetXTime(TRUE);
    m_GDIPlusPlot1.SetCaption("RealTime");
    m_GDIPlusPlot1.AddElement(0);   // 0 stands for Gdiplus::Color Green ARGB
    m_GDIPlusPlot1.AddElement(2);   // 2 stands for Blue
    
    // NOTE: There are some APIs to set the X/Y axis label, but I decide to draw 
    // the full rcBounds size ActiveX control, so comment the drawing 
    // X/Y axis label routine

    // TODO: Initial static GDIPlusPlot
    m_GDIPlusPlot2.SetXTrack(TRUE); 	// Set X track mode a red cursor 
				// following the mouse cursor
    m_GDIPlusPlot2.AddElement(3);
    m_GDIPlusPlot2.AddElement(4);
    m_GDIPlusPlot2.AddElement(0);
    // TODO: Loading the static random datas into static GDIPlusPlot
    for (i = 0; i < 168; i++) 
    {
        int randv = (((double) rand() / 
                         (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
	m_GDIPlusPlot2.PlotXY(i, randv, 0);
	m_GDIPlusPlot2.PlotXY(i, randv / 1.6, 1);
	m_GDIPlusPlot2.PlotXY(i, sin(randv), 2);
    }

    // TODO: Set a timer for loading datas into realtime GDIPlusPlot
    SetTimer(1, 100, NULL);
    
    ......	
}

void CHelloWorldDlg::OnTimer(UINT nIDEvent) 
{
    int RANGE_MIN = -6;
    int RANGE_MAX = 6;
    int randv = (((double) rand() / 
                         (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);

    // TODO: Loading the on-the-fly data into realtime GDIPlusPlot
    m_GDIPlusPlot1.PlotY(randv, 0);
    m_GDIPlusPlot1.PlotY(randv * 3, 1);
	
    ......
}

GDIPlusPlot 属性

BSTR caption;
boolean xTime;
BSTR xLabel;
BSTR yLabel;
short interval;
BSTR annolabel;
boolean xTrack;

方法

void PlotXY(double xValue, double yValue, short index);
void SetRange(double xMin, double xMax, double yMin, double yMax);
void PlotY(double newValue, short index);
void ClearGraph();
void AddElement(short color);
void IsElementVisible(short index, boolean visible);
void SetXCursorPos(double xValue);
double GetQuadrantWidth();

兴趣点 

Nikolai Teofilov 基于 GDI 的 NtGraph ActiveX 控件启发我编写自己的控件,主要是因为我想在需要时自定义源代码,所以我玩得很开心 ^_^。 

© . All rights reserved.