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. GDI / GDI+

GDI / GDI+

Scheduled Pinned Locked Moved C / C++ / MFC
winformsgraphicsannouncement
21 Posts 3 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.
  • M Mark Salsbery

    I "think" you may be able to use a 32-bit ARGB DIBsection - I put together this test just to make sure the alpha channel stuff was working. (My "hdc" would be your "dcMem" and you wouldn't want to delete it or un-select your alpha bitmap like I have if you pass it to UpdateLayeredWindow())

    Image TransparentSrcBitmap(L"clockbg.bmp");

    LONG lImageWidth = TransparentSrcBitmap.GetWidth();
    LONG lImageHeight = TransparentSrcBitmap.GetHeight();
    WORD wBitsPerPixel = 32;

    //LONG lBytesPerRow = (((lImageWidth * (long)wBitsPerPixel + 31L) & (~31L)) / 8L);
    LONG lBytesPerRow = lImageWidth * 4;

    BITMAPINFO bmi;
    memset(&bmi, 0, sizeof(BITMAPINFO));
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = lImageWidth;
    bmi.bmiHeader.biHeight = lImageHeight;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = wBitsPerPixel;
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biSizeImage = lBytesPerRow * lImageHeight;
    //bmi.bmiHeader.biXPelsPerMeter = 0;
    //bmi.bmiHeader.biYPelsPerMeter = 0;
    //bmi.bmiHeader.biClrUsed = 0;
    //bmi.bmiHeader.biClrImportant = 0;

    HDC hdc = ::CreateCompatibleDC(0);

    BYTE* pBitmapBits;
    HBITMAP hBitmap = ::CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&pBitmapBits, NULL, 0);

    if (hBitmap)
    {
    memset(pBitmapBits, 0, bmi.bmiHeader.biSizeImage);

    HGDIOBJ hOldBitmap = ::SelectObject(hdc, hBitmap);

    Graphics DstGraphics(hdc);
    DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());

    //** Test - blt the memory dc to the screen to make sure alpha channel data carried over
    BLENDFUNCTION bf;
    bf.BlendOp = AC_SRC_OVER;
    bf.BlendFlags = 0;
    bf.SourceConstantAlpha = 0x7F; // Alpha multiplier applied overall - use 0xFF - 0x7F is a test
    bf.AlphaFormat = AC_SRC_ALPHA;

    HDC hClientDC = ::GetDC(*this);
    ::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(),
    hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf);
    ::ReleaseDC(*this, hClientDC);
    //** End Test

    ::SelectObject(hdc, hOldBitmap); //<-- Don't do this when using with UpdateLayeredWindow()

    ::DeleteObject(hBitmap); //<-- Don't do this when using with UpdateLayeredWindow()
    }

    ::DeleteDC(hdc); //<-- Don't do this when using with UpdateLayeredWindow()

    Sorry about all the strange extra variables - I cut and paste from assorted worki

    A Offline
    A Offline
    Adno
    wrote on last edited by
    #12

    thank you Mark very much for taking the time, it mean as lot me. but is not quite there is coming out pixalated, the alpha value is being replace by a black colour. program running image: http://img118.imageshack.us/img118/8953/testwindowhu9.png . . . Code: the main method. http://rafb.net/p/1Dfouz58.html thank you so much ....:-D

    M 2 Replies Last reply
    0
    • A Adno

      thank you Mark very much for taking the time, it mean as lot me. but is not quite there is coming out pixalated, the alpha value is being replace by a black colour. program running image: http://img118.imageshack.us/img118/8953/testwindowhu9.png . . . Code: the main method. http://rafb.net/p/1Dfouz58.html thank you so much ....:-D

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

      No problem! Can you show your latest code? Mark

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      1 Reply Last reply
      0
      • A Adno

        thank you Mark very much for taking the time, it mean as lot me. but is not quite there is coming out pixalated, the alpha value is being replace by a black colour. program running image: http://img118.imageshack.us/img118/8953/testwindowhu9.png . . . Code: the main method. http://rafb.net/p/1Dfouz58.html thank you so much ....:-D

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

        What's "image" and what's "program window" in your screenshot? Never mind I get it LOL I'd still like to see your updated code if you can :) Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        A 1 Reply Last reply
        0
        • M Mark Salsbery

          What's "image" and what's "program window" in your screenshot? Never mind I get it LOL I'd still like to see your updated code if you can :) Mark

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          A Offline
          A Offline
          Adno
          wrote on last edited by
          #15

          Code: the main method. http://rafb.net/p/1Dfouz58.html ^i posted here

          M 2 Replies Last reply
          0
          • A Adno

            Code: the main method. http://rafb.net/p/1Dfouz58.html ^i posted here

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

            Try replacing int t = UpdateLayeredWindow(hwnd, NULL, NULL, &sz, hdc, &ptSrc, NULL, &bf, ULW_COLORKEY ); with int t = UpdateLayeredWindow(hwnd, NULL, NULL, &sz, hdc, &ptSrc, NULL, &bf, ULW_ALPHA ); You probably want to set bf.SourceConstantAlpha = 0xFF; (instead of 0x7F) too. Mark

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            1 Reply Last reply
            0
            • A Adno

              Code: the main method. http://rafb.net/p/1Dfouz58.html ^i posted here

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

              And comment out these lines - they were there for testing only (leave in the BLENDFUNCTION since you're using that)... // comment these out HDC hClientDC = ::GetDC(hwnd); ::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf); ::ReleaseDC(hwnd, hClientDC);

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              A 1 Reply Last reply
              0
              • M Mark Salsbery

                And comment out these lines - they were there for testing only (leave in the BLENDFUNCTION since you're using that)... // comment these out HDC hClientDC = ::GetDC(hwnd); ::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf); ::ReleaseDC(hwnd, hClientDC);

                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                A Offline
                A Offline
                Adno
                wrote on last edited by
                #18

                thanks a million one more thing since i've got you here. will i be able to draw on this window? i.e: Graphics graphics(GetDC(hwnd)); Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 100,100); i did a quick test it dont seem to be working. my goal would be to be as great as you one day lol

                M 1 Reply Last reply
                0
                • A Adno

                  thanks a million one more thing since i've got you here. will i be able to draw on this window? i.e: Graphics graphics(GetDC(hwnd)); Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 100,100); i did a quick test it dont seem to be working. my goal would be to be as great as you one day lol

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

                  Lamefif wrote:

                  will i be able to draw on this window?

                  Absolutely not! Just kidding. The DIBSection bitmap is already in a DC which is associated with a Graphics object (DstGraphics). That's where you should draw. Example...

                  // redraw the image background
                  DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());

                  // draw some stuff on top of the background
                  Bitmap bitmap(L"clockbg.bmp");
                  DstGraphics.DrawImage(&bitmap, 100,100);

                  You have full access to the bitmap that IS your window. You can write directly to the pixel bytes, use GDI+ to draw, use GDI to draw, or any combination of those :) You may need to call UpdateLayeredWindow() again to see changes (i think the hdcSrc param can be NULL for updates). Mark

                  "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                  A 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Lamefif wrote:

                    will i be able to draw on this window?

                    Absolutely not! Just kidding. The DIBSection bitmap is already in a DC which is associated with a Graphics object (DstGraphics). That's where you should draw. Example...

                    // redraw the image background
                    DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());

                    // draw some stuff on top of the background
                    Bitmap bitmap(L"clockbg.bmp");
                    DstGraphics.DrawImage(&bitmap, 100,100);

                    You have full access to the bitmap that IS your window. You can write directly to the pixel bytes, use GDI+ to draw, use GDI to draw, or any combination of those :) You may need to call UpdateLayeredWindow() again to see changes (i think the hdcSrc param can be NULL for updates). Mark

                    "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                    A Offline
                    A Offline
                    Adno
                    wrote on last edited by
                    #20

                    Cheers Mark man i new i could do it. :->

                    M 1 Reply Last reply
                    0
                    • A Adno

                      Cheers Mark man i new i could do it. :->

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

                      :beer:

                      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                      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