封装类之CSplashWnd(应用程序的启动画面)

1XX.h文件

class CSplashWnd : public CWnd
{
    DECLARE_DYNAMIC(CSplashWnd)

public:
    CSplashWnd();
    virtual ~CSplashWnd();

protected:
    virtual void PostNcDestroy();
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnTimer(UINT_PTR nIDEvent);
    afx_msg void OnPaint();
    BOOL Create(CWnd* pParentWnd=NULL);
    void HideSplashScreen(void);
    DECLARE_MESSAGE_MAP()

protected:
    CBitmap m_bitmap;
    static CSplashWnd* c_pSplashWnd;
    static BOOL c_bShowSplashWnd;

public:
    static void EnableSplashScreen(BOOL bEnable=TRUE);
    static void ShowSplashScreen(CWnd* pParentWnd=NULL);
    static BOOL PreTranslateAppMessage(MSG* pMsg);
};

2XX.cpp文件

// SplashWnd.cpp : 实现文件
//

#include "stdafx.h"
#include "SplashScreen.h"
#include "SplashWnd.h"
#include "resource.h"


// CSplashWnd
CSplashWnd* CSplashWnd::c_pSplashWnd;
BOOL CSplashWnd::c_bShowSplashWnd;
IMPLEMENT_DYNAMIC(CSplashWnd, CWnd)

CSplashWnd::CSplashWnd()
{

}

CSplashWnd::~CSplashWnd()
{
    ASSERT(c_pSplashWnd == this);
    c_pSplashWnd = NULL;
}


BEGIN_MESSAGE_MAP(CSplashWnd, CWnd)
    ON_WM_CREATE()
    ON_WM_TIMER()
    ON_WM_PAINT()
END_MESSAGE_MAP()



// CSplashWnd 消息处理程序

void CSplashWnd::EnableSplashScreen(BOOL bEnable)
{
    c_bShowSplashWnd = bEnable;
}

void CSplashWnd::ShowSplashScreen(CWnd* pParentWnd)
{
    if ( !c_bShowSplashWnd || c_pSplashWnd != NULL )
    {
        return;
    }

    c_pSplashWnd = new CSplashWnd;
    if ( !c_pSplashWnd->Create(pParentWnd) )
    {
        delete c_pSplashWnd;
    }
    else
    {
        c_pSplashWnd->UpdateWindow();
    }
}

BOOL CSplashWnd::PreTranslateAppMessage(MSG* pMsg)
{
    if ( c_pSplashWnd == NULL )
    {
        return FALSE;
    }

    if ( pMsg->message == WM_KEYDOWN
        || pMsg->message == WM_SYSKEYDOWN
        || pMsg->message == WM_LBUTTONDOWN
        || pMsg->message == WM_RBUTTONDOWN
        || pMsg->message == WM_MBUTTONDOWN
        || pMsg->message == WM_NCLBUTTONDOWN
        || pMsg->message == WM_NCRBUTTONDOWN
        || pMsg->message == WM_NCMBUTTONDOWN)
    {
        c_pSplashWnd->HideSplashScreen();
        return TRUE;
    }

    return FALSE;
}

void CSplashWnd::PostNcDestroy()
{
    delete this;
}

int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if ( CWnd::OnCreate(lpCreateStruct) == -1 )
    {
        return -1;
    }

    CenterWindow();

    SetTimer(1, 3000, NULL);

    return 0;
}

void CSplashWnd::OnTimer(UINT_PTR nIDEvent)
{
    if ( nIDEvent == 1 )
    {
        HideSplashScreen();
    }
}

void CSplashWnd::OnPaint()
{
    CPaintDC dc(this);
    CDC dcImg;
    if ( !dcImg.CreateCompatibleDC(&dc) )
    {
        return;
    }

    BITMAP bm;
    m_bitmap.GetBitmap(&bm);

    CBitmap* pOldBit = dcImg.SelectObject(&m_bitmap);
    dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImg, 0, 0, SRCCOPY);
    dcImg.SelectObject(pOldBit);
}

BOOL CSplashWnd::Create(CWnd* pParentWnd)
{
    if ( !m_bitmap.LoadBitmap(IDB_SPLASH))
    {
        return FALSE;
    }

    BITMAP bm;
    m_bitmap.GetBitmap(&bm);
    return CreateEx(0,
        AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
        NULL,
        WS_POPUP | WS_VISIBLE,
        0, 0,
        bm.bmWidth,
        bm.bmHeight,
        pParentWnd->GetSafeHwnd(),
        NULL);
}

void CSplashWnd::HideSplashScreen()
{
    DestroyWindow();
    AfxGetMainWnd()->UpdateWindow();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值