WinAPI - 如何绘制虚线?
我使用 WinAPI GDI 方法创建 HPEN:
HPEN hPen = CreatePen(PS_DOT, 1, color);
然后使用方法 MoveToEx
和 LineTo
绘制线条。
事实上,绘制的线是虚线。 3 个像素为空,3 个像素为颜色——虚线。
为什么PS_DOT样式不画虚线? 如何使用WinAPI绘制虚线?
I create HPEN using WinAPI GDI method:
HPEN hPen = CreatePen(PS_DOT, 1, color);
Then draw line using the methods MoveToEx
and LineTo
.
In fact drawn line is dashed. 3 pixels empty, 3 pixels with color -- dashed line.
Why PS_DOT style doesn't draw dotted line?
How to draw dotten line using WinAPI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我在 CodeProject 上找到的 MaxHacher 的精彩解决方案
(http://www.codeproject.com/KB/GDI/DOTTED_PEN.aspx)
LOGBRUSH LogBrush;
LogBrush.lbColor = 颜色;
LogBrush.lbStyle = PS_SOLID;
HPEN hPen = ExtCreatePen( PS_COSMETIC | PS_ALTERNATE, 1, &LogBrush, 0, NULL );
效果很好!
Here is wonderful solution by MaxHacher that I've found on CodeProject
(http://www.codeproject.com/KB/GDI/DOTTED_PEN.aspx)
LOGBRUSH LogBrush;
LogBrush.lbColor = color;
LogBrush.lbStyle = PS_SOLID;
HPEN hPen = ExtCreatePen( PS_COSMETIC | PS_ALTERNATE, 1, &LogBrush, 0, NULL );
It works well!
我过去也遇到过这个问题。 我求助于使用 LineDDA 和回调过程。
可能不是最有效的绘图算法,但您现在也可以完全控制点间距。 我采用这种方法是因为我使用其他非本地笔样式进行线条渲染,这些样式使用位模式。 然后我遍历该位并使用 setpixel 作为“on”位。 它运作良好并增加了有用的线条样式。
I too had this problem in the past. I resorted to using LineDDA and a callback proc.
Might not be the most efficient drawing algorithm, but you're now in complete control of dot spacing as well. I went with this approach because there were other non-native pen styles I was using for line rendering which used a bit pattern. I then walked the bit and used setpixel for the 'on' bits. It worked well and increased the useful linestyles.
我还没有尝试过这个,但可能值得检查
HPEN hPen = CreatePen(PS_DOT, 0, color);
的结果画笔宽度为零会导致 GDI 始终使画笔宽度为 1 个像素,无论与设备上下文相关的缩放比例如何。 这可能足以获得您想要的点。
I haven't tried this, but it might be worth checking the results from
HPEN hPen = CreatePen(PS_DOT, 0, color);
A pen width of zero causes GDI to always make the pen one pixel wide, regardless of the scaling associated with the device context. This may be enough to get the dots you want.
我用这个而不是上面的来避免连续两个像素
I used this instead of the above to avoid two pixels in a row