操作方法

操作方法
private:
CBitmap m_bitmap;
BOOL CModify::OnInitDialog()
{
if(!CDialog::OnInitDialog())
{
return FALSE;
}
m_bitmap.LoadBitmap(IDB_BITMAP_MODIFY);
return TRUE;
}
void CModify::OnPaint()
{
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
if(m_bitmap.GetSafeHandle())
{
DrawTransparentBitmap( dc.GetSafeHdc(), (HBITMAP)m_bitmap.GetSafeHandle(), 0, 0, 0, 0, 
rect.Width(), rect.Height(), RGB(0,255,0) );
}
}
绘图函数
void DrawTransparentBitmap(HDC hdc,HBITMAP hBitmap, int xStart, int yStart, 
int xBitmap, int yBitmap, int xWidth, int yHeight,
COLORREF cTransparentColor)
{              
   COLORREF   cColor;
   HBITMAP    bmAndBack, bmAndObject, bmAndMem, bmSave;
   HBITMAP    bmBackOld, bmObjectOld, bmMemOld, bmSaveOld, bmTempOld;
   HDC        hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
   POINT      ptSize;
 
   if(NULL == hdc || NULL == hBitmap)
return;
   hdcTemp = CreateCompatibleDC(hdc);
   bmTempOld = (HBITMAP)SelectObject(hdcTemp, hBitmap);   // Select the bitmap
 
   ptSize.x = xWidth;            // Get width of bitmap
   ptSize.y = yHeight;           // Get height of bitmap
   DPtoLP(hdcTemp, &ptSize, 1);      // Convert from device
                                     // to logical points
   hdcBack   = CreateCompatibleDC(hdc);
   hdcObject = CreateCompatibleDC(hdc);
   hdcMem    = CreateCompatibleDC(hdc);
   hdcSave   = CreateCompatibleDC(hdc);
 
   // 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(hdc, ptSize.x, ptSize.y);
   bmSave      = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
 
   // Each DC must select a bitmap object to store pixel data.
   bmBackOld   = (HBITMAP)SelectObject(hdcBack, bmAndBack);
   bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
   bmMemOld    = (HBITMAP)SelectObject(hdcMem, bmAndMem);
   bmSaveOld   = (HBITMAP)SelectObject(hdcSave, bmSave);
 
   // Set proper mapping mode.
   SetMapMode(hdcTemp, GetMapMode(hdc));
 
 
   // Save the bitmap sent here, because it will be overwritten.
   BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, xBitmap, yBitmap, SRCCOPY);
 
   // Set the background color of the source DC to the color.
   // contained in the parts of the bitmap that should be transparent
   cColor = SetBkColor(hdcTemp, cTransparentColor);
 
   // Create the object mask for the bitmap by performing a BitBlt()
   // from the source bitmap to a monochrome bitmap.
   BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, xBitmap, yBitmap,
          SRCCOPY);
 
   // Set the background color of the source DC back to the original
   // color.
   SetBkColor(hdcTemp, cColor);
 
 
   // Create the inverse of the object mask.
   BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
          NOTSRCCOPY);
 
   // Copy the background of the main DC to the destination.
   BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
          SRCCOPY);
 
   // Mask out the places where the bitmap will be placed.
   BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
 
   // Mask out the transparent colored pixels on the bitmap.
   BitBlt(hdcTemp, xBitmap, yBitmap, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
 
   // XOR the bitmap with the background on the destination DC.
   BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, xBitmap, yBitmap, SRCPAINT);
 
 
   // Copy the destination to the screen.
   BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
          SRCCOPY);
 
   // Place the original bitmap back into the bitmap sent here.
   BitBlt(hdcTemp, xBitmap, yBitmap, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
 
   // Delete the memory bitmaps.
   SelectObject(hdcTemp, bmTempOld);
 
   DeleteObject(SelectObject(hdcBack, bmBackOld));
   DeleteObject(SelectObject(hdcObject, bmObjectOld));
   DeleteObject(SelectObject(hdcMem, bmMemOld));
   DeleteObject(SelectObject(hdcSave, bmSaveOld));
 
   // Delete the memory DCs.
   DeleteDC(hdcMem);
   DeleteDC(hdcBack);
 
   DeleteDC(hdcObject);
   DeleteDC(hdcSave);
   DeleteDC(hdcTemp);
}
posted @ 2013-02-26 12:29  中国男孩  阅读(184)  评论(0编辑  收藏  举报