📄 welcome.cpp
字号:
#include "stdafx.h"
#include "welcome.h"
//显示欢迎画面
BOOL WelcomeToYou(CSplashWnd& splashWnd ,LPSTR lpFileName)
{
char szDrive[_MAX_DRIVE],szDir[_MAX_DIR],szTemp[_MAX_FNAME], szFileName[128];
GetModuleFileName(AfxGetApp()->m_hInstance, szFileName, 128);
_splitpath(szFileName, szDrive, szDir, szTemp, szTemp);
wsprintf(szFileName, "%s%s%s", szDrive, szDir, lpFileName);
// CSplashWnd splashWnd ;
if( !splashWnd.Create( szFileName )){
AfxMessageBox( "Create splash window fail !") ;
return FALSE ;
}
splashWnd.ShowWindow(SW_SHOW);
splashWnd.UpdateWindow();
return TRUE;
}
//--------------------------------------------------------------------------------------------//
#define IS_WIN30_DIB(lpbi) ((*(LPDWORD)(lpbi)) == sizeof(BITMAPINFOHEADER))
#define RECTWIDTH(lpRect) ((lpRect)->right - (lpRect)->left)
#define RECTHEIGHT(lpRect) ((lpRect)->bottom - (lpRect)->top)
CImageFile::CImageFile()
{
m_hDib = NULL ;
m_hImageData = NULL ;
m_nKindOfImage = IMAGE_UNKNOWN ;
m_palDIB = NULL ;
m_strFileName = "" ;
}
CImageFile::CImageFile(LPSTR cFileName)
{
m_hDib = NULL ;
m_hImageData = NULL ;
m_nKindOfImage = IMAGE_UNKNOWN ;
m_palDIB = NULL ;
m_strFileName = cFileName ;
}
CImageFile::~CImageFile()
{
if(m_hDib != NULL )
::GlobalFree( m_hDib ) ;
if(m_palDIB)
delete m_palDIB ;
}
BOOL CImageFile::CreateImage( LPSTR cFileName )
{
m_hDib = NULL ;
m_hImageData = NULL ;
m_nKindOfImage = IMAGE_UNKNOWN ;
m_palDIB = NULL ;
m_strFileName = cFileName ;
return TRUE ;
}
BOOL CImageFile:: PaintImage(HDC hDC , CRect *lpDCRect)
{
LPSTR lpDIBHdr; // Pointer to BITMAPINFOHEADER
LPSTR lpDIBBits; // Pointer to DIB bits
BOOL bSuccess=FALSE; // Success/fail flag
HPALETTE hPal=NULL; // Our DIB's palette
HPALETTE hOldPal=NULL; // Previous palette
/* Check for valid DIB handle */
if (m_hDib == NULL)
return FALSE;
int nDibWidth = ImageWidth() ;
int nDibHeight = ImageHeight() ;
/* Lock down the DIB, and get a pointer to the beginning of the bit
* buffer
*/
lpDIBHdr = (LPSTR) ::GlobalLock((HGLOBAL) m_hDib);
lpDIBBits = (char far*)FindDIBBits(lpDIBHdr);
// Get the DIB's palette, then select it into DC
if (m_palDIB != NULL)
{
hPal = (HPALETTE) (m_palDIB->m_hObject);
// Select as background since we have
// already realized in forground if needed
hOldPal = ::SelectPalette(hDC, hPal, TRUE);
::RealizePalette( hDC );
}
/* Make sure to use the stretching mode best for color pictures */
::SetStretchBltMode(hDC, COLORONCOLOR);
/* Determine whether to call StretchDIBits() or SetDIBitsToDevice() */
if ((RECTWIDTH(lpDCRect) == nDibWidth) &&
(RECTHEIGHT(lpDCRect) == nDibHeight)) {
bSuccess = ::SetDIBitsToDevice(hDC, // hDC
lpDCRect->left, // DestX
lpDCRect->top, // DestY
RECTWIDTH(lpDCRect), // nDestWidth
RECTHEIGHT(lpDCRect), // nDestHeight
0,
0,
0, // nStartScan
nDibHeight, // nNumScans
lpDIBBits, // lpBits
(LPBITMAPINFO)lpDIBHdr, // lpBitsInfo
DIB_RGB_COLORS); // wUsage
}
else{
bSuccess = ::StretchDIBits(hDC, // hDC
lpDCRect->left, // DestX
lpDCRect->top, // DestY
RECTWIDTH(lpDCRect), // nDestWidth
RECTHEIGHT(lpDCRect), // nDestHeight
0,
0,
nDibWidth, // wSrcWidth
nDibHeight , // wSrcHeight
lpDIBBits, // lpBits
(LPBITMAPINFO)lpDIBHdr, // lpBitsInfo
DIB_RGB_COLORS, // wUsage
SRCCOPY); // dwROP
}
::GlobalUnlock((HGLOBAL) m_hDib);
/* Reselect old palette */
if (hOldPal != NULL)
{
::SelectPalette(hDC, hOldPal, TRUE);
}
return bSuccess;
}
BOOL CImageFile::ReadImageFile()
{
return TRUE ;
}
BOOL CImageFile::TranslateImageToDIB()
{
return TRUE ;
}
BOOL CImageFile::CreatePalette(CPalette* pPal)
{
LPLOGPALETTE lpPal; // pointer to a logical palette
HANDLE hLogPal; // handle to a logical palette
HPALETTE hPal = NULL; // handle to a palette
int i; // loop index
WORD wNumColors; // number of colors in color table
LPSTR lpbi; // pointer to packed-DIB
LPBITMAPINFO lpbmi; // pointer to BITMAPINFO structure (Win3.0)
LPBITMAPCOREINFO lpbmc; // pointer to BITMAPCOREINFO structure (old)
BOOL bWinStyleDIB; // flag which signifies whether this is a Win3.0 DIB
BOOL bResult = FALSE;
/* if handle to DIB is invalid, return FALSE */
if (m_hDib == NULL)
return FALSE;
lpbi = (LPSTR) ::GlobalLock((HGLOBAL) m_hDib);
/* get pointer to BITMAPINFO (Win 3.0) */
lpbmi = (LPBITMAPINFO)lpbi;
/* get pointer to BITMAPCOREINFO (old 1.x) */
lpbmc = (LPBITMAPCOREINFO)lpbi;
/* get the number of colors in the DIB */
wNumColors = ImageNumColors();
if (wNumColors != 0)
{
/* allocate memory block for logical palette */
hLogPal = ::GlobalAlloc(GHND, sizeof(LOGPALETTE)
+ sizeof(PALETTEENTRY)
* wNumColors);
/* if not enough memory, clean up and return NULL */
if (hLogPal == 0)
{
::GlobalUnlock((HGLOBAL) m_hDib);
return FALSE;
}
lpPal = (LPLOGPALETTE) ::GlobalLock((HGLOBAL) hLogPal);
/* set version and number of palette entries */
lpPal->palVersion = PALVERSION;
lpPal->palNumEntries = (WORD)wNumColors;
/* is this a Win 3.0 DIB? */
bWinStyleDIB = IS_WIN30_DIB(lpbi);
for (i = 0; i < (int)wNumColors; i++)
{
if (bWinStyleDIB)
{
lpPal->palPalEntry[i].peRed = lpbmi->bmiColors[i].rgbRed;
lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen;
lpPal->palPalEntry[i].peBlue = lpbmi->bmiColors[i].rgbBlue;
lpPal->palPalEntry[i].peFlags = 0;
}
else
{
lpPal->palPalEntry[i].peRed = lpbmc->bmciColors[i].rgbtRed;
lpPal->palPalEntry[i].peGreen = lpbmc->bmciColors[i].rgbtGreen;
lpPal->palPalEntry[i].peBlue = lpbmc->bmciColors[i].rgbtBlue;
lpPal->palPalEntry[i].peFlags = 0;
}
}
/* create the palette and get handle to it */
bResult = pPal->CreatePalette(lpPal);
::GlobalUnlock((HGLOBAL) hLogPal);
::GlobalFree((HGLOBAL) hLogPal);
}
::GlobalUnlock((HGLOBAL) m_hDib);
return bResult;
}
void far* CImageFile::FindDIBBits(LPSTR lpbi)
{
return (lpbi + *(LPDWORD)lpbi + PaletteSize());
}
int CImageFile::ImageWidth()
{
ASSERT( m_hDib != NULL) ;
char far* lpDIB =(char far*)::GlobalLock( m_hDib ) ;
LPBITMAPINFOHEADER lpbmi; // pointer to a Win 3.0-style DIB
LPBITMAPCOREHEADER lpbmc; // pointer to an other-style DIB
/* point to the header (whether Win 3.0 and old) */
lpbmi = (LPBITMAPINFOHEADER)lpDIB;
lpbmc = (LPBITMAPCOREHEADER)lpDIB;
long nWidth ;
/* return the DIB width if it is a Win 3.0 DIB */
if (IS_WIN30_DIB(lpDIB))
nWidth = lpbmi->biWidth;
else /* it is an other-style DIB, so return its width */
nWidth = (DWORD)lpbmc->bcWidth;
::GlobalUnlock( m_hDib ) ;
return LOWORD(nWidth);
}
int CImageFile::ImageHeight()
{
ASSERT(m_hDib != NULL);
char far* lpDIB =(char far*)::GlobalLock( m_hDib ) ;
LPBITMAPINFOHEADER lpbmi; // pointer to a Win 3.0-style DIB
LPBITMAPCOREHEADER lpbmc; // pointer to an other-style DIB
/* point to the header (whether old or Win 3.0 */
lpbmi = (LPBITMAPINFOHEADER)lpDIB;
lpbmc = (LPBITMAPCOREHEADER)lpDIB;
long nHeight ;
/* DIB height if it is a Win 3.0 DIB */
if (IS_WIN30_DIB(lpDIB))
nHeight = lpbmi->biHeight;
else /* it is an other-style DIB, so return its height */
nHeight = (DWORD)lpbmc->bcHeight;
::GlobalUnlock( m_hDib ) ;
return LOWORD(nHeight);
}
int CImageFile::PaletteSize()
{
char far* lpbi =(char far*) ::GlobalLock( m_hDib ) ;
int nPaletteSize ;
/* calculate the size required by the palette */
if (IS_WIN30_DIB (lpbi))
nPaletteSize = (WORD)( ImageNumColors() * sizeof(RGBQUAD));
else
nPaletteSize = (WORD)( ImageNumColors() * sizeof(RGBTRIPLE));
::GlobalUnlock( m_hDib ) ;
return nPaletteSize ;
}
int CImageFile::ImageNumColors()
{
WORD wBitCount; // DIB bit count
/* If this is a Windows-style DIB, the number of colors in the
* color table can be less than the number of bits per pixel
* allows for (i.e. lpbi->biClrUsed can be set to some value).
* If this is the case, return the appropriate value.
*/
char far* lpbi =(char far*)::GlobalLock( m_hDib ) ;
if (IS_WIN30_DIB(lpbi))
{
DWORD dwClrUsed;
dwClrUsed = ((LPBITMAPINFOHEADER)lpbi)->biClrUsed;
if (dwClrUsed != 0)
return (WORD)dwClrUsed;
}
/* Calculate the number of colors in the color table based on
* the number of bits per pixel for the DIB.
*/
if (IS_WIN30_DIB(lpbi))
wBitCount = ((LPBITMAPINFOHEADER)lpbi)->biBitCount;
else
wBitCount = ((LPBITMAPCOREHEADER)lpbi)->bcBitCount;
/* return number of colors based on bits per pixel */
int nNumColors ;
switch (wBitCount)
{
case 1:
nNumColors = 2;
break ;
case 4:
nNumColors = 16;
break ;
case 8:
nNumColors = 256;
break ;
default:
nNumColors = 0;
break ;
}
::GlobalUnlock( m_hDib ) ;
return nNumColors ;
}
//-------------------------------------------------------------------------------------//
#define DIB_HEADER_MARKER ((WORD) ('M' << 8) | 'B')
#define INT_MAXNUM 65534
CDibImage::CDibImage()
{
}
CDibImage::CDibImage(char* cFileName) : CImageFile(cFileName)
{
}
CDibImage::~CDibImage()
{
}
BOOL CDibImage::ReadImageFile()
{
CFile file;
CFileException fe;
if (!file.Open(m_strFileName, CFile::modeRead | CFile::shareDenyWrite, &fe))
{
return FALSE;
}
// replace calls to Serialize with ReadDIBFile function
TRY
{
ReadDIBFile(file);
}
CATCH (CFileException, eLoad)
{
file.Abort(); // will not throw an exception
m_hDib = NULL;
return FALSE;
}
END_CATCH
InitDIBData();
if (m_hDib == NULL)
{
// may not be DIB format
MessageBox(NULL, "It is not DIB format !", NULL, MB_ICONINFORMATION | MB_OK);
return FALSE;
}
return TRUE;
}
BOOL CDibImage::ReadDIBFile(CFile& file)
{
BITMAPFILEHEADER bmfHeader;
DWORD dwBitsSize;
LPSTR pDIB;
/*
* get length of DIB in bytes for use when reading
*/
dwBitsSize = file.GetLength();
/*
* Go read the DIB file header and check if it's valid.
*/
if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
return NULL;
if (bmfHeader.bfType != DIB_HEADER_MARKER)
return NULL;
/*
* Allocate memory for DIB
*/
m_hDib = (HDIB)::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwBitsSize);
if (m_hDib == 0)
{
return FALSE;
}
pDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDib);
/*
* Go read the bits.
*/
if (file.ReadHuge(pDIB, dwBitsSize - sizeof(BITMAPFILEHEADER)) !=
dwBitsSize - sizeof(BITMAPFILEHEADER) )
{
::GlobalUnlock((HGLOBAL) m_hDib);
::GlobalFree((HGLOBAL) m_hDib);
return FALSE;
}
::GlobalUnlock((HGLOBAL) m_hDib);
return TRUE ;
}
void CDibImage::InitDIBData()
{
if (m_palDIB != NULL)
{
delete m_palDIB;
m_palDIB = NULL;
}
if (m_hDib == NULL)
{
return;
}
if (ImageWidth() > INT_MAXNUM ||ImageHeight() > INT_MAXNUM)
{
::GlobalUnlock((HGLOBAL) m_hDib);
::GlobalFree((HGLOBAL) m_hDib);
m_hDib = NULL;
MessageBox(NULL, "DIB is too large", NULL,
MB_ICONINFORMATION | MB_OK);
return;
}
// Create copy of palette
m_palDIB = new CPalette;
if (m_palDIB == NULL)
{
// we must be really low on memory
::GlobalFree((HGLOBAL) m_hDib);
m_hDib = NULL;
return;
}
if (CreatePalette( m_palDIB) == NULL)
{
// DIB may not have a palette
delete m_palDIB;
m_palDIB = NULL;
return;
}
}
/*
void CDibImage::InitDIBData()
{
if (m_palDIB != NULL)
{
delete m_palDIB;
m_palDIB = NULL;
}
if (m_hDib == NULL)
{
return;
}
if (ImageWidth() > INT_MAXNUM ||ImageHeight() > INT_MAXNUM)
{
::GlobalUnlock((HGLOBAL) m_hDib);
::GlobalFree((HGLOBAL) m_hDib);
m_hDib = NULL;
MessageBox(NULL, "The image is too big !", NULL, MB_ICONINFORMATION | MB_OK);
return;
}
// Create copy of palette
m_palDIB = new CPalette;
if (m_palDIB == NULL)
{
// we must be really low on memory
::GlobalFree((HGLOBAL) m_hDib);
m_hDib = NULL;
return;
}
if (CreatePalette(m_palDIB) == NULL)
{
// DIB may not have a palette
delete m_palDIB;
m_palDIB = NULL;
return;
}
}
*/
//----------------------------------------------------------------------------------------//
// splash.cpp : implementation file
//
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSplashWnd
IMPLEMENT_DYNCREATE(CSplashWnd, CView)
CSplashWnd::CSplashWnd()
{
}
CSplashWnd::~CSplashWnd()
{
DestroyWindow() ;
}
BOOL CSplashWnd::Create( LPSTR lpFileName)
{
m_dibImage.CreateImage( lpFileName ) ;
if(!m_dibImage.ReadImageFile()){
AfxMessageBox( " Read DIB Image False ! " ) ;
return FALSE ;
}
int nImageWidth = m_dibImage.ImageWidth() ;
int nImageHeight = m_dibImage.ImageHeight() ;
CRect rect( 0,0,nImageWidth,nImageHeight);
int nLeft,nTop ;
nLeft = GetSystemMetrics( SM_CXSCREEN )/2 - nImageWidth/2 ;
nTop = GetSystemMetrics( SM_CYSCREEN ) /2 - nImageHeight/2 ;
CString str ;
str = AfxRegisterWndClass( NULL ) ;
HWND hwnd = ::GetDesktopWindow() ;
CWnd::CreateEx( WS_EX_TOPMOST,str , NULL , WS_POPUP ,
nLeft , nTop , nImageWidth , nImageHeight , NULL , NULL , NULL ) ;
return TRUE ;
}
BEGIN_MESSAGE_MAP(CSplashWnd, CWnd)
//{{AFX_MSG_MAP(CSplashWnd)
ON_WM_CREATE()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSplashWnd message handlers
int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CSplashWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
int nImageWidth = m_dibImage.ImageWidth() ;
int nImageHeight = m_dibImage.ImageHeight() ;
CRect rect( 0,0,nImageWidth,nImageHeight);
m_dibImage.PaintImage( dc.m_hDC , &rect) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -