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. [SOLVED] Help needed with Alphablend function [modified]

[SOLVED] Help needed with Alphablend function [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestion
8 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.
  • R Offline
    R Offline
    Rozis
    wrote on last edited by
    #1

    Is there anbody out there who has a working code snippet that uses WinApi function Alphablend()? Can Alphablend() be used with a bitmap that has no alphachannel? Thanx

    modified on Sunday, June 28, 2009 5:00 AM

    P 1 Reply Last reply
    0
    • R Rozis

      Is there anbody out there who has a working code snippet that uses WinApi function Alphablend()? Can Alphablend() be used with a bitmap that has no alphachannel? Thanx

      modified on Sunday, June 28, 2009 5:00 AM

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      I use it on a splash screen. header file:

      typedef BOOL (WINAPI ALPHABLEND)(HDC, int, int, int, int, HDC, int, int, int, int, BLENDFUNCTION);
      typedef ALPHABLEND* LPALPHABLEND;

      class ...
      {
      ...
      HINSTANCE m_hLib;
      LPALPHABLEND m_lpfnAlphaBlend;
      ...
      };

      source file:

      // in class c'tor
      m_hLib = LoadLibrary(_T("msimg32.dll"));
      if (m_hLib != NULL)
      {
      m_lpfnAlphaBlend = (LPALPHABLEND)GetProcAddress(m_hLib, "AlphaBlend");
      }

      // in class d'tor
      if (m_hLib != NULL)
      {
      FreeLibrary(m_hLib);
      m_hLib = NULL;
      }

      BOOL C*********::OnEraseBkgnd(CDC* pDC)
      {
      CDialog::OnEraseBkgnd(pDC);

      if (m\_lpfnAlphaBlend != NULL)
      {
          CRect rc;
          GetClientRect(rc);
      
          BITMAP b;
          if (GetObject(m\_hBitmap,
                        sizeof(BITMAP),
                        &b))
          {
              CDC DC;
              DC.CreateCompatibleDC(pDC);
              int s = DC.SaveDC();
      
              DC.SelectObject(m\_hBitmap);
              
              BLENDFUNCTION bf = {0};
              bf.BlendOp = AC\_SRC\_OVER;
              bf.SourceConstantAlpha = 0x20;
      
              m\_lpfnAlphaBlend(\*pDC,
                               (rc.Width() - b.bmWidth) / 2,
                               (rc.Height() - b.bmHeight) / 2,
                               b.bmWidth,
                               b.bmHeight,
                               DC,
                               0,
                               0,
                               b.bmWidth,
                               b.bmHeight,
                               bf);
              DC.RestoreDC(s);
          }
      }
      
      return TRUE;
      

      }


      You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

      R 1 Reply Last reply
      0
      • P PJ Arends

        I use it on a splash screen. header file:

        typedef BOOL (WINAPI ALPHABLEND)(HDC, int, int, int, int, HDC, int, int, int, int, BLENDFUNCTION);
        typedef ALPHABLEND* LPALPHABLEND;

        class ...
        {
        ...
        HINSTANCE m_hLib;
        LPALPHABLEND m_lpfnAlphaBlend;
        ...
        };

        source file:

        // in class c'tor
        m_hLib = LoadLibrary(_T("msimg32.dll"));
        if (m_hLib != NULL)
        {
        m_lpfnAlphaBlend = (LPALPHABLEND)GetProcAddress(m_hLib, "AlphaBlend");
        }

        // in class d'tor
        if (m_hLib != NULL)
        {
        FreeLibrary(m_hLib);
        m_hLib = NULL;
        }

        BOOL C*********::OnEraseBkgnd(CDC* pDC)
        {
        CDialog::OnEraseBkgnd(pDC);

        if (m\_lpfnAlphaBlend != NULL)
        {
            CRect rc;
            GetClientRect(rc);
        
            BITMAP b;
            if (GetObject(m\_hBitmap,
                          sizeof(BITMAP),
                          &b))
            {
                CDC DC;
                DC.CreateCompatibleDC(pDC);
                int s = DC.SaveDC();
        
                DC.SelectObject(m\_hBitmap);
                
                BLENDFUNCTION bf = {0};
                bf.BlendOp = AC\_SRC\_OVER;
                bf.SourceConstantAlpha = 0x20;
        
                m\_lpfnAlphaBlend(\*pDC,
                                 (rc.Width() - b.bmWidth) / 2,
                                 (rc.Height() - b.bmHeight) / 2,
                                 b.bmWidth,
                                 b.bmHeight,
                                 DC,
                                 0,
                                 0,
                                 b.bmWidth,
                                 b.bmHeight,
                                 bf);
                DC.RestoreDC(s);
            }
        }
        
        return TRUE;
        

        }


        You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

        R Offline
        R Offline
        Rozis
        wrote on last edited by
        #3

        Tnx for an answer.. I spelled your code character by character and it seems that i do essentially the same. So this leaves me to ask you some stupid questions: 1. Is bf.BlendOp = AC_SRC_OVER; the same as: bf.BlendOp = 0; ?? 2. To my opinion you do nothing to add an alpha channel to your bitmap. Thus your bitmap has no alpha channel - do you agree on that? 3. Because i use another language then you: what does int s = DC.SaveDC() ; do? Is this one essential to Alphablend? Thanks Rozis

        P 1 Reply Last reply
        0
        • R Rozis

          Tnx for an answer.. I spelled your code character by character and it seems that i do essentially the same. So this leaves me to ask you some stupid questions: 1. Is bf.BlendOp = AC_SRC_OVER; the same as: bf.BlendOp = 0; ?? 2. To my opinion you do nothing to add an alpha channel to your bitmap. Thus your bitmap has no alpha channel - do you agree on that? 3. Because i use another language then you: what does int s = DC.SaveDC() ; do? Is this one essential to Alphablend? Thanks Rozis

          P Offline
          P Offline
          PJ Arends
          wrote on last edited by
          #4

          Rozis wrote:

          1. Is bf.BlendOp = AC_SRC_OVER; the same as: bf.BlendOp = 0; ??

          from wingdi.h:

          #define AC_SRC_OVER 0x00

          Rozis wrote:

          2. To my opinion you do nothing to add an alpha channel to your bitmap. Thus your bitmap has no alpha channel - do you agree on that?

          From MSDN[^]:

          When the AlphaFormat member is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, the AlphaBlend function will fail.

          So you are correct, I did not worry about the alpha channel. I just set the AlphaFormat value to zero.

          Rozis wrote:

          3. Because i use another language then you: what does int s = DC.SaveDC() ; do? Is this one essential to Alphablend?

          See http://msdn.microsoft.com/en-ca/library/dd162945(VS.85).aspx[^] and http://www.flounder.com/savedc.htm[^]


          You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

          R 1 Reply Last reply
          0
          • P PJ Arends

            Rozis wrote:

            1. Is bf.BlendOp = AC_SRC_OVER; the same as: bf.BlendOp = 0; ??

            from wingdi.h:

            #define AC_SRC_OVER 0x00

            Rozis wrote:

            2. To my opinion you do nothing to add an alpha channel to your bitmap. Thus your bitmap has no alpha channel - do you agree on that?

            From MSDN[^]:

            When the AlphaFormat member is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, the AlphaBlend function will fail.

            So you are correct, I did not worry about the alpha channel. I just set the AlphaFormat value to zero.

            Rozis wrote:

            3. Because i use another language then you: what does int s = DC.SaveDC() ; do? Is this one essential to Alphablend?

            See http://msdn.microsoft.com/en-ca/library/dd162945(VS.85).aspx[^] and http://www.flounder.com/savedc.htm[^]


            You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

            R Offline
            R Offline
            Rozis
            wrote on last edited by
            #5

            hmmm... im out of questions but it still doesn't run. This my code: (warning: this is not C-code, but i'll explain when needed)

            hMemDC:=CreateCompatibleDC(hdc) // ':=' is used as '=' in C
            if hMemDC!=NULL_PTR
            hbitmap:=CreatecompatibleBitmap(hdc,w,h)
            hold:=SelectObject(hMemDC,hBitmap)
            Rectangle(hmemDC,0,0,w,h)
            blendfunc.BlendOp:=0 // blendfunc is a structure BlendOp a member of this structure
            blendfunc.BlendFlags:=0
            blendfunc.SourceConstantAlpha:=128 //half transparent
            blendfunc.AlphaFormat:=0
            AlphaBlend(hdc,x,y,w,h,hMemDC,0,0,w,h,@blendfunc) // Here's the troublemaker
            //StretchBlt(hdc,x,x,w,h,hMemDC,0,0,w,h,SRCCOPY)
            //BitBlt(hdc,x,x,w,h,hMemDC,0,0,SRCCOPY)
            SelectObject(hMemDC,hold)
            DeleteObject(hBitmap)
            DeleteDC(hMemDC)
            endif

            When i use StretchBlt() or BitBlt() instead it works (altough not transparent). I checked if x,y,w and h are possitive - they are. Think you may ask whats '@blendfunc'. It means 'pointer to structure'. Use this construct for years so must be correct (for example in GetClientRect()). Tried a demo-exe from the code project to find out if problem is system related - it worked ok. Checked the prototype and the structure. I must do something really stupid... Rozis

            P 1 Reply Last reply
            0
            • R Rozis

              hmmm... im out of questions but it still doesn't run. This my code: (warning: this is not C-code, but i'll explain when needed)

              hMemDC:=CreateCompatibleDC(hdc) // ':=' is used as '=' in C
              if hMemDC!=NULL_PTR
              hbitmap:=CreatecompatibleBitmap(hdc,w,h)
              hold:=SelectObject(hMemDC,hBitmap)
              Rectangle(hmemDC,0,0,w,h)
              blendfunc.BlendOp:=0 // blendfunc is a structure BlendOp a member of this structure
              blendfunc.BlendFlags:=0
              blendfunc.SourceConstantAlpha:=128 //half transparent
              blendfunc.AlphaFormat:=0
              AlphaBlend(hdc,x,y,w,h,hMemDC,0,0,w,h,@blendfunc) // Here's the troublemaker
              //StretchBlt(hdc,x,x,w,h,hMemDC,0,0,w,h,SRCCOPY)
              //BitBlt(hdc,x,x,w,h,hMemDC,0,0,SRCCOPY)
              SelectObject(hMemDC,hold)
              DeleteObject(hBitmap)
              DeleteDC(hMemDC)
              endif

              When i use StretchBlt() or BitBlt() instead it works (altough not transparent). I checked if x,y,w and h are possitive - they are. Think you may ask whats '@blendfunc'. It means 'pointer to structure'. Use this construct for years so must be correct (for example in GetClientRect()). Tried a demo-exe from the code project to find out if problem is system related - it worked ok. Checked the prototype and the structure. I must do something really stupid... Rozis

              P Offline
              P Offline
              PJ Arends
              wrote on last edited by
              #6

              Rozis wrote:

              Think you may ask whats '@blendfunc'. It means 'pointer to structure'.

              That is the problem right there. AlphaBlend takes the structure, not a pointer to a structure, as the final parameter.


              You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

              R 1 Reply Last reply
              0
              • P PJ Arends

                Rozis wrote:

                Think you may ask whats '@blendfunc'. It means 'pointer to structure'.

                That is the problem right there. AlphaBlend takes the structure, not a pointer to a structure, as the final parameter.


                You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

                R Offline
                R Offline
                Rozis
                wrote on last edited by
                #7

                Guess what, you're right!! That was the thing i was overlooking. You're a genius!!! Did a quick try, made 0x00800000 as the last parameter to Alphablend() and it does the job. And it is lightyears faster then the workaround i made with getpixel(). Problem solved! The reason i never questioned it, is because a struct is a chunk of memory so i expected this would always be a pointer when used as a parameter. I mean: when a have a struct of let's say 200 bytes and use it as a parameter to a function would this mean these 200 bytes are allocated on the stack? Saw you're living in Canada, although you have a dutch name. So whenever you are in holland, call me, i have a present for you. Thanks for taking your time. Rozis

                P 1 Reply Last reply
                0
                • R Rozis

                  Guess what, you're right!! That was the thing i was overlooking. You're a genius!!! Did a quick try, made 0x00800000 as the last parameter to Alphablend() and it does the job. And it is lightyears faster then the workaround i made with getpixel(). Problem solved! The reason i never questioned it, is because a struct is a chunk of memory so i expected this would always be a pointer when used as a parameter. I mean: when a have a struct of let's say 200 bytes and use it as a parameter to a function would this mean these 200 bytes are allocated on the stack? Saw you're living in Canada, although you have a dutch name. So whenever you are in holland, call me, i have a present for you. Thanks for taking your time. Rozis

                  P Offline
                  P Offline
                  PJ Arends
                  wrote on last edited by
                  #8

                  Rozis wrote:

                  You're a genius!!!

                  Not really, I just read the documentation :rolleyes: , I am just glad to be able to help.

                  Rozis wrote:

                  Saw you're living in Canada, although you have a dutch name.So whenever you are in holland, call me, i have a present for you

                  My grandparents emigrated from Holland to Canada in the fifties. I would love to go there some day and see where my heritage comes from. Maybe when I do finally manage the trip I will try to look you up and take you up on your offer.


                  You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

                  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