如何设置方形边线 api windows

How to set square edged line api windows

我在 windows(不是 gdi)上使用 api,我想知道如何制作方边线。

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

我当前的行输出:

我想要这条线:

谢谢,有代码

您可以修改用于绘制线条的笔样式,特别是 PS_ENDCAP_SQUARE 和 select 将笔插入设备上下文,请阅读 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);

太棒了!谢谢 ! 请参阅以下代码:

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