How to fix alpha value after calling GDI text functions?

I have an application that uses the Aero glass effect, so each pixel has an alpha value in addition to the red, green and blue values. I have one 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, making the text translucently show which window is under my application.

After invoking the rendering of the text, I would like to go through all the pixels in the control and set its alpha value to 255. What is the best way to do this?

I'm out of luck with the features BitBlt, GetPixeland SetPixel. They do not seem to pay attention to the alpha value.

Here are other solutions that I reviewed and rejected:

  • Draw a bitmap, then copy the bitmap to the device: with this approach, text rendering does not use the characteristics of the monitor (for example, ClearText). If you know a way to get GDI to render text in a bitmap just like on the screen, this will also solve my problem.
  • Use GDI + to render text: this app originally used GDI + to render text (before I started working with Aero support). I switched to GDI due to the difficulties that I encountered while trying to accurately measure strings using GDI +. I would rather not come back.
  • Define the Aero scope to avoid the corresponding control: my application window is actually a child window of another application running in another process. I do not have direct control over the Aero settings in the top-level window.

The application is written in C # using Windows Forms, although I am not above using Interop to call Win32 API functions.

+2
source share
2 answers

, . , , , . BufferedGraphics Graphics (.. ). BufferedGraphics TextRenderer.DrawText() , . , Buffered Graphics , , .

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

CompositingMode, , , , , , .

+1

, GDI + , . , , GDI +, ( ) , .

, GDI + . , , GDI. , GDI + , GDI, .

0

Source: https://habr.com/ru/post/1502508/


All Articles