Search code examples
c++capilinesquare

How to set square edged line api windows


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


Solution

  • 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);