程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> 關於VC++ >> 如何對BCGControlBarPro進行換膚

如何對BCGControlBarPro進行換膚

編輯:關於VC++

效果圖

圖一

我們知道使用VC開發大型的應用系統時,都會碰到一個界面設計和風格布局的問題。如果一切都重頭開始設計和編寫的話,將會是巨大的工作量。在短時間內很難寫出一個比較健壯功能強大的界面系統出來。對軟件項目進度也帶來了不可預測的風險。在這種形勢下BCG庫就應運而生了。目前BCG可以做出諸如Visual Studio .Net 2003 ,Outlook等大型界面系統。幾乎可以滿足目前市場上絕大多數管理信息系統的界面要求。在我所看到的很多ERP,GSP等的MIS軟件公司,都在采用BCG系統。該庫非常穩定和易用。

隨著 Mircorsoft WindowsXP 系統的推出,計算機世界已經進入個性化時代。用戶對界面系統提出更高一層的要求。盡管BCG本身自帶了Skin工程,但那個工程的功能還是非常弱的。主要表現在:

不能對標題欄,滾動條和 Windows 系統標准的窗口換膚;

沒有豐富的界面元素提供下載。

本人使用Skin++庫(www.uipower.com),在BCG的例子中作了幾處改動後,BCG就擁有了動態換膚的功能。

我們在這裡以BCGCBDotNetExample為例:

1、Skin頭文件的包含在StdAfx.h中包含Skin庫的頭文件。

#include "SkinPlusPlus.h"

2、Skin庫的加載:

BOOL CBCGCBDotNetExampleApp::InitInstance()
{
  ......
  InitializeSkin(_T("XPCorona.ssk"));
  ......
}

3、讓BCG重新取下系統色:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  ..................
  -----------------
  // Create menu bar:
  //-----------------
  if (!m_wndMenuBar.Create (this))
  {
    TRACE0("Failed to create menubar\n");
    return -1; // fail to create
  }
  m_wndMenuBar.SendMessage(WM_SYSCOLORCHANGE,0,0);
  ..................
}

4、工具條圖標的透明色問題;

從CBCGPToolBar派生一個子類CMyBCGPToolBar,在該子類中覆蓋CBCGPToolBar的 virtual void DoPaint(CDC* pDCPaint)。這裡主要處理工具條圖標的透明色問題。

凡是使用CBCGPToolBar的地方全部替換成CMyBCGPToolBar。這樣你的工具條就有了和皮膚一致的皮膚色。而不是Windows的系統色。代碼如下:

