VC-MFC-PNG按钮实现

自助开通VIP,整站资源任意下载

近日一网友问PNG按钮的实现,前几天忙,这段时间看了下,做了个简单的DEMO

具体实现思路,因为MFC要画PNG需要引入GDI+头文件和包。这里引入之后新建了一个类,继承自BUTTON。

在PNG按钮中DrawItem中绘制时候需要引入对话框中传入的CDC 用于绘制整体背景。同时需要将消息CtlColor 中进行绘制。

绘制代码我参考了网上的一个案例。

HBRUSH CGdipButton::CtlColor(CDC* pScreenDC, UINT nCtlColor) 
{
 if(!m_bHaveBitmaps)
 {
  if(!m_pStdImage)
  {
   return NULL; // Load the standard image with LoadStdImage()
  }

  CBitmap bmp, *pOldBitmap;

  CRect rect;
  GetClientRect(rect);

  // do everything with mem dc
  CMemDC pDC(pScreenDC, rect);

  Gdiplus::Graphics graphics(pDC->m_hDC);

  // background
  if (m_dcBk.m_hDC == NULL)
  {

   CRect rect1;
   CClientDC clDC(GetParent());
   GetWindowRect(rect1);
   GetParent()->ScreenToClient(rect1);

   m_dcBk.CreateCompatibleDC(&clDC);
   bmp.CreateCompatibleBitmap(&clDC, rect.Width(), rect.Height());
   pOldBitmap = m_dcBk.SelectObject(&bmp);
   m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), &clDC, rect1.left, rect1.top, SRCCOPY);
   bmp.DeleteObject();
  }

  // standard image
  if (m_dcStd.m_hDC == NULL)
  {
   PaintBk(pDC);

   graphics.DrawImage(*m_pStdImage, 0, 0);
  
   m_dcStd.CreateCompatibleDC(pDC);
   bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
   pOldBitmap = m_dcStd.SelectObject(&bmp);
   m_dcStd.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
   bmp.DeleteObject();

   // standard image pressed
   if (m_dcStdP.m_hDC == NULL)
   {
    PaintBk(pDC);

    graphics.DrawImage(*m_pStdImage, 1, 1);

    m_dcStdP.CreateCompatibleDC(pDC);
    bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
    pOldBitmap = m_dcStdP.SelectObject(&bmp);
    m_dcStdP.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
    bmp.DeleteObject();
   }

   // standard image hot
   if(m_dcStdH.m_hDC == NULL)
   {
    PaintBk(pDC);

    ColorMatrix HotMat = { 1.05f, 0.00f, 0.00f, 0.00f, 0.00f,
          0.00f, 1.05f, 0.00f, 0.00f, 0.00f,
          0.00f, 0.00f, 1.05f, 0.00f, 0.00f,
          0.00f, 0.00f, 0.00f, 1.00f, 0.00f,
          0.05f, 0.05f, 0.05f, 0.00f, 1.00f };

    ImageAttributes ia;
    ia.SetColorMatrix(&HotMat);

    float width = (float)m_pStdImage->m_pBitmap->GetWidth();
    float height = (float)m_pStdImage->m_pBitmap->GetHeight();

    RectF grect; grect.X=0, grect.Y=0; grect.Width = width; grect.Height = height;

    graphics.DrawImage(*m_pStdImage, grect, 0, 0, width, height, UnitPixel, &ia);

    m_dcStdH.CreateCompatibleDC(pDC);
    bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
    pOldBitmap = m_dcStdH.SelectObject(&bmp);
    m_dcStdH.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
    bmp.DeleteObject();
   }

   // grayscale image
   if(m_dcGS.m_hDC == NULL)
   {
    PaintBk(pDC);

    ColorMatrix GrayMat = { 0.30f, 0.30f, 0.30f, 0.00f, 0.00f,
          0.59f, 0.59f, 0.59f, 0.00f, 0.00f,
          0.11f, 0.11f, 0.11f, 0.00f, 0.00f,
          0.00f, 0.00f, 0.00f, 1.00f, 0.00f,
          0.00f, 0.00f, 0.00f, 0.00f, 1.00f };

    ImageAttributes ia;
    ia.SetColorMatrix(&GrayMat);

    float width = (float)m_pStdImage->m_pBitmap->GetWidth();
    float height = (float)m_pStdImage->m_pBitmap->GetHeight();

    RectF grect; grect.X=0, grect.Y=0; grect.Width = width; grect.Height = height;

    graphics.DrawImage(*m_pStdImage, grect, 0, 0, width, height, UnitPixel, &ia);

    m_dcGS.CreateCompatibleDC(pDC);
    bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
    pOldBitmap = m_dcGS.SelectObject(&bmp);
    m_dcGS.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
    bmp.DeleteObject();
   }
  }

  // alternate image
  if( (m_dcAlt.m_hDC == NULL) && m_bHaveAltImage )
  {
   PaintBk(pDC);

   graphics.DrawImage(*m_pAltImage, 0, 0);
  
   m_dcAlt.CreateCompatibleDC(pDC);
   bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
   pOldBitmap = m_dcAlt.SelectObject(&bmp);
   m_dcAlt.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
   bmp.DeleteObject();

   // alternate image pressed
   if( (m_dcAltP.m_hDC == NULL) && m_bHaveAltImage )
   {
    PaintBk(pDC);

    graphics.DrawImage(*m_pAltImage, 1, 1);
   
    m_dcAltP.CreateCompatibleDC(pDC);
    bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
    pOldBitmap = m_dcAltP.SelectObject(&bmp);
    m_dcAltP.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
    bmp.DeleteObject();
   }

   // alternate image hot
   if(m_dcAltH.m_hDC == NULL)
   {
    PaintBk(pDC);

    ColorMatrix HotMat = { 1.05f, 0.00f, 0.00f, 0.00f, 0.00f,
          0.00f, 1.05f, 0.00f, 0.00f, 0.00f,
          0.00f, 0.00f, 1.05f, 0.00f, 0.00f,
          0.00f, 0.00f, 0.00f, 1.00f, 0.00f,
          0.05f, 0.05f, 0.05f, 0.00f, 1.00f };

    ImageAttributes ia;
    ia.SetColorMatrix(&HotMat);

    float width = (float)m_pStdImage->m_pBitmap->GetWidth();
    float height = (float)m_pStdImage->m_pBitmap->GetHeight();

    RectF grect; grect.X=0, grect.Y=0; grect.Width = width; grect.Height = height;

    graphics.DrawImage(*m_pAltImage, grect, 0, 0, width, height, UnitPixel, &ia);

    m_dcAltH.CreateCompatibleDC(pDC);
    bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
    pOldBitmap = m_dcAltH.SelectObject(&bmp);
    m_dcAltH.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
    bmp.DeleteObject();
   }
  }

  if(m_pCurBtn == NULL)
  {
   m_pCurBtn = &m_dcStd;
  }

  m_bHaveBitmaps = TRUE;
 }

 return NULL;
}

同时将绘制背景那里设置为TRUE

在需要使用的程序中直接引用这个BUTTON即可!


转载请说明出处
棋牌资源网 » VC-MFC-PNG按钮实现

发表评论

欢迎 访客 发表评论

这里有你所需要的,找专业的人做专业的事!

本站演示 联系客服