EVC下实现WinCE软件换肤之换肤基础类CBitmapSK

由于在wince下不支持hook,所以对ce下的软件换肤,会很慧心,不过,曾经看到过一个国外的网站上有pocket下实现hook的代码,没读懂,呵呵,自己太水了,这就涉及到另外一个问题了,还没有找到实现hook的方法,所有要另外想办法了,所以不管怎么样都要重写控件,参考了一些VC实现的换肤,在此实现了基本类CBitmapSK。其中有个郁闷的地方是evc对CRgn支持的不全面,对任意形状的窗口实现有些麻烦,所以有个重要的函数不能实现,要想其他的办法实现,
//////////////////////////////////////////////////////////////////////
//
// BaseSK.h: interface for the CBitmapSK class.
//
// writer : erran
//
// time:  2006-03-10
//
//////////////////////////////////////////////////////////////////////

class CBitmapSK : public CBitmap 
{
public:
 
 CBitmapSK();
 virtual ~CBitmapSK();

 //load bitmap by resource file path
 BOOL LoadBitmapEx(LPCTSTR lpszFileName);

 //load bitmap by resource name or ID
 BOOL LoadBitmapEx(LPCTSTR lpszResourceName, UINT nIDResource);

 //attaches a hBitmap to a CBitmap object.
 BOOL Attach(HBITMAP hbmp)
 {
     return CBitmap::Attach(hbmp);
 }
 
 void Size(int &width, int &height)
 {
    BITMAP bm;
    memset(&bm, 0, sizeof(bm));
    CBitmap::GetBitmap(&bm);
    width = bm.bmWidth;
    height = bm.bmHeight;
 }

 //bitmap width
 int Width(void)
 {
    BITMAP bm;
    memset(&bm, 0, sizeof(bm));
    CBitmap::GetBitmap(&bm);
    return bm.bmWidth;
 }

 //bitmap height
 int Height()
 {
    BITMAP bm;
    memset(&bm, 0, sizeof(bm));
    CBitmap::GetBitmap(&bm);
    return bm.bmHeight;
 }

 /////// src(bmp) ---> Des(pDC)

 //draw current bitmap to device(pDC rect(lpr)) 
 BOOL Draw(CDC *pDC, LPRECT lprDes);
 //draw current bitmap to device(pDC rect(lpr)) , transparent 
 BOOL Draw(CDC *pDC, LPRECT lprDes, COLORREF crTrans, BOOL isTrans);

 //draw sub bitmap(the rect lprSrc of current bitmap) to special point( the begin point (x,y) of pDC device)
 BOOL Draw(CDC *pDC, int x, int y, LPRECT lprSrc);
 //draw sub bitmap to special point
 BOOL Draw(CDC *pDC, int x, int y, LPRECT lprSrc, COLORREF crTrans, BOOL isTrans);

 BOOL Fill(CDC *pDC, LPRECT lprDes);
 BOOL Fill(CDC *pDC, LPRECT lprDes, COLORREF crTrans, BOOL isTrans);

 BOOL Fill(CDC *pDC, LPRECT lprDes, LPRECT lprSrc); 
 BOOL Fill(CDC *pDC, LPRECT lprDes, LPRECT lprSrc, COLORREF crTrans, BOOL isTrans);

 //Drawing Transparent Bitmaps,Copies a bitmap transparently onto the destination DC
 BOOL TransparentDraw(CDC * pDC, int x, int y, COLORREF crColour);
 

 //Copies a bitmap from a source rectangle into a destination rectangle,
 //stretching or compressing the bitmap if necessary to fit the dimensions of the destination rectangle.
 BOOL StretchDraw(CDC *pDC, LPRECT lprDes, LPRECT lprSrc);
 //Copies a bitmap from a source rectangle into a destination rectangle,
 //stretching or compressing the bitmap if necessary to fit the dimensions of the destination rectangle. 
 BOOL StretchDraw(CDC *pDC, LPRECT lprSrc);

 //make a region from bitmap, It will show as a window     ::: faulse function :::
 //HRGN CreateRegion(COLORREF crColour, BOOL isTrans = TRUE);  

};

 


////for class CBitmapSK

CBitmapSK::CBitmapSK()
{
 
}

CBitmapSK::~CBitmapSK()
{
 
}

