//********************************************************************************
//* 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 char THIS_FILE[] = __FILE__;
#endif
#define WM_LIBEN WM_USER+994
CString GetCurPath()
{
TCHAR exeFullPath[MAX_PATH];
CString strPath;
GetModuleFileName(NULL,exeFullPath,MAX_PATH);
strPath.Format("%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
//********************************************************************************
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
DWORD nPackedDIBLen = file.GetLength() - sizeof(BITMAPFILEHEADER);
HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, nPackedDIBLen);
if (hDIB == 0)
return FALSE;
// Read the remainder of the bitmap file.
if (file.ReadHuge((LPSTR)hDIB, 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
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)
{
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)
END_MESSAGE_MAP()
//********************************************************************************
//* CreateTransparent()
//*
//* Creates the main application window transparent
//********************************************************************************
void CTransparentWnd::CreateTransparent(LPCTSTR pTitle)
{
FindBmpFile(GetCurPath());
CDib dib(m_szBmpFile);
CRect rect(0,0,dib.m_nWidth,dib.m_nHeight);
CreateEx( WS_EX_TOOLWINDOW,
AfxRegisterWndClass(0),
pTitle,
WS_POPUP | WS_SYSMENU|WS_EX_TOOLWINDOW,
rect,
NULL,
NULL,
NULL );
SetupRegion(GetWindowDC());
//m_BitmapID = BitmapID;
SetTimer(1,100,NULL);
NOTIFYICONDATA tnd;
tnd.cbSize=sizeof(NOTIFYICONDATA);
tnd.hWnd=this->m_hWnd;
tnd.uID=IDR_MAINFRAME;
tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
tnd.uCallbackMessage=WM_LIBEN;
tnd.hIcon=LoadIcon(AfxGetInstance