Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using GDI+ to display static pictures in MFC dialogs with an alpha channel

Using GDI+ to display static pictures in MFC dialogs with an alpha channel

Scheduled Pinned Locked Moved C / C++ / MFC
c++winformsgraphicsquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sternocera
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • S 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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Are the bitmaps composed programatically or with an image editor? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        Are the bitmaps composed programatically or with an image editor? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S Offline
        S Offline
        Sternocera
        wrote on last edited by
        #3

        They are simple bitmap files, composed with an image editor, Regards, Sternocera

        M 1 Reply Last reply
        0
        • S Sternocera

          They are simple bitmap files, composed with an image editor, Regards, Sternocera

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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+ calls

          ULONG 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:

          S 1 Reply Last reply
          0
          • M Mark Salsbery

            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+ calls

            ULONG 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:

            S Offline
            S Offline
            Sternocera
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • S 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

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              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:

              S 1 Reply Last reply
              0
              • M Mark Salsbery

                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:

                S Offline
                S Offline
                Sternocera
                wrote on last edited by
                #7

                Mark, That sounds like a good solution. I'll investigate it. Thank you very much, Sternocera

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups