问题
I have a application that uses the Aero glass effect, so each pixel has an alpha value in addition to red, green, and blue values. I have one custom-draw control that has a solid white background (alpha = 255). I would like to draw solid text on the control using the GDI text functions. However, these functions set the alpha value to an arbitrary value, causing the text to translucently show whatever window is beneath my application's.
After calling rendering the text, I would like to go through all of the pixels in the control and set their alpha value back to 255. What's the best way to do that?
I haven't had any luck with the BitBlt
, GetPixel
, and SetPixel
functions. They appear to be oblivious to the alpha value.
Here are other solutions that I have considered and rejected:
- Draw to a bitmap, then copy the bitmap to the device: With this approach, the text rendering does not make use of the characteristics of the monitor (e.g., ClearText). If you know of a way to get GDI to render the text to the bitmap exactly as it would render to the screen, that would also solve my problem.
- Use GDI+ for text rendering: This application originally used GDI+ for text rendering (before I started working on Aero support). I switched to GDI because of difficulties I encountered trying to accurately measure strings with GDI+. I'd rather not switch back.
- Set the Aero region to avoid the control in question: My application's window is actually a child window of a different application running in a different process. I don't have direct control over the Aero settings on the top-level window.
The application is written in C# using Windows Forms, though I'm not above using Interop to call Win32 API functions.
回答1:
Below is the solution I eventually came up with. It's kind of ugly and could probably be simplified, but it works. The idea is to create a BufferedGraphics object based on the original Graphics object (i.e., the screen). In the BufferedGraphics object, TextRenderer.DrawText() will render the text exactly like it would if it was drawing to the screen. I then create a regular graphics object, copy the Buffered Graphics object to the regular graphics object, and finally draw the regular graphics object to the screen.
Rectangle inner = new Rectangle(Point.Empty, ContentRectangle.Size);
using (BufferedGraphics bg = BufferedGraphicsManager.Current.Allocate(e.Graphics, inner)) {
using (Bitmap bmp = new Bitmap(inner.Width, inner.Height, bg.Graphics)) {
using (Graphics bmpg = Graphics.FromImage(bmp)) {
bg.Graphics.Clear(BackColor);
do_my_drawing(bg.Graphics);
bg.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
e.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
bmpg.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
bg.Render(bmpg);
e.Graphics.DrawImageUnscaledAndClipped(bmp, ContentRectangle);
}
}
}
Setting all of the CompositingMode attributes probably isn't necessary, but once I got it working I didn't bother testing all the permutations to figure out which, if any, are needed.
回答2:
You might want to try using GDI+ on a separate bitmap, copying that to the screen. The advantage is that the alpha of text drawn with GDI+ will show the color of the bitmap (which you would've painted white) instead of making the bitmap transparant.
But indeed, GDI+ has very buggy or just inaccurate text measuring functionality. But maybe you can use the GDI functions for that. In the end, your GDI+ text rendering should look exactly like the GDI text rendering you have now, so it might be worth a try.
来源:https://stackoverflow.com/questions/2691543/how-do-i-fix-the-alpha-value-after-calling-gdi-text-functions