用于 Visual C++ 的简单面向对象 2D 图形框架?

发布于 2024-07-12 21:58:38 字数 408 浏览 9 评论 0原文

我们正在构建一种通过 Visual C++ 应用程序 (MFC) 中的 GUI 以可视方式连接组件的方法。 简单的事情,例如单击框并在连接的框之间绘制线条,并在其上存储信息。 问题是我们是在 GDI 中从头开始自己制作这一切,并且很快就会变得需要维护大量代码。

我们真的是在重新发明轮子吗?

我在网上查找了提供面向对象的 2D 框架的组件(矢量图形在这里很有趣)。 面向对象是指屏幕上的正方形是代码中的正方形对象,或者至少可以在图形对象上存储自定义信息。 它应该支持在代码中检索有关对象位置等的信息,以在对象之间绘制线条,并检测鼠标是否位于对象上方。

并不是真正复杂的事情,但是如果仅仅因为您自己编写了所有内容而需要维护数百或数千行,那么它就会变得相当复杂且难以维护,更不用说潜在的错误潜入,这在成熟的系统中是可以避免的框架。

We're building a method of connecting components visually via a GUI in a Visual C++ application (MFC). Simple things like clicking on boxes and drawing lines between those that are connected, and storing information on them. The problem is that we're making all this by ourselves from the ground up in GDI and it quickly becomes a heck of a lot of code to maintain.

Are we really reinventing the wheel here?

I've looked around on the web for components that provide an object-oriented 2D framework (vector graphics being interesting here). Object-oriented in the sense that a square on the screen is a square object in code, or at least that you can store custom information on the graphical object. It should support retrieving information on object positions etc in code to draw lines between objects, and detect whether the mouse is over an object or not.

Not really complicated things, but it becomes quite complicated and hard to maintain if there are hundreds or thousands of line to maintain just because you wrote all of it yourself, not to mention the potential for bugs creeping in, that would be avoided in a mature framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云淡风轻 2024-07-19 21:58:38

“绘制对象并将它们连接在一起”的描述听起来有点像 Fig (xfig / winfig / et al) 处理的东西。

另一种可能符合此目的的产品(尽管价格较高)是 Visio——Microsoft Office Visio SDK(请参阅 http://office.microsoft.com/en-us/visio/HA101656401033.aspx)据说相当丰富。

然而,我没有从程序员的角度看过Fig或Visio,所以我不知道底层代码是什么样的,或者它是否适合您的目的......但我认为它们都是灵感的良好起点。 也许还有另一个图表/图形库可能适合您的目的。

顺便说一句,当我在这个问题上闲逛(呃,谷歌搜索)时,我偶然发现了 AGD - 自动图形绘制 http://www.ads.tuwien.ac.at/research/graphDrawing.html。 同样,不确定它是否适合您的特定情况,但它似乎很有趣,可以指出这一点。

祝你好运!

The description of "drawing objects and connect them together", sounds vaguely like something that Fig (xfig / winfig / et al) handles.

Another product that might fit the purpose (albeit at a price) is Visio -- the Microsoft Office Visio SDK (see http://office.microsoft.com/en-us/visio/HA101656401033.aspx) is supposedly quite rich.

I have not looked either fig or Visio from a programmer's standpoint, however, so I have no idea what the underlying code looks like, or whether it's suitable for your purpose... But I think they both are good starting points for inspiration. Perhaps there is another graph/figure library out there that might suit your purpose.

BTW, while noodling (er, Googling) around on this, I stumbled across AGD - automatic graph drawing at http://www.ads.tuwien.ac.at/research/graphDrawing.html. Again, not sure how suitable it is for your particular situation, but it seemed interesting enough to point it out.

Good luck!

狼亦尘 2024-07-19 21:58:38

也许有些过头了,但请查看这个。 还有这个

Maybe overkill but check out this. And also this

计㈡愣 2024-07-19 21:58:38

作为一个看起来很有用的 SVG 库(感谢 Malkocoglu 的想法!),我找到了这个:libboard。 代码示例的简单性看起来棒极了; 我唯一剩下的问题是让用户与其生成的 SVG 绘图进行交互。 :/ AFAIK,它甚至不包含渲染器,更不用说与其绘图交互的方法了。 我不确定我是否愿意自己开发一个 SVG 解析器来控制这些需求。 但以编程方式构建绘图的代码简单性看起来正是我所追求的。 唔。

As an SVG library that looks useful (thanks Malkocoglu for the idea!), I found this one: libboard. The simplicity in the code samples look awesome; my only remaining issue would then be in having the user interact with its generated SVG drawings. :/ AFAIK, it doesn't even include a renderer, much less a method to interact with its drawing. I'm not sure I'm willing to develop an SVG parser myself to control these needs. But the code simplicity to programmatically build the drawings look like just what I'm after. Hmm.

尴尬癌患者 2024-07-19 21:58:38

我确信您会选择 OO 包,但不要指望奇迹。
原因如下。

我假设您从一些应用程序数据开始,一组应用程序对象,我们将它们称为对象 A。

您可以使用 OO 图形对象包来表示对象 A 的图形视图,将这组新对象称为 G。

现在您有两组对象,A 和 G,其中任何一组都可以动态更改,并且您面临着保持它们正确对应的问题。
不仅要从A生成G,还要在A改变时修改G,
当G改变时修改A。
这需要大量事件驱动的链接代码,并且您永远无法确定是否正确处理了每种情况。
您很容易陷入所见并非所见的情况。
(WYSINWYG)

我有两个建议:

  • 你正在做什么

有一个直接渲染对象 A 的“paint”例程(如果你想避免闪烁,请使用“blt”)。 将简单的图形信息附加到对象 A,例如屏幕位置和大小。 自己处理鼠标事件,以突出显示、拖动、创建连线等。这可能看起来很麻烦,但它避免了您因冗余对象集而遇到的所有链接麻烦。 而且,您可以完全控制代码。

这是管理冗余集的通用技术的物体。 然而,它有一个艰难的学习曲线。 大多数程序员都无法做到这一点,但它确实减少了代码并保证了正确性。

I'm sure you will go with an OO package, but don't expect miracles.
Here's why.

I assume you start out with some application data, a set of application objects, let's call them objects A.

You could use a package of OO graphical objects to represent the graphical view of objects A, call this new set of objects G.

Now you have two sets of objects, A and G, either one of which could change dynamically, and you are faced with the problem of keeping them in correct correspondence.
Not only do you have to generate G from A, you have to modify G when A changes,
and modify A when G changes.
This calls for lots of event-driven linkage code, and you can never be sure you've handled every case correctly.
You can easily get into situations where what you see is not what you get.
(WYSINWYG)

I have two suggestions:

  • What you're doing

Have a "paint" routine that directly renders objects A (using "blt" if you want to avoid flashing). Attach simple graphical information to objects A, like screen position and size. Handle mouse events yourself, for highlighting, dragging, creating wires, etc. This may seem like a lot of trouble, but it avoids all the linkage trouble you get into with redundant sets of objects. And, you have complete control of the code.

This is a general technique for managing redundant sets of objects. However, it has a tough learning curve. Most programmers are not up to it, but it does reduce code and guarantee correctness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文