作者:茫茫人海啊啊啊_574 | 来源:互联网 | 2023-05-18 06:02
Iamusinglayeredwindowsanddrawingaroundedrectangleonthescreen.However,Idliketosmoot
I am using layered windows and drawing a rounded rectangle on the screen. However, I'd like to smooth out the jagged edges. I think that I'll need alpha blending for this. Is there a way I can do this with GDI?
我正在使用分层窗口并在屏幕上绘制一个圆角矩形。但是,我想平滑锯齿状的边缘。我认为我需要alpha混合。我有办法用GDI做到这一点吗?
4 个解决方案
There isn't an easy way to do such drawing with just GDI calls. What you want isn't just alpha blending: you want anti-aliasing. That usually involves drawing what you want at a larger resolution and then scaling down.
只用GDI调用就没有简单的方法来进行这种绘图。你想要的不只是alpha混合:你想要抗锯齿。这通常涉及以更大的分辨率绘制您想要的内容,然后缩小。
What I've done in the past for similar problems is to use an art program to draw whatever shape I want (e.g. a rounded corner) much larger than I needed it in black and white. When I wanted to draw it I would scale the black and white bitmap to whatever size I wanted (using a variant of a scaling class from Code Project). This gives me a grayscale image that I can use as an alpha channel, which I'd then use for alpha blending, either by calling the Win32 function AlphaBlend, or by using a DIBSection and manually changing the appropriate pixels.
我过去对类似问题所做的是使用艺术程序绘制我想要的任何形状(例如圆角),比我在黑白中所需要的大得多。当我想绘制它时,我会将黑白位图缩放到我想要的任何大小(使用Code Project中的缩放类的变体)。这给了我一个灰度图像,我可以用它作为alpha通道,然后通过调用Win32函数AlphaBlend,或者通过使用DIBSection并手动更改相应的像素,我将其用于alpha混合。
Another variation of this approach would be to allocate a DIBSection about four times larger than you wanted the final result, draw into that, and then use the above scaling class to scale it down: the scaling from a larger image will give the appropriate smoothing effect.
这种方法的另一个变体是分配一个大约比你想要的最终结果大四倍的DIBSection,然后使用上面的缩放类来缩小它:从更大的图像缩放将给出适当的平滑效果。
If all this sounds like quite a lot of work: well, it is.
如果所有这些听起来像是很多工作:嗯,确实如此。
EDIT: To answer the title of this question: you can create a bitmap with an alpha channel by calling CreateDIBSection. That won't on its own do what you want though, I think.
编辑:要回答这个问题的标题:你可以通过调用CreateDIBSection创建一个带alpha通道的位图。我想,这不会自己做你想做的事。