您的位置:首页 > 编程语言 > C#

在C#中使用WinGraphviz,一个COM图论组件,兼谈COM接口IStream在.NET下的互操作

2017-03-11 15:35 141 查看
Graphviz是AT&T实验室的一个项目,提供了通过DOT语言(一种自定义的语言)绘出图论中有向图的工具,当然Graphviz也完全可以用于无向图的绘制。 在图(这里作为数据结构)的可视化方面,Graphviz是非常理想的工具。

WinGraphviz基于Graphviz项目,在Win32平台上,将Graphviz封装为一个COM对象,从而可以在Windows下方便的使用。

WinGraphviz是COM组件,在它的网页上没有提供C#的示例,所以我这里就尝试着写了一个:WinGraphVizDemo.,如果对这个组件感兴趣的,请到WinGraphviz主页下载WinGraphviz_v1.02.25s.cab,按照上面提示方法安装后,即可看到结果。

Install (WinGraphviz_v1.xx.x.msi)

Download Binary File:"WinGraphviz_v1.xx.x.msi" on your local file system.

Run "WinGraphviz_v1.xx.x.msi"

Complete
Install (WinGraphviz_v1.xx.x.cab)

Download Binary File:"WinGraphviz_v1.xx.x.cab" on your local file system.

If you install previous version WinFraphviz ,uninstall and delete it on your system

Unzip the file to the system dicroty "\[Windows]\SYSTEM32"

Open a Command Window

if your windows is Win98/me then run "regserver.exe WinGraphviz.dll" to regist it on your system

if your windows is WinNT/2000/XP then run "regsvr32.exe WinGraphviz.dll" to regist it on your system

Complete
Uninstall (WinGraphviz_v1.xx.x.msi)

Uninstall from control panel

Complete
Uninstall (WinGraphviz_v1.xx.x.cab)

Open a Command Window

Goto the system dicroty "\[Windows]\SYSTEM32"

if your windows is Win98/me then run "regserver.exe WinGraphviz.dll /u" to unregist it on your system

if your windows is WinNT/2000/XP then run "regsvr32.exe WinGraphviz.dll /u" to unregist it on your system

Delete WinGraphviz.dll

Complete

程序讲解到此结束,以下为程序缺憾,请熟悉COM和.NET互操作的网友赐教:

在学习过程中,发现虽然WinGraphviz提供了IBinaryImage接口,其中的Dump方法支持一个拥有IStream接口的COM对象的写入,但是在.NET平台上,我却没有发现能够支持,COM的IStream的.NET类,.NET下的PictureBox也不支持COM的stdole.IPictureDisp图像文件接口。所以实现采用了最笨的存到C:\Temp.emf,然后再读出来的办法,肯定有更好的办法,但是暂时我想不出……

(一天后添加)没想到博客园高手如云,哈哈,gray给出的评论给了我相当的指导,讲老实话,直到现在我对ComStream的工作原理还是不甚了了,但无论如何,现在总算完全解决了IStream问题,不用临时文件过渡了。使用ComStream的关键代码如下:

WINGRAPHVIZLib.DOT my_Dot = new WINGRAPHVIZLib.DOT();
if(my_Dot.Validate(textBox.Text)==true)
{
WINGRAPHVIZLib.BinaryImage bi=my_Dot.ToEMF(textBox.Text);
UCOMIStream iStream = null;
ComStream.CreateStreamOnHGlobal(0, false, out iStream);
bi.Dump((WINGRAPHVIZLib.IStream) iStream);
ComStream comStream = new ComStream(ref iStream);

// comStream must reset to position 0 if use Metafile, But needn't if use Bitmap, Why?
comStream.Position = 0;

Metafile mf = new Metafile(comStream);
pictureBox.Height = mf.Height;
pictureBox.Width = mf.Width;
this.Height = mf.Height;
this.Width = mf.Width;
pictureBox.Image = mf;
}
具体例子见本人的资源下载:http://download.csdn.net/detail/qinzhenhua100/9777830
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: