-1

I'm using api on windows (not gdi), and i would like to know how to do square edged line.

MoveToEx(hdc, x1, y1, NULL);
LineTo(hdc, x2, y2);

My current line output :

Current output

I want this line :

Desired output

thanks, have code

Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24

2 Answers2

2

You can modify the pen style used to draw your line, specifically PS_ENDCAP_SQUARE and select that pen into the device context, read the documentation for CPen:

 LOGBRUSH logBrush;//you need to use LOGBRUSH structure to specifiy brush attributes of the pen when the pen has PS_GEOMETRIC style
 logBrush.lbStyle = BS_SOLID;
 logBrush.lbColor = RGB(255,0,0);
 CPen pen( PS_GEOMETRIC |  PS_ENDCAP_SQUARE,10,&logBrush);//creates a pen with a square end caps and width of 10 pixels

 SelectObject(hdc,pen.GetSafeHandle());//select the above pen into the device context
 MoveToEx(hdc,x1,y1,NULL);
 LineTo(hdc,x2,y2);
Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24
-1

That work ! thanks ! See the following code :

         LOGBRUSH logBrush;
         logBrush.lbStyle = BS_SOLID;
         logBrush.lbColor = RGB(R,G,B);
         HPEN border = ExtCreatePen(PS_GEOMETRIC | PS_ENDCAP_SQUARE | PS_SOLID | PS_JOIN_MITER, size_, &logBrush, 0, nullptr);

         SelectObject(hdc, border);
         MoveToEx(hdc, x1, y1, nullptr);
         LineTo(hdc, x2, y2);

         DeleteObject(border);