[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 839
  • Last Modified:

Creating Oval Shape Buttons with Bitmap

Hi,
  I m creating a dialog based application wherein I want to use oval shaped Buttons having a bitmap on it. This Bitmap should take the oval shape of the Button...
Can anyone please help me find a solution to this, by providing the necessary code..
Thanx  
0
Shilpa
Asked:
Shilpa
  • 2
  • 2
1 Solution
 
chensuCommented:
See the Non-Rectangular buttons section of the following page.

http://www.codeguru.com/buttonctrl/index.shtml
0
 
ShilpaAuthor Commented:
Hi chensu,
 I had tried all these options that u hav provided.....but the bitmap does'nt take the oval shape of the Button....It gives a rectangular shape of the Bitmap itself due to which the rounded effect of the button is lost..
Can u please suggest a differnt method to achieve the same.....
Waiting eagerly for ur reply
Thanx
0
 
RONSLOWCommented:
Create your bitmap so that there is a unique colour around the outside of the oval shap

Make the button owner-drawn and set up the bitmap so that it the outside color is drawn transparently (fairly easy to do).

Then you should only see an oval.

The only catch (if it is one) is that the full rectangle still active as a button .. but it will look like only an oval.

Another alternative is to use a non-rectangular window for the button itself (you can apply a path the specifies the window shape, there is a clock example that shows a round clock application).  This would need further investigation.
0
 
ShilpaAuthor Commented:
Hi Ronslow,
Thanx...
   u hav suggested the method of making the Bitmap transparent on the button...It would be nice of u, if u could please provide the code for the same..
Waiting eagerly for ur reply..
Thanx once again.
0
 
RONSLOWCommented:
One way would be to put the bitmap in an ImageList (which has methods for drawing transparently).

The other is to use the BitBlt methods which use the ROP codes to do transparency.

There are numerous examples around.  For example, MS KB article "HOWTO: Drawing Transparent Bitmaps ID: Q79212"

You could also try codeguru (think its name has changed now) or look elsewhere here.

Here is some code that does it that I dug up in an MSDN article:

void MSJDrawTransparentBitmap(CDC* pDC, CBitmap* pBitmap, int xStart,                               int yStart, COLORREF cTransparentColor)
{
   CBitmap    bmAndBack, bmAndObject, bmAndMem, bmSave;
   CDC        dcMem, dcBack, dcObject, dcTemp, dcSave;

   dcTemp.CreateCompatibleDC(pDC);
   dcTemp.SelectObject(pBitmap);          // Select the bitmap


   BITMAP     bm;
   pBitmap->GetObject(sizeof(BITMAP), (LPSTR)&bm);

   CPoint     ptSize;
   ptSize.x = bm.bmWidth;                // Get width of bitmap
   ptSize.y = bm.bmHeight;               // Get height of bitmap
   dcTemp.DPtoLP(&ptSize, 1);            // Convert from device
                                         // to logical points

   // Create some DCs to hold temporary data.
   dcBack.CreateCompatibleDC(pDC);
   dcObject.CreateCompatibleDC(pDC);
   dcMem.CreateCompatibleDC(pDC);
   dcSave.CreateCompatibleDC(pDC);

   // Create a bitmap for each DC. DCs are required for a number of GDI functions.

   // Monochrome DC
   bmAndBack.CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);

   // Monochrome DC
   bmAndObject.CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);

   bmAndMem.CreateCompatibleBitmap(pDC, ptSize.x, ptSize.y);
   bmSave.CreateCompatibleBitmap(pDC, ptSize.x, ptSize.y);

   // Each DC must select a bitmap object to store pixel data.
   CBitmap* pbmBackOld   = dcBack.SelectObject(&bmAndBack);
   CBitmap* pbmObjectOld = dcObject.SelectObject(&bmAndObject);
   CBitmap* pbmMemOld    = dcMem.SelectObject(&bmAndMem);
   CBitmap* pbmSaveOld   = dcSave.SelectObject(&bmSave);

   // Set proper mapping mode.
   dcTemp.SetMapMode(pDC->GetMapMode());

   // Save the bitmap sent here, because it will be overwritten.
   dcSave.BitBlt(0, 0, ptSize.x, ptSize.y, &dcTemp, 0, 0, SRCCOPY);

   // Set the background color of the source DC to the color
   // contained in the parts of the bitmap that should be transparent
   COLORREF cColor = dcTemp.SetBkColor(cTransparentColor);

   // Create the object mask for the bitmap by performing a BitBlt
   // from the source bitmap to a monochrome bitmap.
   dcObject.BitBlt(0, 0, ptSize.x, ptSize.y, &dcTemp, 0, 0, SRCCOPY);

   // Set the background color of the source DC back to the original
   // color.
   dcTemp.SetBkColor(cColor);

   // Create the inverse of the object mask.
   dcBack.BitBlt(0, 0, ptSize.x, ptSize.y, &dcObject, 0, 0, NOTSRCCOPY);

   // Copy the background of the main DC to the destination.
   dcMem.BitBlt(0, 0, ptSize.x, ptSize.y, pDC, xStart, yStart, SRCCOPY);

   // Mask out the places where the bitmap will be placed.
   dcMem.BitBlt(0, 0, ptSize.x, ptSize.y, &dcObject, 0, 0, SRCAND);

   // Mask out the transparent colored pixels on the bitmap.
   dcTemp.BitBlt(0, 0, ptSize.x, ptSize.y, &dcBack, 0, 0, SRCAND);

   // XOR the bitmap with the background on the destination DC.
   dcMem.BitBlt(0, 0, ptSize.x, ptSize.y, &dcTemp, 0, 0, SRCPAINT);

   // Copy the destination to the screen.
   pDC->BitBlt(xStart, yStart, ptSize.x, ptSize.y, &dcMem, 0, 0, SRCCOPY);

   // Place the original bitmap back into the bitmap sent here.
   dcTemp.BitBlt(0, 0, ptSize.x, ptSize.y, &dcSave, 0, 0, SRCCOPY);

   // Reset the memory bitmaps.
   dcBack.SelectObject(pbmBackOld);
   dcObject.SelectObject(pbmObjectOld);
   dcMem.SelectObject(pbmMemOld);
   dcSave.SelectObject(pbmSaveOld);

   // Memory DCs and Bitmap objects will be deleted automatically
}
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now