使用自定义笔样式在 win32/GDI 中绘制线条?

发布于 2024-08-16 14:42:34 字数 509 浏览 6 评论 0原文

我需要使用 win32/GDI(本机,而不是 .NET)进行一些绘图,并且遇到了以下问题:

我需要绘制“样式化”的线条。例如,在附图中,就我的应用程序数据而言,标记为“A”的线是一条直线,只需使用附加的锯齿形作为样式来绘制即可。当然,这很容易通过编程来完成,但当线可以呈任何角度(“B”)甚至是贝塞尔曲线(“C”)时,事情就会变得更加复杂。

现在,我可以通过编程来完成这一切,煞费苦心地进行数学运算,在每条线的可能性周围放置一个锯齿形,但这将花费大量时间,更重要的是,很容易出错。

是否可以只给 windows/GDI 一种“样式”来应用于线条,也许是像标记为“D”的位图,并让它使用它作为笔来绘制线条?如果没有,是否有比为每条“样式”线编写一堆特定绘图代码更灵活且更不易出错的方法?

*显然初学者无法发布图像。示例可以在 https://i.sstatic.net/oMFbc.png 中找到

I have a need to do some drawing using win32/GDI (Native, not .NET), and I've run into the following problem:

I need to draw lines that are "styled." For example, in the attached image, the line marked "A" is a straight line as far as my application data is concerned, it just needs to be drawn with the additional zigzag as a style. Of course, this is easy to do programatically, but it gets more complicated when the line can be at any angle ("B") or even a bezier curve ("C").

Now, I could do this all programatically, painstakingly doing the math to put a zigzag around each line possibility, but that will take a lot of time and, more importantly, be rather error prone.

Is it possible to just give windows/GDI a "style" to apply to the line, perhaps a bitmap like the one marked "D", and have it use that as a pen to draw the lines? If not, is there a more flexible and less error-prone way of doing this than writing a bunch of specific drawing code for each of the "styled" lines?

*Apparently first timers can't post images. Examples can be found at https://i.sstatic.net/oMFbc.png

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

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

发布评论

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

评论(4

长伴 2024-08-23 14:42:34

这在 Win32 GDI 中是不可能的。您需要自己进行数学计算。

但应该注意的是,您可以获得用于构成直线或曲线的点,这应该会使其变得更加容易。

有关示例,请参阅此“命中测试”教程。

对于贝塞尔曲线,您可以使用 Path函数

对于直线,您可以使用:

LineDDA

This is not possible in Win32 GDI. You will need to do the math yourself.

It should be noted however, that you can obtain the points used to make up the line or curve which should make it substantially easier.

See this "Hit-Testing" tutorial for an example.

For a bezier curve you would use Path Functions:

For straight lines you could use:

LineDDA

季末如歌 2024-08-23 14:42:34

据我所知,GDI 甚至 GDI+ 中没有任何内容可以支持这一点。您拥有的唯一线条选项是破折号图案、复合笔、破折号帽、端帽和填充画笔。

可能可以欺骗其中一个函数来绘制一些与直线样条线的摆动类似的东西,但它绝对不适用于曲线样条线。

然而,做到这一点应该不会太难。当然,这需要一天左右的时间,但您所要做的就是编写一条直线和贝塞尔插值器,将曲线分成相等的段,找到所有这些段的切线,然后左右交替。您最终将得到一个点数组,可以将其快速绘制为折线。

As far as I know there's nothing in GDI or even GDI+ that would support this. The only line options you have are dash-patterns, compound-pens, dash caps, end caps, and fill brushes.

You might be able to trick one of those functions into drawing something vaguely akin to your wiggles for straight splines, but it definitely won't work for curved splines.

It shouldn't be too hard to do this however. Sure, it will take a day or so, but all you have to do is write a line and bezier interpolator, divide the curves into equal segments, find the tangents at all those segments and alternate left and right. You'll end up with an array of points which can be drawn very quickly as a polyline.

梨涡少年 2024-08-23 14:42:34

没有什么可以自动完成此操作。您必须编写一些代码。您可能需要查看 LineDDA GDI 中的 API。它可能会简化您的代码所需的数学运算。

There's nothing that'll do this automatically. You'll have to write some code. You might want to look at the LineDDA API in GDI. It might simplify the math your code will need.

云淡月浅 2024-08-23 14:42:34

ExtCreatePen(),也许?我不知道它是否支持之字形...

ExtCreatePen(), maybe? I don't know for a fact if it supports zigzagging...

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