EVC下實現WinCE軟體換膚之換膚基礎類CBitmapSK

來源:互聯網
上載者:User

由於在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下開發的。。。

相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.