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. Creating a colored, transparent CStatic

Creating a colored, transparent CStatic

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsquestion
2 Posts 1 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.
  • D Offline
    D Offline
    David Fleming
    wrote on last edited by
    #1

    I've been looking around in the articles on CStatics as well as in stuff on Bitmaps and the GDI, but I'm not quite finding what I need, so here goes.... What I would like to create is a CStatic-derived class that will make what looks like a partially transparent CStatic. Think of it as a piece of colored glass with text written on it, hovering over the background. I can handle making a CStatic transparent (there are a couple of nifty articles on that). In essence, taking an image of the part of the parent control behind the CStatic and painting that as the background of the CStatic. To be more explicit, here's a code snippet for doing basic transparent CStatic: This is in OnPaint for the CStatic-derived class (so dc is the supplied DC):

    CDC\* pdcT;
    pdcT = new CDC;
    pdcT->CreateCompatibleDC(&dc);
    CBitmap bitmap;
    CBitmap\* pOldBitmap;
    pOldBitmap = pdcT->SelectObject(&bitmap);
    dc.BitBlt(0, 0, rc.Width(), rc.Height(), pdcT, 0, 0, SRCCOPY);
    pdcT->SelectObject(pOldBitmap);
    delete pdcT;
    

    So what I need to be able to do is modify that "parent" image to add a wash of color, then paint that to the background of the CStatic. It seems that I need to do the modifying of the bitmap after it has been selected into the pdcT, but I don't know where to go on that. Or perhaps I'm missing something that I can do in the BitBlt function to accomplish this. Can it be done? And if so, how? Thanks.

    D 1 Reply Last reply
    0
    • D David Fleming

      I've been looking around in the articles on CStatics as well as in stuff on Bitmaps and the GDI, but I'm not quite finding what I need, so here goes.... What I would like to create is a CStatic-derived class that will make what looks like a partially transparent CStatic. Think of it as a piece of colored glass with text written on it, hovering over the background. I can handle making a CStatic transparent (there are a couple of nifty articles on that). In essence, taking an image of the part of the parent control behind the CStatic and painting that as the background of the CStatic. To be more explicit, here's a code snippet for doing basic transparent CStatic: This is in OnPaint for the CStatic-derived class (so dc is the supplied DC):

      CDC\* pdcT;
      pdcT = new CDC;
      pdcT->CreateCompatibleDC(&dc);
      CBitmap bitmap;
      CBitmap\* pOldBitmap;
      pOldBitmap = pdcT->SelectObject(&bitmap);
      dc.BitBlt(0, 0, rc.Width(), rc.Height(), pdcT, 0, 0, SRCCOPY);
      pdcT->SelectObject(pOldBitmap);
      delete pdcT;
      

      So what I need to be able to do is modify that "parent" image to add a wash of color, then paint that to the background of the CStatic. It seems that I need to do the modifying of the bitmap after it has been selected into the pdcT, but I don't know where to go on that. Or perhaps I'm missing something that I can do in the BitBlt function to accomplish this. Can it be done? And if so, how? Thanks.

      D Offline
      D Offline
      David Fleming
      wrote on last edited by
      #2

      OK, it looks like I solved my own problem. Just in case anyone else is interested, here's what I found:

      CPaintDC dc(this); //get a DC for the control
      //The next several lines create a compatible bitmap in memory
      // and draw a solid light green "bitmap" on it,
      // then select that bitmap into the DC in memory
      pDCMem = new CDC;
      pDCMem->CreateCompatibleDC(&dc);
      bmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
      pOldBitmap = pDCMem->SelectObject(&bmp);
      CBrush brush;
      brush.CreateSolidBrush(RGB(122,255,122));
      pDCMem->FillRect(rc, &brush);
      //After drawing on the DC in memory (text and whatever else)
      // use BitBlt to merge the bitmap in memory with what's on screen
      dc.BitBlt(0,0,rc.Width(),rc.Height(),pDCMem,0,0,SRCAND);
      //SRCAND combines the src and dest DCs using boolean AND
      //Then clean up
      pDCMem->SelectObject ( pOldBitmap ) ;
      delete pDCMem;

      And it works. Now the CStatic is light green, with text on it (which I did not include in the code snippet) and the bitmap "behind" it can be seen greenishly colored through 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