发信人: skyice() 
整理人: mrcloud(2000-08-24 02:23:16), 站内信件
 | 
 
 
1)创建一个用来绘制标识符的普通窗口
   用 Class Wizard 创建一个来自 generic CWnd 的通用窗口。
   在该通用窗口中,用后面提供的位图类嵌入一个位图变量,
 如下所示:
     CIceBitmap m_bitmap;
     在该类中添加一个 CreateBX() 成员函数、该函数将装入位图标识符,并
 同时在工具栏中创建一个实际的窗口:
 void CIceLogo::CreateBX(CWnd *pWnd,UINT nBitmapID,UINT nChildID)
 {
     m_bitmap.LoadBitmapEx(nBitmapID,TRUE);
     CRect rect(0,0,0,0);
     Create(NULL,_T(""),WS_CHILD|WS_VISIBLE,rect,pWnd,nChildID);
 }
   添加另一个成员函数,该函数使得工具栏可以移动窗口:
 void CIceLogo::MoveLogo( int nWidth, int nHeight )
 {
     MoveWindow(nWidth-m_bitmap.m_Width,0,
         m_bitmap.m_Width,nHeight);
 }
   应用 Class Wizard 添加一个在窗口中绘制位图的 WM_PAINT 消息处理
 函数句柄,并以此来结束该类:
 void CIceLogo::OnPaint() 
 {
     CPaintDC dc(this); // device context for painting
 
     // get bitmap colors
     CPalette *pOldPal = dc.SelectPalette(
                         m_bitmap.GetPalette(),FALSE);
     dc.RealizePalette();
 
     // get device context to select bitmap into
     CDC dcComp;
     dcComp.CreateCompatibleDC(&dc);
     dcComp.SelectObject(&m_bitmap);
 
     // draw bitmap
     dc.BitBlt(0,0,m_bitmap.m_Width,m_bitmap.m_Height,
               &dcComp, 0,0,SRCCOPY);
 
     // reselect old palette
     dc.SelectPalette(pOldPal,FALSE);
 }
 2)创建一个包含普通窗口类的自定义工具栏
   使用 Class Wizard 从CToolBarCtrl中创建一个新类。编辑所有生成
 的 .h 和 .cpp 文件,以修改所有从 CToolBarCtrl 到 CToolBar 的引用
 (ClassWizard 不允许从 CToolBar 类派生一个类)。
   在新的工具栏中嵌入新的普通窗口类,如下所示:
   CIceLogo m_Logo;
   使用 Class Wizard 给工具栏添加一个 WM_CREATE 消息处理函数句柄。
 用该句柄调用普通窗口类的 CreatBX() 函数:
 int CIceToolbar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
 {
     if (CToolBar::OnCreate(lpCreateStruct) == -1)
         return -1;
     
     m_Logo.CreateBX(this,IDB_LOGO,-1);
     
     return 0;
 }
   再一次使用 Class Wizard 来添加一个 WM_SIZE 消息处理函数,这里要
 调用普通窗口类的 MoveLogo() 函数:
 void CIceToolbar::OnSize(UINT nType, int cx, int cy) 
 {
     CToolBar::OnSize(nType, cx, cy);
     
     m_Logo.MoveLogo(cx,cy);
 }
 3)在应用程序中使用新的工具栏类
   现在,在 MainFrm.h 文件中用新的工具栏类替换 CToolBar:
   // change CToolBar to CIceToolBar
   CIceToolbar m_wndToolBar;
   不幸的是,本实例对可停靠的工具栏就不起作用了。这是因为可停靠的工
 具栏仅仅与它所包括的最后一个按钮长度相同。而该实例却依赖于具有大量额
 外空间的工具栏、以便在结尾处粘贴普通窗口。因此必须在注释工具栏处将其
 设为非停靠式(un-dockable),或者在 CmainFrame::OnCreate() 中删除
 以下各列:
 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
 {
 ......
 
 //    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
 //    EnableDocking(CBRS_ALIGN_ANY);
 //    DockControlBar(&m_wndToolBar);
 
     return 0;
 }
 
 
 附:类的源代码
 
 // IceBtmap.cpp : implementation of the CIceBitmap class
 //
 
 #include "stdafx.h"
 #include "IceBtmap.h"
 
 //////////////////////////////////////////////////////////////
 // CIceBitmap
 
 IMPLEMENT_DYNAMIC(CIceBitmap, CBitmap)
 
 
 CIceBitmap::CIceBitmap()
 {
     m_pPalette=NULL;
 }
 
 CIceBitmap::~CIceBitmap()
 {
     if (m_pPalette)
     {
         delete m_pPalette;
     }
 }
 
 void CIceBitmap::LoadBitmapEx(UINT nID, BOOL bTransparent )
 {
 // can only load once
     ASSERT(!m_pPalette);
 
     CDC dcScreen;
     dcScreen.Attach(::GetDC(NULL));
 
 // find and lock bitmap resource
     HRSRC hRsrc = FindResource(AfxGetResourceHandle(),
                                MAKEINTRESOURCE(nID),RT_BITMAP);
     HGLOBAL hglb = LoadResource(AfxGetResourceHandle(), hRsrc);
     LPBITMAPINFOHEADER lpBitmap = (
               LPBITMAPINFOHEADER)LockResource(hglb);
 
 // get pointers into bitmap structures (
 //   header, color table and picture bits)
     LPBITMAPINFO pBitmapInfo = (LPBITMAPINFO)lpBitmap;
     LPBITMAPINFOHEADER pBitmapInfoHeader = (
                   LPBITMAPINFOHEADER)lpBitmap;
     // if the picture data uses more then 8 bits per pixel,
     // there's no color table to turn into a palette
     int nNumberOfColors=0;
     if (lpBitmap->biClrUsed)
         nNumberOfColors = lpBitmap->biClrUsed;
     else if (pBitmapInfoHeader->biBitCount <= 8)
            nNumberOfColors = (1<<pBitmapInfoHeader->biBitCount);
     LPBYTE pBitmapPictureData = (
                     LPBYTE)lpBitmap+lpBitmap->biSize+
                     (nNumberOfColors*sizeof(RGBQUAD));
 
     // get width and height
     m_Width = lpBitmap->biWidth;
     m_Height = lpBitmap->biHeight;
 
 // create a logical palette from the color table in this bitmap 
     if (nNumberOfColors)
     {
         LOGPALETTE *pLogPal = (LOGPALETTE *)new BYTE[
             sizeof(LOGPALETTE) + (nNumberOfColors * 
             sizeof(PALETTEENTRY))];
         pLogPal->palVersion    = 0x300;
         pLogPal->palNumEntries = nNumberOfColors;
 
         for (int i = 0;  i < nNumberOfColors;  i++)
         {
 //if flag set, replace grey color with window's background color
             if (bTransparent && 
                     pBitmapInfo->bmiColors[i].rgbRed==192 &&
                     pBitmapInfo->bmiColors[i].rgbGreen==192 &&
                     pBitmapInfo->bmiColors[i].rgbBlue==192)
             {
                 pBitmapInfo->bmiColors[i].rgbRed=  GetRValue(
                              ::GetSysColor(COLOR_BTNFACE));
                 pBitmapInfo->bmiColors[i].rgbGreen=GetGValue(
                              ::GetSysColor(COLOR_BTNFACE));
                 pBitmapInfo->bmiColors[i].rgbBlue= GetBValue(
                              ::GetSysColor(COLOR_BTNFACE));
             }
             pLogPal->palPalEntry[i].peRed   = 
                              pBitmapInfo->bmiColors[i].rgbRed;
             pLogPal->palPalEntry[i].peGreen = 
                              pBitmapInfo->bmiColors[i].rgbGreen;
             pLogPal->palPalEntry[i].peBlue  = 
                              pBitmapInfo->bmiColors[i].rgbBlue;
             pLogPal->palPalEntry[i].peFlags = 0;
         }
         m_pPalette=new CPalette;
         m_pPalette->CreatePalette(pLogPal);
         delete []pLogPal;
         dcScreen.SelectPalette(m_pPalette,TRUE);
         dcScreen.RealizePalette();
     }
     
 // create device dependant bitmap
     HBITMAP bitmap = ::CreateDIBitmap(dcScreen.m_hDC, 
            pBitmapInfoHeader, CBM_INIT, pBitmapPictureData, 
            pBitmapInfo, DIB_RGB_COLORS);
 
 // attach this new bitmap object to our CBitmap class
     Attach(bitmap);
 
 // release dc
     ::ReleaseDC(NULL, dcScreen.Detach());
 }
 
 
 
 #ifndef ICEBITMAP_H
 #define ICEBITMAP_H
 
 // IceBtmap.h  head file
 //
 class CIceBitmap : public CBitmap
 {
 public:
     DECLARE_DYNAMIC(CIceBitmap)
 
 // Constructors
     CIceBitmap();
 
     void LoadBitmapEx(UINT nID, BOOL bIconBkgrd );
     CPalette *GetPalette(){return m_pPalette;};
 
 // Implementation
 public:
     virtual ~CIceBitmap();
 
 // Attributes
     int    m_Width;
     int m_Height;
 // Operations
 
 private:
     CPalette *m_pPalette;
 };
 #endif
 
 
 // IceLogo.cpp : implementation file
 //
 
 #include "stdafx.h"
 #include "IceLogo.h"
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #undef THIS_FILE
 static char THIS_FILE[] = __FILE__;
 #endif
 
 ////////////////////////////////////////////////////////
 // CIceLogo
 
 CIceLogo::CIceLogo()
 {
 }
 
 CIceLogo::~CIceLogo()
 {
 }
 
 
 BEGIN_MESSAGE_MAP(CIceLogo, CWnd)
     //{{AFX_MSG_MAP(CIceLogo)
     ON_WM_PAINT()
     //}}AFX_MSG_MAP
 END_MESSAGE_MAP()
 
 
 //////////////////////////////////////////////////////////
 // CIceLogo message handlers
 
 void CIceLogo::OnPaint() 
 {
     CPaintDC dc(this); // device context for painting
 
     // get bitmap colors
     CPalette *pOldPal = dc.SelectPalette(
                         m_bitmap.GetPalette(),FALSE);
     dc.RealizePalette();
 
     // get device context to select bitmap into
     CDC dcComp;
     dcComp.CreateCompatibleDC(&dc);
     dcComp.SelectObject(&m_bitmap);
 
     // draw bitmap
     dc.BitBlt(0,0,m_bitmap.m_Width,m_bitmap.m_Height,
               &dcComp, 0,0,SRCCOPY);
 
     // reselect old palette
     dc.SelectPalette(pOldPal,FALSE);
 }
 
 void CIceLogo::CreateBX(CWnd *pWnd,UINT nBitmapID,UINT nChildID)
 {
     m_bitmap.LoadBitmapEx(nBitmapID,TRUE);
     CRect rect(0,0,0,0);
     Create(NULL,_T(""),WS_CHILD|WS_VISIBLE,rect,pWnd,nChildID);
 }
 
 
 void CIceLogo::MoveLogo( int nWidth, int nHeight )
 {
     MoveWindow(nWidth-m_bitmap.m_Width,0,
                m_bitmap.m_Width,nHeight);
 }
 
 
 
 #if !defined ICELOGO_H
 #define ICELOGO_H
 
 // IceLogo.h : header file
 //
 #include "IceBtmap.h"
 /////////////////////////////////////////////////////////
 // CIceLogo window
 
 class CIceLogo : public CWnd
 {
 // Construction
 public:
     CIceLogo();
 
 // Attributes
 public:
 
 // Operations
 public:
     void CreateBX(CWnd *pWnd, UINT nBitmapID, UINT nChildID );
     void MoveLogo( int nWidth, int nHeight );
 
 // Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CIceLogo)
     //}}AFX_VIRTUAL
 
 // Implementation
 public:
     virtual ~CIceLogo();
 
     // Generated message map functions
 protected:
     //{{AFX_MSG(CIceLogo)
     afx_msg void OnPaint();
     //}}AFX_MSG
     DECLARE_MESSAGE_MAP()
 private:
     CIceBitmap m_bitmap;
 };
 
 //////////////////////////////////////////////////////////
 
 #endif
 
 
 // IceTlbar.cpp : implementation file
 //
 
 #include "stdafx.h"
 #include "IceTlbar.h"
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #undef THIS_FILE
 static char THIS_FILE[] = __FILE__;
 #endif
 
 //////////////////////////////////////////////////////////
 // CIceToolbar
 
 CIceToolbar::CIceToolbar()
 {
 }
 
 CIceToolbar::~CIceToolbar()
 {
 }
 
 
 BEGIN_MESSAGE_MAP(CIceToolbar, CToolBar)
     //{{AFX_MSG_MAP(CIceToolbar)
     ON_WM_SIZE()
     ON_WM_CREATE()
     //}}AFX_MSG_MAP
 END_MESSAGE_MAP()
 
 //////////////////////////////////////////////////////////
 // CIceToolbar message handlers
 
 int CIceToolbar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
 {
     if (CToolBar::OnCreate(lpCreateStruct) == -1)
         return -1;
     
     m_Logo.CreateBX(this,IDB_LOGO,-1);
     
     return 0;
 }
 
 void CIceToolbar::OnSize(UINT nType, int cx, int cy) 
 {
     CToolBar::OnSize(nType, cx, cy);
     
     m_Logo.MoveLogo(cx,cy);
 }
 
 
 
 #if !defined ICETOOLBAR_H
 #define ICETOOLBAR_H
 
 // IceTlbar.h : header file
 //
 #include "IceLogo.h"
 ////////////////////////////////////////////////////////
 // CIceToolbar window
 
 class CIceToolbar : public CToolBar
 {
 // Construction
 public:
     CIceToolbar();
 
 // Attributes
 public:
 
 // Operations
 public:
 
 // Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CIceToolbar)
     //}}AFX_VIRTUAL
 
 // Implementation
 public:
     virtual ~CIceToolbar();
 
     // Generated message map functions
 protected:
     //{{AFX_MSG(CIceToolbar)
     afx_msg void OnSize(UINT nType, int cx, int cy);
     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
     //}}AFX_MSG
 
     DECLARE_MESSAGE_MAP()
 private:
     CIceLogo m_Logo;
 };
 
 ////////////////////////////////////////////////////////////
 
 #endif
  -- ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.99.93.3]
  | 
 
 
 |