📄 transparentwnd.cpp
字号:
//********************************************************************************
//* TransparentWindow.CPP
//*
//* A transparent window class.
//*
//* Based on the idea of Jason Wylie ,Franz Polzer,Luo yun bin
//* e9225140@student.tuwien.ac.at
//* (C) 2002 by 王鹏
//*
//* Write to me: mailwp@21cn.com
//********************************************************************************
#include "stdafx.h"
#include "TransparentWnd.h"
#include "Dib.h"
#include "resource.h"
#include <assert.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static TCHAR THIS_FILE[] = TEXT(__FILE__);
#endif
#define WM_LIBEN WM_USER+994
CString GetCurPath()
{
TCHAR exeFullPath[MAX_PATH];
CString strPath;
GetModuleFileName(NULL,exeFullPath,MAX_PATH);
strPath.Format(TEXT("%s"), exeFullPath);
strPath = strPath.Left(strPath.ReverseFind('\\'));
return strPath;
}
//********************************************************************************
// LoadBMPImage - Loads a BMP file and creates a bitmap GDI object
// also creates logical palette for it.
// Returns - TRUE for success
// sBMPFile - Full path of the BMP file
// bitmap - The bitmap object to initialize
// pPal - Will hold the logical palette. Can be NULL
//********************************************************************************
WORD DIBNumColors( LPSTR lpbi )
{
WORD wBitCount;
DWORD dwClrUsed;
dwClrUsed = ((LPBITMAPINFOHEADER) lpbi)->biClrUsed;
if (dwClrUsed)
return (WORD) dwClrUsed;
wBitCount = ((LPBITMAPINFOHEADER) lpbi)->biBitCount;
switch (wBitCount)
{
case 8: return 256;
case 1: return 2;
case 2: return 4;
case 4: return 16;
default:return 0;
}
return 0;
}
WORD PaletteSize( LPSTR lpbi )
{
return ( DIBNumColors( lpbi ) * sizeof( RGBQUAD ) );
}
HBITMAP DIBTohBitmap(HDC m_hDC,LPSTR lpSrcDIB)
{
HBITMAP hBitmap = NULL;
HBITMAP hOldBmp = NULL;
HDC hTmpDC = NULL;
BITMAPINFOHEADER* bitmapheader = (BITMAPINFOHEADER*)lpSrcDIB;
hBitmap = CreateCompatibleBitmap(m_hDC,
bitmapheader->biWidth,
(bitmapheader->biHeight));
hTmpDC=CreateCompatibleDC(m_hDC);
hOldBmp=(HBITMAP)SelectObject(hTmpDC,hBitmap);
INT dwPSize = PaletteSize((LPSTR)lpSrcDIB);
BITMAPINFO * pbinfo = (BITMAPINFO *)lpSrcDIB;
StretchDIBits(hTmpDC,0,0,bitmapheader->biWidth,
(bitmapheader->biHeight),0,0,bitmapheader->biWidth,
(bitmapheader->biHeight),lpSrcDIB + 40 + dwPSize,pbinfo,
DIB_RGB_COLORS,SRCCOPY);
SelectObject(hTmpDC,hOldBmp);
DeleteDC(hTmpDC);
return hBitmap; //记得外面用完释放
}
BOOL LoadBMPImage( LPCTSTR sBMPFile, CBitmap& bitmap, CPalette *pPal )
{
CFile file;
if( !file.Open( sBMPFile, CFile::modeRead) )
return FALSE;
BITMAPFILEHEADER bmfHeader;
// Read file header
if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
return FALSE;
// File type should be 'BM'
if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B'))
return FALSE;
// Get length of the remainder of the file and allocate memory
ULONGLONG nPackedDIBLen = file.GetLength() - sizeof(BITMAPFILEHEADER);
HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, (UINT) nPackedDIBLen);
if (hDIB == 0)
return FALSE;
// Read the remainder of the bitmap file.
if (file.Read((LPSTR)hDIB, (UINT) nPackedDIBLen) != nPackedDIBLen )
{
::GlobalFree(hDIB);
return FALSE;
}
BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ;
BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
// If bmiHeader.biClrUsed is zero we have to infer the number
// of colors from the number of bits used to specify it.
int nColors = bmiHeader.biClrUsed ? bmiHeader.biClrUsed :
1 << bmiHeader.biBitCount;
LPVOID lpDIBBits;
if( bmInfo.bmiHeader.biBitCount > 8 )
lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors + bmInfo.bmiHeader.biClrUsed) +
((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
else
lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
// Create the logical palette
// if( pPal != NULL )
// {
// // Create the palette
// if( nColors <= 256 )
// {
// UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
// LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
//
// pLP->palVersion = 0x300;
// pLP->palNumEntries = nColors;
//
// for( int i=0; i < nColors; i++)
// {
// pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
// pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
// pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
// pLP->palPalEntry[i].peFlags = 0;
// }
//
// pPal->CreatePalette( pLP );
//
// delete[] pLP;
// }
// }
CClientDC dc(NULL);
// CPalette* pOldPalette = NULL;
// if( pPal )
// {
// pOldPalette = dc.SelectPalette( pPal, FALSE );
// dc.RealizePalette();
// }
// HBITMAP hBmp = CreateDIBitmap( dc.m_hDC, // handle to device context
// &bmiHeader, // pointer to bitmap size and format data
// CBM_INIT, // initialization flag
// lpDIBBits, // pointer to initialization data
// &bmInfo, // pointer to bitmap color-format data
// DIB_RGB_COLORS); // color-data usage
HBITMAP hBmp = DIBTohBitmap(dc.GetSafeHdc(), (LPSTR) hDIB);
bitmap.Attach( hBmp );
// if( pOldPalette )
// dc.SelectPalette( pOldPalette, FALSE );
::GlobalFree(hDIB);
return TRUE;
}
//********************************************************************************
// function:FindBmpFile()
// strParent search path
// 1.search bmp file
// 2.create random number
//********************************************************************************
void CTransparentWnd::FindBmpFile(CString strPath)
{
m_szBmpFile = strPath + TEXT("\\bg.bmp");
m_szNumFile = strPath + TEXT("\\num.bmp");
// int nFilecount1=0; // 背景
// int nFilecount2=0; // 数字
// int nFilter;
// nFilter=GetCurPath().GetLength()+10;
// CFileFind finder;
//
// // build a string with wildcards
// CString strWildcard(strPath);
// strWildcard += _T("\\*.bmp");
// try
// {
// //--------------------------------------------------------------
// // start working for filescount
// //--------------------------------------------------------------
// BOOL bWorking = finder.FindFile(strWildcard);
//
// while (bWorking)
// {
// bWorking = finder.FindNextFile();
//
// // skip . and .. files; otherwise, we'd
// // recur infinitely!
//
// if (finder.IsDots())
// continue;
//
// // if it's NOT a directory, recursively search it
//
// if (!finder.IsDirectory())
// {
// if (finder.GetFilePath().GetLength() > nFilter)
// nFilecount1++; //background
// else
// nFilecount2++; //number
// }
// }
// DWORD dwTick=GetTickCount();
// //random the image
// nFilecount1=(int)(dwTick%(nFilecount1-1));
// nFilecount2=(int)(dwTick%(nFilecount2-1));
// //--------------------------------------------------------------
// // 获得具体的图片
// //--------------------------------------------------------------
// bWorking = finder.FindFile(strWildcard);
// int m=0;
// int n=0;
// while (bWorking) //2
// {
// bWorking = finder.FindNextFile();
//
// // skip . and .. files; otherwise, we'd
// // recur infinitely!
//
// if (finder.IsDots())
// continue;
//
// // if it's NOT a directory, recursively search it
//
// if (!finder.IsDirectory())
// {
// if (finder.GetFilePath().GetLength() > nFilter)
// {
// if (m==nFilecount1)
// m_szBmpFile=finder.GetFilePath();
// m++;
// }
// else
// {
// if (n==nFilecount2)
// m_szNumFile=finder.GetFilePath();
// n++;
// }
// }
// }//2
// }
// catch(...)
// {
//
// }
//
}
//********************************************************************************
//* Constructor
//********************************************************************************
CTransparentWnd::CTransparentWnd()
{
m_nh1=0;
m_nh2=0;
m_nm1=0;
m_nm2=0;
m_ns1=0;
m_ns2=0;
}
//********************************************************************************
//* Destructor
//********************************************************************************
CTransparentWnd::~CTransparentWnd()
{
KillTimer(1);
//AfxPostQuitMessage(0);
}
BEGIN_MESSAGE_MAP(CTransparentWnd, CWnd)
//{{AFX_MSG_MAP(CTransparentWnd)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
ON_COMMAND(ID_MIN_ICON, OnMinIcon)
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_CLOSE, OnClose)
//}}AFX_MSG_MAP
// ON_MESSAGE(WM_LIBEN,OnLiben)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -