WinAPI - how to draw a dashed line?

I create HPEN using the WinAPI GDI method:

HPEN hPen = CreatePen(PS_DOT, 1, color);

Then draw a line using the MoveToExand methods LineTo.

The actually drawn line is dotted. 3 pixels blank, 3 pixels with color - dashed line.

Why doesn't PS_DOT style draw a dashed line? How to draw a line of points using WinAPI?

+3
source share
3 answers

Here is the wonderful MaxHacher solution I found in 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!

+5
source

I also had this problem in the past. I resorted to using LineDDA and the callback process.

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;
}

, , . , , , -. setpixel 'on'. .

+1

I have not tried this, but it might be worth checking the results.

HPEN hPen = CreatePen(PS_DOT, 0, color);

A zero pen width causes GDI to always make a pen one pixel wide, regardless of the scale associated with the device context. This may be enough to get the right points.

0
source

Source: https://habr.com/ru/post/1703092/


All Articles