简体   繁体   English

如何通过用GDI对颜色进行异或来在填充的矩形上绘制文本?

[英]How to draw text over filled rectangle by XOR-ing its color with GDI?

I'm not sure if I'm expressing it right with words. 我不确定我是不是用语言表达正确。 The best way is to show it with an image. 最好的方法是将其与图像一起显示。 I'm trying to fill a rectangle while inverting the text color in it. 我正在尝试在反转文本颜色的同时填充矩形。 Just like an edit control does: 就像edit控件一样:

在此处输入图片说明

So here's a drawing code (called from WM_PAINT ): 所以这是一个绘图代码(从WM_PAINT调用):

//Erase background
::FillRect(hDC, &rcClient, ::GetSysColorBrush(COLOR_WINDOW));

//Draw text
::DrawText(hDC, text, text.size(), &rc, 
    DT_LEFT | DT_TOP | DT_EDITCONTROL | DT_NOPREFIX | DT_SINGLELINE);

//Draw highlighted rect & invert text
HGDIOBJ hOldBrush = ::SelectObject(hDC, ::GetSysColorBrush(COLOR_HIGHLIGHT));
::PatBlt(hDC, rcDrawFrame.left, rcDrawFrame.top, rcDrawFrame.Width(), rcDrawFrame.Height(), DSTINVERT);
::SelectObject(hDC, hOldBrush);

But for some reason the background rectangle comes out as black, and I know that COLOR_HIGHLIGHT is blue on this system: 但是由于某种原因,背景矩形显示为黑色,并且我知道该系统上的COLOR_HIGHLIGHT是蓝色的:

在此处输入图片说明

So what am I doing wrong here? 那我在做什么错呢?

You might want to consider a different strategy; 您可能需要考虑其他策略; drawing the text twice with inverted clipped region . 用倒截的区域画两次文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM