程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> vc教程 >> 怎樣實現動畫背景旗幟

怎樣實現動畫背景旗幟

編輯:vc教程

以下代碼是關於用類CmdiMainClIEnt來顯示動畫背景旗幟的一個例子。原理是很簡單的,我們只需要一系列連續的位圖,並且讓它連續顯示。在這個例子中你可以用鼠標的右鍵來開始/停止這個動畫,如果點左鍵的話,就可以顯示一個子窗口,來保持動畫是繼續。為了你的方便,你可以把代碼直接從浏覽器中拷貝下去,並且插到你的代碼中就行了。以下是代碼:

#include "stdafx.h"
#include "banner.h"
#include "mainfrm.h"

#include "resource.h"

#ifdef _DEBUG
  #undef THIS_FILE
  static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMdiMainClIEnt

CMdiMainClient::CMdiMainClient(): m_sizeClIEnt(0, 0)

{
  VERIFY(m_bmpBackGround.LoadBitmap(IDB_BCKGRD));
  ASSERT(m_bmpBackGround.GetSafeHandle());

  VERIFY(m_bmpBackGround2.LoadBitmap(IDB_BITMAP0));
  ASSERT(m_bmpBackGround2.GetSafeHandle());

  BmpLoaded = 1;
  StopRotation = FALSE;
}

CMdiMainClient::~CMdiMainClIEnt()
{
  if (m_bmpBackGround.GetSafeHandle())
    m_bmpBackGround.DeleteObject();

  if (m_bmpBackGround2.GetSafeHandle())
    m_bmpBackGround2.DeleteObject();
}


BEGIN_MESSAGE_MAP(CMdiMainClIEnt, CWnd)
//{{AFX_MSG_MAP(CMdiMainClIEnt)
// NOTE - the ClassWizard will add and remove mapping Macros here.
ON_WM_TIMER()ON_WM_ERASEBKGND()ON_WM_RBUTTONDOWN()ON_WM_LBUTTONDOWN()ON_WM_SIZE
  ()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CMdiMainClIEnt message handlers
BOOL CMdiMainClIEnt::OnEraseBkgnd(CDC *pDC)
{
  // Erase only the area needed
  CRect rect;
  GetClIEntRect(&rect);

  SetTimer(0, 300, NULL);

  // Place the bitmap
  BITMAP bm;
  CDC dcMem;

  BITMAP bm2;
  CDC dcMem2;

VERIFY(m_bmpBackGround.GetObject(sizeof(bm), (LPVOID) &bm));
  dcMem.CreateCompatibleDC(NULL);
  CBitmap *pbmpOld = (CBitmap*)dcMem.SelectObject(&m_bmpBackGround);
  ASSERT(pbmpOld);
  // Display red background
  pDC->StretchBlt(0, 0, rect.right, rect.bottom, &dcMem, 0, 0, bm.bmWidth,
    bm.bmHeight, SRCCOPY);

  VERIFY(m_bmpBackGround2.GetObject(sizeof(bm2), (LPVOID) &bm2));
  dcMem2.CreateCompatibleDC(NULL);
  dcMem2.SelectObject(&m_bmpBackGround2);
  // Display picture background
  pDC->StretchBlt(200, 100, 400, 200, &dcMem2, 0, 0, bm2.bmWidth, bm2.bmHeight,
    SRCCOPY);

  // Display Instructions
  pDC->SetBkColor(RGB(255, 0, 0));
  pDC->SetTextColor(RGB(192, 192, 192));
  pDC->TextOut(480, 500, "Mouse Right Click to Stop/Start Special Effects");
  pDC->TextOut(480, 515, "Mouse Left-Click to Show Child Window");

  // CleanUp
  dcMem.SelectObject(pbmpOld);

  return TRUE;
}

////////////////////////////////////////////////////////////////////
//
void CMdiMainClIEnt::OnSize(UINT nType, int cx, int cy)
{
  Default();
  // if the app is just starting up, save the window
  // dimensions and get out

  if ((m_sizeClient.cx == 0) && (m_sizeClIEnt.cy == 0))
  {
    m_sizeClIEnt.cx = cx;
    m_sizeClIEnt.cy = cy;

    return ;
  }

  // if the size hasn't changed, break and pass to default

  if ((m_sizeClient.cx == cx) && (m_sizeClIEnt.cy == cy))
  {
    return ;
  }

  // window size has changed so save new dimensions and force
  // entire background to redraw, including icon backgrounds

  m_sizeClIEnt.cx = cx;
  m_sizeClIEnt.cy = cy;

  RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ERASENOW |
    RDW_ALLCHILDREN);

  return ;
}

void CMdiMainClIEnt::OnTimer(UINT nIDEvent)
{
  DisplayNextLogoFrame();
  CWnd::OnTimer(nIDEvent);
}