BOOL CBitmapSK::LoadBitmapEx(LPCTSTR lpszFileName)
{
   DeleteObject();

   ASSERT(m_hObject==NULL);

   HBITMAP hBitmap = NULL;

   hBitmap = (HBITMAP)::LoadImage(0, lpszFileName, IMAGE_BITMAP, 0, 0, 0);

   if(hBitmap == NULL) return FALSE;

   return CBitmap::Attach(hBitmap);
}

BOOL CBitmapSK::LoadBitmapEx(LPCTSTR lpszResourceName, UINT nIDResource)
{
   DeleteObject();

   ASSERT(m_hObject==NULL);
 
   if (lpszResourceName != NULL)
  {
    return CBitmap::LoadBitmap(lpszResourceName);
  }

  if (nIDResource != 0)
  {
     return CBitmap::LoadBitmap(nIDResource);
  }
 
  return FALSE;
}

BOOL CBitmapSK::Draw(CDC *pDC, LPRECT lprDes)
{
  ASSERT(pDC!=NULL);

  CDC dc;

  dc.CreateCompatibleDC(pDC);

  CBitmap *bmp = dc.SelectObject(this); 

  pDC->BitBlt(lprDes->left, lprDes->top,
    lprDes->right - lprDes->left, lprDes->bottom - lprDes->top,
    &dc, 0, 0, SRCCOPY);

  dc.SelectObject(bmp);  

  dc.DeleteDC();
  bmp = NULL;

  return TRUE;
}

BOOL CBitmapSK::Draw(CDC *pDC, LPRECT lprDes, COLORREF crTrans, BOOL isTrans)
{
  ASSERT(pDC!=NULL);

  if (!isTrans)
  {
    return Draw(pDC, lprDes);
  }
  else
  {
    return TransparentBitBlt(pDC->GetSafeHdc(),
             lprDes->left,lprDes->top,Width(),Height(),
             (HBITMAP)this->GetSafeHandle(),0,0,crTrans,NULL);
  }
}


BOOL CBitmapSK::Draw( CDC *pDC, int x, int y, LPRECT lprSrc)
{
   ASSERT(pDC!=NULL);

   CDC dc;

   dc.CreateCompatibleDC(pDC);

   CBitmap *pOldbmp = dc.SelectObject(this);

   if (lprSrc != NULL)
   {
     //Copies a bitmap from the source device context to this current device context
     pDC->BitBlt(x, y, lprSrc->right - lprSrc->left, lprSrc->bottom - lprSrc->top,
               &dc, lprSrc->left, lprSrc->top, SRCCOPY);
   }
   else
   {
      pDC->BitBlt(x, y, Width(), Height(), &dc, 0, 0, SRCCOPY);
   }

   dc.SelectObject(pOldbmp);
   dc.DeleteDC();
   pOldbmp = NULL;

   return TRUE;
}


BOOL CBitmapSK::Draw( CDC *pDC, int x, int y, LPRECT lprSrc, COLORREF crTrans, BOOL isTrans)
{
    ASSERT(pDC!=NULL);

    if (!isTrans)
   {
     return Draw(pDC, x, y, lprSrc);
    }
   else
  {
    return TransparentBitBlt(pDC->GetSafeHdc(), x, y, lprSrc->right - lprSrc->left, lprSrc->bottom - lprSrc->top,
         (HBITMAP)this->GetSafeHandle(), lprSrc->left, lprSrc->top, crTrans, NULL );
  }
}


