Using GDI+ to display static pictures in MFC dialogs with an alpha channel
-
Hello, I'm writing an MFC application that has headings of anti-aliased text in dialogs that are CStatic pictures (bitmaps). They appear against another flat coloured CStatic that has it's color set to "Gray" - the actual shade of gray varies, based on the windows style - a unique shade for windows classic style, windows XP style, or windows Vista style. I could just make the background of the overlaid, anti-aliased text the same as the colour of the flat "gray", but, as I've said, the exact color varies. What can I do to achieve the desired effect of having an image of anti-aliased text overlaid over the variable "gray", looking correct in all possible scenarios? GDI+ looks interesting, but I can't find a codeproject project that does what I've described. Thanks in advance, Sternocera
-
Hello, I'm writing an MFC application that has headings of anti-aliased text in dialogs that are CStatic pictures (bitmaps). They appear against another flat coloured CStatic that has it's color set to "Gray" - the actual shade of gray varies, based on the windows style - a unique shade for windows classic style, windows XP style, or windows Vista style. I could just make the background of the overlaid, anti-aliased text the same as the colour of the flat "gray", but, as I've said, the exact color varies. What can I do to achieve the desired effect of having an image of anti-aliased text overlaid over the variable "gray", looking correct in all possible scenarios? GDI+ looks interesting, but I can't find a codeproject project that does what I've described. Thanks in advance, Sternocera
Are the bitmaps composed programatically or with an image editor? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Are the bitmaps composed programatically or with an image editor? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
They are simple bitmap files, composed with an image editor, Regards, Sternocera
-
They are simple bitmap files, composed with an image editor, Regards, Sternocera
If you create the bitmaps with a transparent background and save them in a format that preserves alpha channel info (e.g. PNG) then you can use GDI+ to easily render the images on any background.
// Note: the GdiplusStartup/GdiplusShutdown code only needs to be done once per process
// not wrapped around every set of GDI+ callsULONG dwToken; Gdiplus::GdiplusStartupInput input; Gdiplus::GdiplusStartupOutput output; Gdiplus::Status status = Gdiplus::GdiplusStartup(&dwToken, &input, &output); if(status == Gdiplus::Ok) { Gdiplus::Bitmap \*srcBitmap = new Gdiplus::Bitmap(L"c:\\\\some.png", FALSE); Gdiplus::Graphics g(hwnd, 0); g.DrawImage(srcBitmap, 0, 0); delete srcBitmap; Gdiplus::GdiplusShutdown(dwToken); }
You could also use GDI+ to compose a bitmap programatically. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
If you create the bitmaps with a transparent background and save them in a format that preserves alpha channel info (e.g. PNG) then you can use GDI+ to easily render the images on any background.
// Note: the GdiplusStartup/GdiplusShutdown code only needs to be done once per process
// not wrapped around every set of GDI+ callsULONG dwToken; Gdiplus::GdiplusStartupInput input; Gdiplus::GdiplusStartupOutput output; Gdiplus::Status status = Gdiplus::GdiplusStartup(&dwToken, &input, &output); if(status == Gdiplus::Ok) { Gdiplus::Bitmap \*srcBitmap = new Gdiplus::Bitmap(L"c:\\\\some.png", FALSE); Gdiplus::Graphics g(hwnd, 0); g.DrawImage(srcBitmap, 0, 0); delete srcBitmap; Gdiplus::GdiplusShutdown(dwToken); }
You could also use GDI+ to compose a bitmap programatically. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark, Thanks for that. It would be preferable to have a CStatic inheriting class that encapsulated all of this GDI stuff from me, or perhaps worked in a different way, and allowed me to use resources, because the images's(currently a CStatic) position is currently dictated by a third party class that changes the position of widgets as the application's main window is resized, to keep their relative position - I need to maintain this. All I want is a CStatic with dithered transparency - Is there a more expedient way to get this effect? Regards, Sternocera
-
Mark, Thanks for that. It would be preferable to have a CStatic inheriting class that encapsulated all of this GDI stuff from me, or perhaps worked in a different way, and allowed me to use resources, because the images's(currently a CStatic) position is currently dictated by a third party class that changes the position of widgets as the application's main window is resized, to keep their relative position - I need to maintain this. All I want is a CStatic with dithered transparency - Is there a more expedient way to get this effect? Regards, Sternocera
Sternocera wrote:
It would be preferable to have a CStatic inheriting class that encapsulated all of this GDI stuff from me
You can do the rendering from anywhere you want. Instead of GDI+, you could create the bitmaps with a background of a certain color like RGB(0x00,0xFF,0x00). That color can then be used in the TransparentBlt() API to render a bitmap or to create a masked imagelist from the bitmap which can be rendered on the control.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Sternocera wrote:
It would be preferable to have a CStatic inheriting class that encapsulated all of this GDI stuff from me
You can do the rendering from anywhere you want. Instead of GDI+, you could create the bitmaps with a background of a certain color like RGB(0x00,0xFF,0x00). That color can then be used in the TransparentBlt() API to render a bitmap or to create a masked imagelist from the bitmap which can be rendered on the control.
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark, That sounds like a good solution. I'll investigate it. Thank you very much, Sternocera