示例如下:
    (1)先用appwizards生成一个MFC的Dialog应用模板,假定对话框类为CTest01Dlg。
    (2)再添入两个oicq的bmp文件到资源中去
    (3)添加一个按钮(button)到对话框上.用作启动、停止动画的button
    (4)用ClassWizard为button/onclick及dlg/ontimer生成事件响应函数,
    (5)用Resource Symbol加入一个标识定义IDC_TIMER1
    (6)在ClassView中为CTest01Dlg加入以下成员变量和成员函数
      CriticalSection ccs;
      CBitmap bm[2];
      CWinThread* pMyThread;
      static UINT MyThreadProc( LPVOID pParam);
      void DisplayFace(CPoint r);
      实现文件中加入相应代码(见下面)
    (7)stdafx.h中加入#include 
    源代码如下,凡是我新加的代码周围都有注释包围,其它是ClassWizards自动写的:
    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    file://
     #if !defined(AFX_STDAFX_H__5B92DAA8_FE27_4702_8037_A2538343E69D__INCLUDED_)
     #define AFX_STDAFX_H__5B92DAA8_FE27_4702_8037_A2538343E69D__INCLUDED_
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
    #include // MFC core and standard components
    #include // MFC extensions
    #include // MFC support for Internet Explorer 4 Common Controls
    file://加入头引用主要是CCriticalSection对象的定义.
    #include 
    file://加入结束
    #ifndef _AFX_NO_AFXCMN_SUPPORT
    #include // MFC support for Windows Common Controls
    #endif // _AFX_NO_AFXCMN_SUPPORT
    file://{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    #endif // !defined(AFX_STDAFX_H__5B92DAA8_FE27_4702_8037_A2538343E69D__INCLUDED_)
    // test01Dlg.h : header file
    file://
    #if !defined(AFX_TEST01DLG_H__F3780E23_CCFC_468C_A262_50FFF1D991BC__INCLUDED_)
    #define AFX_TEST01DLG_H__F3780E23_CCFC_468C_A262_50FFF1D991BC__INCLUDED_
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    /////////////////////////////////////////////////////////////////////////////
    // CTest01Dlg dialog
    class CTest01Dlg : public CDialog
    {
     // Construction
     public:
     file://加入
     CBitmap bm[2];
     CCriticalSection ccs;
     CWinThread* pMyThread;
     static UINT MyThreadProc( LPVOID pParam);
     void DisplayFace(CPoint r);
     CTest01Dlg(CWnd* pParent = NULL); // standard constructor
     file://加入结束
     // Dialog Data
     file://{{AFX_DATA(CTest01Dlg)
     enum { IDD = IDD_TEST01_DIALOG };
     // NOTE: the ClassWizard will add data members here
     file://}}AFX_DATA
     // ClassWizard generated virtual function overrides
     file://{{AFX_VIRTUAL(CTest01Dlg)
     protected:
     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
     file://}}AFX_VIRTUAL
     // Implementation
     protected:
      HICON m_hIcon;
      // Generated message map functions
      file://{{AFX_MSG(CTest01Dlg)
      virtual BOOL OnInitDialog();
      afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
      afx_msg void OnPaint();
      afx_msg HCURSOR OnQueryDragIcon();
      afx_msg void OnButton1();
      afx_msg void OnTimer(UINT nIDEvent);
     file://}}AFX_MSG
     DECLARE_MESSAGE_MAP()
     };
     file://{{AFX_INSERT_LOCATION}}
     // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
     #endif // !defined(AFX_TEST01DLG_H__F3780E23_CCFC_468C_A262_50FFF1D991BC__INCLUDED_)
     // test01Dlg.cpp : implementation file
     file://
     #include "stdafx.h"
     #include "test01.h"
     #include "test01Dlg.h"
     #ifdef _DEBUG
     #define new DEBUG_NEW
     #undef THIS_FILE
     static char THIS_FILE[] = __FILE__;
     #endif
     /////////////////////////////////////////////////////////////////////////////
     // CAboutDlg dialog used for App About
     class CAboutDlg : public CDialog
      {
       public:
       CAboutDlg();
       // Dialog Data
       file://{{AFX_DATA(CAboutDlg)
       enum { IDD = IDD_ABOUTBOX };
       file://}}AFX_DATA
     // ClassWizard generated virtual function overrides
     file://{{AFX_VIRTUAL(CAboutDlg)
     protected:
     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
     file://}}AFX_VIRTUAL
     // Implementation
     protected:
     file://{{AFX_MSG(CAboutDlg)
     file://}}AFX_MSG
     DECLARE_MESSAGE_MAP()
     };
     CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
     {
      file://{{AFX_DATA_INIT(CAboutDlg)
      file://}}AFX_DATA_INIT
     }
     void CAboutDlg::DoDataExchange(CDataExchange* pDX)
      {
       CDialog::DoDataExchange(pDX);
       file://{{AFX_DATA_MAP(CAboutDlg)
       file://}}AFX_DATA_MAP
      }
    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    file://{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    file://}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // CTest01Dlg dialog
    CTest01Dlg::CTest01Dlg(CWnd* pParent /*=NULL*/)
    : CDialog(CTest01Dlg::IDD, pParent)
     {
      file://{{AFX_DATA_INIT(CTest01Dlg)
      // NOTE: the ClassWizard will add member initialization here
      file://}}AFX_DATA_INIT
      // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
      m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
      file://加入
      pMyThread =NULL;
      file://加入结束
     }
    void CTest01Dlg::DoDataExchange(CDataExchange* pDX)
    {
     CDialog::DoDataExchange(pDX);
     file://{{AFX_DATA_MAP(CTest01Dlg)
     // NOTE: the ClassWizard will add DDX and DDV calls here
     file://}}AFX_DATA_MAP
    }
    BEGIN_MESSAGE_MAP(CTest01Dlg, CDialog)
     file://{{AFX_MSG_MAP(CTest01Dlg)
      ON_WM_SYSCOMMAND()
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
      ON_WM_TIMER()
     file://}}AFX_MSG_MAP
     END_MESSAGE_MAP()
     /////////////////////////////////////////////////////////////////////////////
     // CTest01Dlg message handlers
     BOOL CTest01Dlg::OnInitDialog()
     {
      CDialog::OnInitDialog();
      // Add "About..." menu item to system menu.
      // IDM_ABOUTBOX must be in the system command range.
      ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
      ASSERT(IDM_ABOUTBOX < 0xF000);
      CMenu* pSysMenu = GetSystemMenu(FALSE);
      if (pSysMenu != NULL)
       {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
         pSysMenu->AppendMenu(MF_SEPARATOR);
         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
       }
      // Set the icon for this dialog. The framework does this automatically
      // when the application''s main window is not a dialog
      SetIcon(m_hIcon, TRUE); // Set big icon
      SetIcon(m_hIcon, FALSE); // Set small icon
      // TODO: Add extra initialization here
      file://加入
      bm[0].LoadBitmap (IDB_BITMAP1);
      bm[1].LoadBitmap (IDB_BITMAP3);
      file://加入结束
      return TRUE; // return TRUE unless you set the focus to a control
     }
     void CTest01Dlg::OnSysCommand(UINT nID, LPARAM lParam)
     {
      if ((nID & 0xFFF0) == IDM_ABOUTBOX)
       {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
       }
      else
       {
        CDialog::OnSysCommand(nID, lParam);
        }
      }
     void CTest01Dlg::OnPaint() 
      {
       if (IsIconic())
        {
         CPaintDC dc(this); // device context for painting
         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
         // Center icon in client rectangle
         int cxIcon = GetSystemMetrics(SM_CXICON);
         int cyIcon = GetSystemMetrics(SM_CYICON);
         CRect rect;
         GetClientRect(&rect);
         int x = (rect.Width() - cxIcon + 1) / 2;
         int y = (rect.Height() - cyIcon + 1) / 2;
         // Draw the icon
         dc.DrawIcon(x, y, m_hIcon);
      }
      else
      {
       CDialog::OnPaint();
      }
     }
     HCURSOR CTest01Dlg::OnQueryDragIcon()
      {
       return (HCURSOR) m_hIcon;
       }
      file://加入
      void CTest01Dlg::OnButton1() 
       {
        static BOOL bStarted=FALSE;
        if (!bStarted){
         SetTimer(IDC_TIMER1,500,NULL); 
         pMyThread=AfxBeginThread(MyThreadProc,this);
        }else{
        if (pMyThread){
         pMyThread->PostThreadMessage (WM_QUIT,0,0);
         ::WaitForSingleObject(pMyThread->m_hThread ,INFINITE);
         pMyThread=NULL;
         }
         KillTimer(IDC_TIMER1);
        }
       bStarted=!bStarted;
       ((CButton*)GetDlgItem(IDC_BUTTON1))->SetWindowText((bStarted?_T("停止"):_T("启动")));
      }
      void CTest01Dlg::OnTimer(UINT nIDEvent) 
      {
       if (nIDEvent==IDC_TIMER1)
        DisplayFace(CPoint(10,10));
        CDialog::OnTimer(nIDEvent);
       }
      void CTest01Dlg::DisplayFace(CPoint p)
       {
        static int i=0;
        ccs.Lock ();
        BITMAP bmo;
        bm[i].GetObject (sizeof(bmo),&bmo);
        CClientDC dc(this);
        CDC bmpDC;
        bmpDC.CreateCompatibleDC (&dc);
        bmpDC.SelectObject (&bm[i]);
        dc.BitBlt (p.x ,p.y ,bmo.bmWidth,bmo.bmHeight,&bmpDC,0,0,SRCCOPY);
        i++;
        if (i==sizeof(bm)/sizeof(bm[0])) i=0;
         ccs.Unlock ();
        }
       UINT CTest01Dlg::MyThreadProc(LPVOID pParam)
       {
        CTest01Dlg *me=(CTest01Dlg *)pParam;
        MSG msg;
        while(!PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)){
         me->DisplayFace (CPoint(100,10));
         ::Sleep (200);
         }
        return 0;
      }
      //加入结束            
     
  说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。 
  2/2   首页 上一页 1 2  |