#ifndef _MYTOOLBAR_H_
#define _MYTOOLBAR_H_
#include "stdafx.h"
class CMyBCGPToolBar : public CBCGPToolBar
{
public:
  virtual void DoPaint(CDC* pDCPaint)
  {
    ASSERT_VALID(this);
    ASSERT_VALID(pDCPaint);
    CRect rectClip;
    pDCPaint->GetClipBox (rectClip);
    BOOL bHorz = GetCurrentAlignment () & CBRS_ORIENT_HORZ ? TRUE : FALSE;
    CRect rectClient;
    GetClientRect (rectClient);
    CDC* pDC = pDCPaint;
    BOOL m_bMemDC = FALSE;
    CDC dcMem;
    CBitmap bmp;
    CBitmap* pOldBmp = NULL;
    if (dcMem.CreateCompatibleDC (pDCPaint) &&
      bmp.CreateCompatibleBitmap (pDCPaint, rectClient.Width (),
      rectClient.Height ()))
    {
      m_bMemDC = TRUE;
      pOldBmp = dcMem.SelectObject (&bmp);
      pDC = &dcMem;
      if ((GetStyle () & TBSTYLE_TRANSPARENT) == 0)
      {
        CBCGPVisualManager::GetInstance ()->OnFillBarBackground (pDC, this,
          rectClient, rectClip);
      }
    }
    OnFillBackground (pDC);
    pDC->SetTextColor (globalData.clrBtnText);
    pDC->SetBkMode (TRANSPARENT);
    CRect rect;
    GetClientRect(rect);
    if (bHorz)
    {
      rect.bottom = rect.top + GetRowHeight ();
    }
    else
    {
      rect.right = rect.left + GetColumnWidth ();
    }
    CBCGPToolBarImages* pImages = GetImageList
      (m_Images, m_ImagesLocked, m_LargeImages, m_LargeImagesLocked);
    CBCGPToolBarImages* pHotImages = pImages;
    CBCGPToolBarImages* pColdImages = GetImageList(m_ColdImages,
       m_ColdImagesLocked, m_LargeColdImages,m_LargeColdImagesLocked);
    CBCGPToolBarImages* pDisabledImages = GetImageList(m_DisabledImages,
       m_DisabledImagesLocked, m_LargeDisabledImages,m_LargeDisabledImagesLocked);
    CBCGPToolBarImages* pMenuImages = !m_bLocked ?
      &m_MenuImages : &m_MenuImagesLocked;
    CBCGPToolBarImages* pDisabledMenuImages = !m_bLocked ?
      &m_DisabledMenuImages : &m_DisabledMenuImagesLocked;
    BOOL bDrawImages = pImages->IsValid ();
    //globalData.clrBtnFace
    pHotImages->SetTransparentColor(GetDefaultSysColor(COLOR_BTNFACE));
    BOOL bFadeInactiveImages = CBCGPVisualManager::GetInstance ()->IsFadeInactiveImage ();
    CBCGPDrawState ds;
    if (bDrawImages &&
      !pHotImages->PrepareDrawImage (ds,
      m_bMenuMode ? m_sizeMenuImage : GetImageSize (),
      bFadeInactiveImages))
    {
      return;
    }
    CFont* pOldFont;
    if (bHorz)
    {
      pOldFont = (CFont*) pDC->SelectObject (&globalData.fontRegular);
    }
    else
    {
      pOldFont = (CFont*) pDC->SelectObject (&globalData.fontVert);
    }
    if (pColdImages->GetCount () > 0)
    {
      CBCGPVisualManager::GetInstance ()->SetFadeInactiveImage (FALSE);
    }
    if (pColdImages->GetCount ())
    {
      CBCGPVisualManager::GetInstance ()->SetFadeInactiveImage (FALSE);
    }
    int iButton = 0;
    for (POSITION pos = m_Buttons.GetHeadPosition (); pos != NULL; iButton ++)
    {
      CBCGPToolbarButton* pButton = (CBCGPToolbarButton*) m_Buttons.GetNext (pos);
      if (pButton == NULL)
      {
        break;
      }
      ASSERT_VALID (pButton);
      rect = pButton->Rect ();
      CRect rectInter;
      if (pButton->m_nStyle & TBBS_SEPARATOR)
      {
        BOOL bHorzSeparator = bHorz;
        CRect rectSeparator; rectSeparator.SetRectEmpty ();
        OnCalcSeparatorRect (pButton, rectSeparator, bHorz);
        if (pButton->m_bWrap && bHorz)
        {
          bHorzSeparator = FALSE;
        }
        if (rectInter.IntersectRect (rectSeparator, rectClip) && !pButton->IsHidden())
        {
          DrawSeparator (pDC, rectSeparator, bHorzSeparator);
        }
        continue;
      }
      if (!rectInter.IntersectRect (rect, rectClip))
      {
        continue;
      }
      BOOL bHighlighted = FALSE;
      BOOL bDisabled = (pButton->m_nStyle & TBBS_DISABLED) && !IsCustomizeMode ();
      if (IsCustomizeMode () && !m_bLocked)
      {
        bHighlighted = FALSE;
      }
      else
      {
        if (m_bMenuMode)
        {
          bHighlighted = (iButton == m_iHighlighted);
        }
        else
        {
          bHighlighted = ((iButton == m_iHighlighted ||
            iButton == m_iButtonCapture) &&
            (m_iButtonCapture == -1 ||
            iButton == m_iButtonCapture));
        }
      }
      if (pDC->RectVisible(&rect))
      {
        BOOL bDrawDisabledImages = FALSE;
        if (bDrawImages)
        {
          CBCGPToolBarImages* pNewImages = NULL;
          if (pButton->m_bUserButton)
          {
            if (pButton->GetImage () >= 0)
            {
              pNewImages = m_pUserImages;
            }
          }
          else
          {
            if (m_bMenuMode)
            {
              if (bDisabled && pDisabledMenuImages->GetCount () > 0)
              {
                bDrawDisabledImages = TRUE;
                pNewImages = pDisabledMenuImages;
              }
              else if (pMenuImages->GetCount () > 0)
              {
                pNewImages = pMenuImages;
              }
              else
              {
                bDrawDisabledImages =
                  (bDisabled && pDisabledImages->GetCount () > 0);
                pNewImages = bDrawDisabledImages ? pDisabledImages : pHotImages;
              }
            }
            else // Toolbar mode
            {
              bDrawDisabledImages =
                (bDisabled && pDisabledImages->GetCount () > 0);
              pNewImages = bDrawDisabledImages ? pDisabledImages : pHotImages;
              if (!bHighlighted && !bDrawDisabledImages &&
                (pButton->m_nStyle & TBBS_PRESSED) == 0 &&
                pColdImages->GetCount () > 0 &&
                !pButton->IsDroppedDown ())
              {
                pNewImages = pColdImages;
              }
            }
          }
          if (bDrawImages && pNewImages != pImages && pNewImages != NULL)
          {
            pImages->EndDrawImage (ds);
            pNewImages->SetTransparentColor (globalData.clrBtnFace);
            pNewImages->PrepareDrawImage (ds,
              m_bMenuMode ? m_sizeMenuImage : GetImageSize (), bFadeInactiveImages);
            pImages = pNewImages;
          }
        }
        DrawButton (pDC, pButton, bDrawImages ? pImages : NULL,
          bHighlighted, bDrawDisabledImages);
      }
    }
    //-------------------------------------------------------------
    // Highlight selected button in the toolbar customization mode:
    //-------------------------------------------------------------
    if (m_iSelected >= m_Buttons.GetCount ())
    {
      m_iSelected = -1;
    }
    if (IsCustomizeMode () && m_iSelected >= 0 && !m_bLocked &&
      m_pSelToolbar == this)
    {
      CBCGPToolbarButton* pSelButton = GetButton (m_iSelected);
      ASSERT (pSelButton != NULL);
      if (pSelButton != NULL && pSelButton->CanBeStored ())
      {
        CRect rectDrag1 = pSelButton->Rect ();
        if (pSelButton->GetHwnd () != NULL)
        {
          rectDrag1.InflateRect (0, 1);
        }
        pDC->Draw3dRect(&rectDrag1, globalData.clrBtnText, globalData.clrBtnText);
        rectDrag1.DeflateRect (1, 1);
        pDC->Draw3dRect(&rectDrag1, globalData.clrBtnText, globalData.clrBtnText);
      }
    }
    if (IsCustomizeMode () && m_iDragIndex >= 0 && !m_bLocked)
    {
      DrawDragMarker (pDC);
    }
    pDC->SelectObject (pOldFont);
    if (bDrawImages)
    {
      pImages->EndDrawImage (ds);
    }
    if (m_bMemDC)
    {
      //--------------------------------------
      // Copy the results to the on-screen DC:
      //--------------------------------------
      pDCPaint->BitBlt (rectClip.left, rectClip.top, rectClip.Width(), rectClip.Height(),
        &dcMem, rectClip.left, rectClip.top, SRCCOPY);
      dcMem.SelectObject(pOldBmp);
    }
    CBCGPVisualManager::GetInstance ()->SetFadeInactiveImage (bFadeInactiveImages);
  }
};
#endif //_MYTOOLBAR_H_

5、Skin 庫的釋放:

int CBCGCBDotNetExampleApp::ExitInstance()
{
  ExitSkin();
  .......
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved