问题描述
Win32 GDI DrawFocusRect(HDC,const RECT *)
函数在所需devince上下文上绘制矩形的虚线轮廓.关于此函数的最酷的事情是它使用XOR函数绘制点,这样当您在同一设备上下文和矩形上再次调用它时,它将擦除自身:
The Win32 GDI DrawFocusRect(HDC, const RECT*)
function draws the dotted outline of a rectangle on the desired devince context. The cool thing about this function is it draws the dots using an XOR function so that when you call it a second time on the same device context and rectangle, it erases itself:
RECT rc = { 0, 0, 100, 100 };
DrawFocusRect(hdc, &rc); // draw rectangle
DrawFocusRect(hdc, &rc); // erase the rectangle we just drew
我想获得与 DrawFocusRect()
相同的虚线效果,但是我只想要一条线,而不是整个矩形.我尝试通过将高度为1的 RECT
传递给 DrawFocusRect()
来执行此操作,但这不起作用,因为它会在矩形顶部对矩形的底线"进行XOR顶线,所以什么都没画.
I want to achieve the same dotted line effect as DrawFocusRect()
but I just want a line, not a whole rectangle. I tried doing this by passing a RECT
of height 1 to DrawFocusRect()
but this doesn't work because it XORs the "bottom line" of the rectange on top of the top line so nothing gets painted.
我可以创建一个普通的HPEN来实现与 DrawFocusRect()
相同的效果,以便仅画一条线吗?
Can I create a plain HPEN that achieves the same effect as DrawFocusRect()
so I can draw just a single line?
推荐答案
正如@IInspectable所评论,您要使用 SetROP2()
.战斗的另一半是创造正确的笔.这是整个过程的震撼:
As @IInspectable commented, you want to use SetROP2()
. The other half of the battle is creating the correct pen. Here is how the whole thing shakes out:
HPEN create_focus_pen()
{
LONG width(1);
SystemParametersInfo(SPI_GETFOCUSBORDERHEIGHT, 0, &width, 0);
LOGBRUSH lb = { }; // initialize to zero
lb.lbColor = 0xffffff; // white
lb.lbStyle = BS_SOLID;
return ExtCreatePen(PS.GEOMETRIC | PS.DOT, width, &lb, 0, 0);
}
void draw_focus_line(HDC hdc, HPEN hpen, POINT from, POINT to)
{
HPEN old_pen = SelectObject(hdc, hpen);
int old_rop = SetROP2(R2_XORPEN);
MoveToEx(hdc, from.x, from.y, nullptr);
LineTo(hdc, to.x, to.y);
SelectObject(hdc, old_pen);
SetROP2(old_rop);
}
这篇关于是否可以创建诸如DrawFocusRect()的XOR笔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!