BOOL CBitmapSK::Fill(CDC *pDC, LPRECT lprDes, LPRECT lprSrc)

   ASSERT(pDC!=NULL);

   int widthDes, widthSrc, heightDes, heightSrc;
   widthDes = abs(lprDes->right - lprDes->left);
   widthSrc = abs(lprSrc->right - lprSrc->left);
   heightDes = abs(lprDes->bottom - lprDes->top);
   heightSrc = abs(lprSrc->bottom - lprSrc->top);

   int paddingx, paddingy, i, j;
   paddingx = int(widthDes / widthSrc);
   paddingy = int(heightDes / heightSrc);
 
   CDC dc;
   dc.CreateCompatibleDC(pDC);
   CBitmap *pOldbmp = dc.SelectObject(this);

   if (widthDes <= widthSrc)        
  {
    if (heightDes <= heightSrc)
   {
      Draw(pDC, lprDes);
    }
   else
   {   
     for (i=1; i<paddingy; i++)
     {
        pDC->BitBlt(lprDes->left, lprDes->top + heightSrc*(i-1), widthDes, heightSrc,&dc, lprSrc->left, lprSrc->top, SRCCOPY);
     }
     pDC->BitBlt(lprDes->left,lprDes->top + heightSrc*(i-1), widthDes, heightDes - heightSrc*(i-1), &dc, lprSrc->left, lprSrc->top, SRCCOPY);
   }
 }
 else
 {
   if (heightDes <= heightSrc)
   {
      for (i=1; i<paddingx; i++)
     {
         pDC->BitBlt(lprDes->left + widthSrc*(i-1), lprDes->top, widthSrc, heightDes, &dc, lprSrc->left, lprSrc->top, SRCCOPY);
     }
     pDC->BitBlt(lprDes->left + widthSrc*(i-1), lprDes->top, widthDes - widthSrc*(i-1), heightDes, &dc, lprSrc->left, lprSrc->top, SRCCOPY);
   }
   else
   {
     for (j=1; j<=paddingy; j++)
    {
       for (i=1; i<=paddingx; i++)
       {
           pDC->BitBlt(lprDes->left + widthSrc*(i-1),lprDes->top + heightSrc*(j-1), widthSrc, heightSrc, &dc, lprSrc->left, lprSrc->top, SRCCOPY);    
       }
       pDC->BitBlt(lprDes->left + widthSrc*(i-1), lprDes->top + heightSrc*(j-1),widthDes - widthSrc*(i-1), heightSrc, &dc, lprSrc->left, lprSrc->top, SRCCOPY);
    }
   }
   for (i=1; i<=paddingx; i++)
   {
      pDC->BitBlt(lprDes->left + widthSrc*(i-1), lprDes->top + heightSrc*(j-1), widthSrc, heightDes - heightSrc*(j-1), &dc, lprSrc->left, lprSrc->top, SRCCOPY);
   }
   pDC->BitBlt(lprDes->left + widthSrc*(i-1), lprDes->top + heightSrc*(j-1),  widthDes - widthSrc*(i-1),
      heightDes - heightSrc*(j-1), &dc, lprSrc->left, lprSrc->top, SRCCOPY);
  }
 
  dc.SelectObject(pOldbmp);
  dc.DeleteDC();
  pOldbmp = NULL;

  return TRUE;
}

BOOL CBitmapSK::TransparentDraw(CDC * pDC, int x, int y, COLORREF crTrans)
{
  ASSERT(pDC!=NULL);

  return TransparentBitBlt(pDC->GetSafeHdc(), x, y, Width(), Height(), (HBITMAP)this->GetSafeHandle(),
        0, 0, crTrans, NULL  );
}


BOOL CBitmapSK::StretchDraw(CDC *pDC, LPRECT lprDes, LPRECT lprSrc)
{
 ASSERT(pDC!=NULL);

 if(lprDes == NULL) return FALSE;
 
 CDC dc;
 dc.CreateCompatibleDC(pDC);
 CBitmap *pOldbmp = dc.SelectObject(this);

 //SetStretchBltMode(pDC->GetSafeHdc(), COLORONCOLOR);

 if(!lprSrc)
 {
  pDC->StretchBlt(lprDes->left, lprDes->top, lprDes->right,  lprDes->bottom,  &dc,  0, 0,  Width(),  Height(), SRCCOPY);
 }
 else
 {
  pDC->StretchBlt(lprDes->left, lprDes->top, lprDes->right - lprDes->left, lprDes->bottom - lprDes->top,
      &dc, lprSrc->left, lprSrc->top, lprSrc->right - lprSrc->left, lprSrc->bottom - lprSrc->top,SRCCOPY);
 }
 
 dc.SelectObject(pOldbmp);
 dc.DeleteDC();
 pOldbmp = NULL;

 return TRUE; 
}

BOOL CBitmapSK::StretchDraw(CDC *pDC, LPRECT lprSrc)
{
 ASSERT(pDC!=NULL);

 CDC dc;
 dc.CreateCompatibleDC(pDC);
 CBitmap *pOldbmp = dc.SelectObject(this);
 
 pDC->StretchBlt(lprSrc->left, lprSrc->top, lprSrc->right, lprSrc->bottom, &dc,
     0,      0,      Width(),      Height(),     SRCCOPY);
 
 dc.SelectObject(pOldbmp);
 dc.DeleteDC();
 pOldbmp = NULL;

 return TRUE; 
}

使用这个类实现了对话筐的换肤,测试还行。

暂时还没发现ce下的换肤第三方控件,见过最好的也就是那个CCeButton了,还是在EVC3.0下开发的。。。

你可能感兴趣的:(bitmap)