windows WinAPI - 如何绘制虚线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/541166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WinAPI - how to draw dotted line?
提问by user22814
I create HPEN using WinAPI GDI method:
我使用 WinAPI GDI 方法创建 HPEN:
HPEN hPen = CreatePen(PS_DOT, 1, color);
HPEN hPen = CreatePen(PS_DOT, 1, color);
Then draw line using the methods MoveToEx
and LineTo
.
然后使用方法MoveToEx
和画线LineTo
。
In fact drawn line is dashed. 3 pixels empty, 3 pixels with color -- dashed line.
实际上绘制的线是虚线。3 个像素为空,3 个像素有颜色——虚线。
Why PS_DOT style doesn't draw dotted line? How to draw dotten line using WinAPI?
为什么 PS_DOT 样式不绘制虚线?如何使用WinAPI绘制虚线?
回答by user22814
Here is wonderful solution by MaxHacher that I've found on CodeProject
(http://www.codeproject.com/KB/GDI/DOTTED_PEN.aspx)
这是我在 CodeProject
( http://www.codeproject.com/KB/GDI/DOTTED_PEN.aspx)上找到的 MaxHacher 的绝妙解决方案
LOGBRUSH LogBrush;
LogBrush.lbColor = color;
LogBrush.lbStyle = PS_SOLID;
HPEN hPen = ExtCreatePen( PS_COSMETIC | PS_ALTERNATE, 1, &LogBrush, 0, NULL );
LOGBRUSH LogBrush;
LogBrush.lbColor = color;
LogBrush.lbStyle = PS_SOLID;
HPEN hPen = ExtCreatePen( PS_COSMETIC | PS_ALTERNATE, 1, &LogBrush, 0, NULL );
It works well!
它运作良好!
回答by SAMills
I too had this problem in the past. I resorted to using LineDDA and a callback proc.
我过去也遇到过这个问题。我求助于使用 LineDDA 和回调过程。
struct LineData{
CDC* pDC;
COLORREF crForegroundColor;
COLORREF crBackgroundColor;
};
.
.
.
LineData* pData = new LineData;
pData->crForegroundColor = crForegroundColor;
pData->crBackgroundColor = crBackgroundColor;
pData->pDC = pdc;
LineDDA(nStartx, nStarty, nEndPointX, nEndPointY, LineDDAProc, (LPARAM) pData);
delete pData;
.
.
.
void
LineDDAProc(int x, int y, LPARAM lpData)
{
static short nTemp = 0;
LineData* pData = (LineData*) lpData;
if (nTemp == 1)
pData->pDC->SetPixel(x, y, pData->crForegroundColor);
else
pData->pDC->SetPixel(x, y, pData->crBackgroundColor);
nTemp = (nTemp + 1) % 2;
}
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.
可能不是最有效的绘图算法,但您现在也可以完全控制点间距。我采用这种方法是因为还有其他非本地钢笔样式用于使用位模式的线条渲染。然后我走了那个位并使用 setpixel 作为“on”位。它运行良好并增加了有用的线条样式。
回答by DavidK
I haven't tried this, but it might be worth checking the results from
我没有试过这个,但可能值得检查一下结果
HPEN hPen = CreatePen(PS_DOT, 0, color);
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.
笔宽度为零会导致 GDI 始终使笔宽度为一个像素,而不管与设备上下文相关联的缩放比例如何。这可能足以获得您想要的点。
回答by Kalles Fraktaler
I used this instead of the above to avoid two pixels in a row
我用这个代替上面的来避免连续两个像素
void LineDDAProc(int x, int y, LPARAM lpData)
{
LineData* pData = (LineData*) lpData;
if (x%2!=y%2)
pData->pDC->SetPixel(x, y, pData->crForegroundColor);
}