void CMdiMainClIEnt::DisplayNextLogoFrame()
{
  CRect rect;
  GetClIEntRect(&rect);

  BITMAP bm2;
  CDC dcMem2;

  CDC *pDC = GetDC();

  if (!StopRotation)
  {
    // Select next logo frame to be displayed
    switch (BmpLoaded)
    {
      case 1:
        if (m_bmpBackGround2.GetSafeHandle())
          m_bmpBackGround2.DeleteObject();
        VERIFY(m_bmpBackGround2.LoadBitmap(IDB_BITMAP1));
        ASSERT(m_bmpBackGround2.GetSafeHandle());
        break;
      case 2:
        if (m_bmpBackGround2.GetSafeHandle())
          m_bmpBackGround2.DeleteObject();
        VERIFY(m_bmpBackGround2.LoadBitmap(IDB_BITMAP2));
        ASSERT(m_bmpBackGround2.GetSafeHandle());
        break;
      case 3:
        if (m_bmpBackGround2.GetSafeHandle())
          m_bmpBackGround2.DeleteObject();
        VERIFY(m_bmpBackGround2.LoadBitmap(IDB_BITMAP3));
        ASSERT(m_bmpBackGround2.GetSafeHandle());
        break;
      case 4:
        if (m_bmpBackGround2.GetSafeHandle())
          m_bmpBackGround2.DeleteObject();
        VERIFY(m_bmpBackGround2.LoadBitmap(IDB_BITMAP4));
        ASSERT(m_bmpBackGround2.GetSafeHandle());
        break;
      case 5:
        if (m_bmpBackGround2.GetSafeHandle())
          m_bmpBackGround2.DeleteObject();
        VERIFY(m_bmpBackGround2.LoadBitmap(IDB_BITMAP5));
        ASSERT(m_bmpBackGround2.GetSafeHandle());
        break;
      case 6:
        if (m_bmpBackGround2.GetSafeHandle())
          m_bmpBackGround2.DeleteObject();
        VERIFY(m_bmpBackGround2.LoadBitmap(IDB_BITMAP6));
        ASSERT(m_bmpBackGround2.GetSafeHandle());
        break;
      case 7:
        if (m_bmpBackGround2.GetSafeHandle())
          m_bmpBackGround2.DeleteObject();
        VERIFY(m_bmpBackGround2.LoadBitmap(IDB_BITMAP7));
        ASSERT(m_bmpBackGround2.GetSafeHandle());
        break;
      case 8:
        if (m_bmpBackGround2.GetSafeHandle())
          m_bmpBackGround2.DeleteObject();
        VERIFY(m_bmpBackGround2.LoadBitmap(IDB_BITMAP8));
        ASSERT(m_bmpBackGround2.GetSafeHandle());
        break;
      case 9:
        if (m_bmpBackGround2.GetSafeHandle())
          m_bmpBackGround2.DeleteObject();
        VERIFY(m_bmpBackGround2.LoadBitmap(IDB_BITMAP9));
        ASSERT(m_bmpBackGround2.GetSafeHandle());
        break;

    }
    // Change Bitmap index
    if (BmpLoaded == 9)
      BmpLoaded = 0;
    else
      BmpLoaded++;
  }

  VERIFY(m_bmpBackGround2.GetObject(sizeof(bm2), (LPVOID) &bm2));
  dcMem2.CreateCompatibleDC(NULL);
  dcMem2.SelectObject(&m_bmpBackGround2);
  pDC->StretchBlt(200, 100, 400, 200, &dcMem2, 0, 0, bm2.bmWidth, bm2.bmHeight,
    SRCCOPY);

  ReleaseDC(pDC);
}

void CMdiMainClIEnt::OnRButtonDown(UINT nFlags, CPoint point)
{
  if (StopRotation)
    StopRotation = FALSE;
  else
    StopRotation = TRUE;

  CWnd::OnRButtonDown(nFlags, point);
}

void CMdiMainClIEnt::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_Hide_Logo = TRUE;
  KillTimer(0);

  // Display Application
  ((CBannerApp*)AfxGetApp())->pDocTemplate->OpenDocumentFile(NULL);

  CWnd::OnLButtonDown(nFlags, point);
}

// MdiMainClIEnt.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CMdiMainClIEnt window

extern BOOL m_Hide_Logo;

class CMdiMainClIEnt: public CWnd
{
    // Construction
  public:
    CMdiMainClIEnt();

    // Attributes
  protected:
    CBitmap m_bmpBackGround2;
    CBitmap m_bmpBackGround;

    int BmpLoaded;
  public:
    BOOL StopRotation;
    CSize m_sizeClIEnt;

    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMdiMainClIEnt)
    //}}AFX_VIRTUAL

    // Implementation
  public:
    void Display_Blank_Bitmap();
    void DisplayNextLogoFrame();
    virtual ~CMdiMainClIEnt();
    // Generated message map functions
  protected:
    //{{AFX_MSG(CMdiMainClIEnt)
    // NOTE - the ClassWizard will add and remove member functions here.
    afx_msg BOOL OnEraseBkgnd(CDC *pDC);
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg void OnTimer(UINT nIDEvent);
    afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

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