⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 window下的多线程编程参考书。值得一读
💻 CPP
字号:
//
// FILE: Main.cpp
//
// Copyright (c) 1997 by Aaron Michael Cohen and Mike Woodring
//
/////////////////////////////////////////////////////////////////////////
#include <afxwin.h>
#include <afxext.h>

#include "Animator.h"
#include "resource.h"

#define ANIMATION_FRAME_WIDTH       128
#define ANIMATION_FRAME_HEIGHT      128
#define ANIMATION_NUMBER_FRAMES     8
#define ANIMATION_INITIAL_FPS       20

class CAnimatorDlg : public CDialog {
private:
    HBITMAP m_hBitmap;
    CAnimator m_cAnimator;

public:
    // prepare dialog resource id in the constructor...
    CAnimatorDlg(CWnd* pParent = NULL) :        
            CDialog(IDD_ANIMATORDEMO, pParent) {
        m_hBitmap = NULL;
    };

    ~CAnimatorDlg() {
        // clean up the bitmap resource
        if (m_hBitmap)
            ::DeleteObject(m_hBitmap);
    };

    virtual BOOL OnInitDialog() {
        // call parent class function...
        CDialog::OnInitDialog();

        // subclass the Static control to be used for animation...
        m_cAnimator.SubclassDlgItem( IDC_ANIMATION, this);

        // set our application icon...
        HICON hIcon = AfxGetApp()->LoadIcon(IDI_ANIMATORDEMO);
        SetIcon( hIcon, TRUE);

        // center the dialog window in the screen...
        CenterWindow();

        // all of the frames for the animation are in one bitmap,
        // load the bitmap and set up the animation...
        m_hBitmap = (HBITMAP) ::LoadImage( AfxGetApp()->m_hInstance, "bounce.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
        m_cAnimator.SetupAnimation( m_hBitmap, CSize(ANIMATION_FRAME_WIDTH,ANIMATION_FRAME_HEIGHT), ANIMATION_NUMBER_FRAMES, ANIMATION_INITIAL_FPS, TRUE, FALSE);

        // make the animator control client size window equal to the image size...
        CRect wr, cr;
        m_cAnimator.GetWindowRect(wr);
        m_cAnimator.GetClientRect(cr);
        int width = ANIMATION_FRAME_WIDTH + (wr.right - wr.left) - (cr.right - cr.left);
        int height = ANIMATION_FRAME_HEIGHT + (wr.bottom - wr.top) - (cr.bottom - cr.top);
        m_cAnimator.SetWindowPos( NULL, 0, 0, width, height, SWP_NOZORDER | SWP_NOMOVE);

        // set the dialog radio buttons to reflect the animation settings...
        SendDlgItemMessage( IDC_OSCILLATE, BM_SETCHECK, TRUE);
        SendDlgItemMessage( IDC_20FPS, BM_SETCHECK, TRUE);
        SendDlgItemMessage( IDC_OPAQUE, BM_SETCHECK, TRUE);

        return TRUE;
    };

    virtual void OnCancel(void) {
        // use the IDCANCEL handler to stop the animation...
        m_cAnimator.Stop();

        // reset the animation back to the first frame...
        m_cAnimator.SetFrame(0);
    };

    virtual void OnOK(void) {
        // use the IDOK handler to start the animation...
        m_cAnimator.Start();
    };

    afx_msg void OnSysCommand( UINT nId, LPARAM lParam) {
        if ((nId & 0xFFF0) == SC_CLOSE) {
            // the user clicked the "x" close box, so exit the dialog...
            EndDialog(0);
        }
        else {
            // otherwise use default processing...
            CDialog::OnSysCommand( nId, lParam);
        }
    };

    afx_msg void OnLoop() {
        // change the animation repeat style to loop around and around...
        m_cAnimator.SetLoopStyle(FALSE);
    };

    afx_msg void OnOscillate() {
        // change the animation repeat style to oscillate back and forth...
        m_cAnimator.SetLoopStyle(TRUE);
    };

    afx_msg void On5FPS() {
        // change the animation frame rate to 5 frames/sec...
        m_cAnimator.SetFrameRate(5);
    };

    afx_msg void On20FPS() {
        // change the animation frame rate to 20 frames/sec...
        m_cAnimator.SetFrameRate(20);
    };

    afx_msg void OnTransparent() {
        // set the transparency of the animation to transparent...
        m_cAnimator.SetTransparency(TRUE);
    };

    afx_msg void OnOpaque() {
        // set the transparency of the animation to opaque...
        m_cAnimator.SetTransparency(FALSE);
    };

    // declare the MFC message map variables...
    DECLARE_MESSAGE_MAP()
};

// define the MFC message map for our dialog box...
BEGIN_MESSAGE_MAP( CAnimatorDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_COMMAND( IDC_LOOP, OnLoop)
    ON_COMMAND( IDC_OSCILLATE, OnOscillate)
    ON_COMMAND( IDC_5FPS, On5FPS)
    ON_COMMAND( IDC_20FPS, On20FPS)
    ON_COMMAND( IDC_TRANSPARENT, OnTransparent)
    ON_COMMAND( IDC_OPAQUE, OnOpaque)
END_MESSAGE_MAP()


// simple app just creates a modal version of our dialog
// box and exits when the dialog box exits...
class CAnimatorDemo : public CWinApp {
public:
    virtual BOOL InitInstance(void) {
        // the application is implemented as a dialog box...
        CAnimatorDlg dlg;
        m_pMainWnd = &dlg;
        dlg.DoModal();

        // return FALSE to prevent the application
        // class message pump from starting...
        return FALSE;        
    };
};

// declare the single global application instance...
CAnimatorDemo g_theApp;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -