📄 mjsplash.cpp
字号:
// MJSplash.cpp : implementation file
//
#include "stdafx.h"
#include "ElecDemo.h"
#include "MJSplash.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define SPLASH_TIMER WM_USER+100
/////////////////////////////////////////////////////////////////////////////
// CMJSplash dialog
CMJSplash::CMJSplash(CWnd* pParent /*=NULL*/)
: CDialog(CMJSplash::IDD, pParent)
{
//{{AFX_DATA_INIT(CMJSplash)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_yPos=0;
m_Speed=4;
m_xPos=20;
m_bStrech=TRUE;
m_crString=RGB(242,192,86);
m_Height=0;
}
void CMJSplash::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMJSplash)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMJSplash, CDialog)
//{{AFX_MSG_MAP(CMJSplash)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_WM_ERASEBKGND()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMJSplash message handlers
void CMJSplash::SetSplash(UINT uBmp)
{
m_bmpSplash.DeleteObject();
m_bmpSplash.LoadBitmap(uBmp);
BITMAP bm;
m_bmpSplash.GetBitmap(&bm);
m_bmpSize.cx=bm.bmWidth;
m_bmpSize.cy=bm.bmHeight;
}
void CMJSplash::SetSplash(CString bmpPath)
{
if(!LoadPictureFile(bmpPath,&m_bmpSplash,m_bmpSize))
AfxMessageBox("载入图片错误!");
}
void CMJSplash::SetColor(COLORREF color)
{
m_crString=color;
}
BOOL CMJSplash::LoadPictureFile(LPCTSTR szFile,CBitmap*pBitmap,CSize &mSize)
{ // open file
HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if(INVALID_HANDLE_VALUE == hFile)
{
return FALSE;
}
// get file size
DWORD dwFileSize = GetFileSize(hFile, NULL);
if(-1 == dwFileSize)
{
return FALSE;
}
LPVOID pvData = NULL;
// alloc memory based on file size
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
_ASSERTE(NULL != hGlobal);
pvData = GlobalLock(hGlobal);
if(NULL == hGlobal)
{
return FALSE;
}
DWORD dwBytesRead = 0;
// read file and store in global memory
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
_ASSERTE(FALSE != bRead);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
LPSTREAM pstm = NULL;
// create IStream* from global memory
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
_ASSERTE(SUCCEEDED(hr) && pstm);
// Create IPicture from image file
LPPICTURE gpPicture;
hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
_ASSERTE(SUCCEEDED(hr) && gpPicture);
pstm->Release();
OLE_HANDLE m_picHandle;
gpPicture->get_Handle(&m_picHandle);
pBitmap->DeleteObject();
pBitmap->Attach((HGDIOBJ) m_picHandle);
BITMAP bm;
GetObject(pBitmap->m_hObject, sizeof(bm), &bm);
mSize.cx = bm.bmWidth; //nWidth;
mSize.cy = bm.bmHeight; //nHeight;
return TRUE;
}
void CMJSplash::OnPaint()
{
CPaintDC dc(this); // device context for painting
ASSERT(m_bmpSplash.m_hObject);
CRect rect;
GetClientRect(&rect);
CDC memDC,bmpDC;
CBitmap memBmp;
memDC.CreateCompatibleDC(&dc);
bmpDC.CreateCompatibleDC(&dc);
memBmp.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
bmpDC.SelectObject(&m_bmpSplash);
memDC.SelectObject(&memBmp);
if(m_bStrech)
{
memDC.StretchBlt(0,0,rect.Width(),rect.Height(),&bmpDC,0,0,m_bmpSize.cx,m_bmpSize.cy,
SRCCOPY);
}
else
{
memDC.BitBlt(0,0,m_rcClient.Width(),m_rcClient.Height(),&bmpDC,0,0,SRCCOPY);
}
memDC.SetBkMode(TRANSPARENT);
memDC.SetTextColor(m_crString);
memDC.SelectObject(&m_Font);
DrawSplashText(&memDC);
dc.BitBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,SRCCOPY);
memDC.DeleteDC();
memBmp.DeleteObject();
// Do not call CDialog::OnPaint() for painting messages
}
void CMJSplash::AddSplashString(CString string,COLORREF cr,HFONT hFont)
{
if(hFont==NULL)
hFont=(HFONT)m_Font.GetSafeHandle();
CSplashString *pS=new CSplashString(string,cr,hFont);
m_SplashStrings.Add(pS);
}
void CMJSplash::SetSpeed(int speed)
{
m_Speed=speed;
}
void CMJSplash::Show()
{
DoModal();
}
void CMJSplash::OnTimer(UINT nIDEvent)
{
if(nIDEvent==SPLASH_TIMER)
{
m_yPos+=m_Speed;
CRect rect;
GetClientRect(rect);
if((m_yPos-m_Height)>=rect.Height())
EndDialog(0);
Invalidate();
}
CDialog::OnTimer(nIDEvent);
}
BOOL CMJSplash::OnInitDialog()
{
CDialog::OnInitDialog();
VERIFY(m_Font.CreateFont(
24, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_SEMIBOLD, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Arial")); // lpszFacename
MoveWindow(0,0,m_bmpSize.cx,m_bmpSize.cy);
CenterWindow();
GetClientRect(&m_rcClient);
SetTimer(SPLASH_TIMER,100,NULL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CMJSplash::OnLButtonDown(UINT nFlags, CPoint point)
{
EndDialog(0);
CDialog::OnLButtonDown(nFlags, point);
}
BOOL CMJSplash::OnEraseBkgnd(CDC* /*pDC*/)
{
return FALSE;
}
void CMJSplash::DrawSplashText(CDC*pDC)
{
m_Height=0;
int y=m_rcClient.bottom-m_yPos;
for(int i=0;i<m_SplashStrings.GetSize();i++)
{
CSplashString *pS=(CSplashString *)m_SplashStrings.GetAt(i);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(pS->color);
CFont font;
font.Attach(pS->hfont);
pDC->SelectObject(&font);
CSize size=pDC->GetTextExtent(pS->string);
CRect rect=CRect(m_xPos,y,m_xPos+size.cx,y+size.cy);
pDC->DrawText(pS->string,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
y+=size.cy;
m_Height+=size.cy;
}
}
void CMJSplash::SetStrech(BOOL bStr)
{
m_bStrech=bStr;
}
void CMJSplash::OnDestroy()
{
CDialog::OnDestroy();
for(int i=0;i<m_SplashStrings.GetSize();i++)
{
CSplashString*pS=(CSplashString*)m_SplashStrings.GetAt(i);
delete pS;
}
m_SplashStrings.RemoveAll();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -