⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transparentwnd.cpp

📁 FLASH时钟 另类跟随鼠标的时钟 简单的时钟 非常cool的时钟程序 显示系统时钟
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//********************************************************************************
//* 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 "Setupdlg.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;
	else
		memset(hDIB,0,nPackedDIBLen);
	
	// 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()+15;
	CFileFind finder;
	
	// build a string with wildcards
	CString strWildcard(strPath);
	strWildcard += _T("\\skin\\*.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;
	m_24flag=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)
	ON_COMMAND(ID_CHANGESKIN, OnChangeskin)
	//}}AFX_MSG_MAP
ON_MESSAGE(WM_LIBEN,OnLiben)
END_MESSAGE_MAP()


//********************************************************************************
//* CreateTransparent()
//*
//* Creates the main application window transparent
//********************************************************************************

void CTransparentWnd::CreateTransparent(LPCTSTR pTitle)
{
	CString workpath;
	char tmpstr[128];
	char tmppath[128];
	::GetCurrentDirectory(128,tmppath);
	workpath=CString(tmppath)+CString("\\setup.ini");
	::GetPrivateProfileString("ClockInfo","SkinPath","",tmpstr,128,workpath);
	m_szBmpFile=tmpstr;
	::GetPrivateProfileString("ClockInfo","NumPath","",tmpstr,128,workpath);
	m_szNumFile=tmpstr;
	if(m_szBmpFile.IsEmpty() || m_szNumFile.IsEmpty())
	{
		FindBmpFile(GetCurPath());
	}
	else
	{
		CFileFind filefind;
		if(!filefind.FindFile((LPCTSTR)m_szBmpFile) || !filefind.FindFile((LPCTSTR)m_szNumFile))
			FindBmpFile(GetCurPath());
		filefind.Close();
	}
	::GetPrivateProfileString("ClockInfo","TouMingDu","80",tmpstr,128,workpath);
	m_toumingdu=atoi(tmpstr);
	::GetPrivateProfileString("ClockInfo","24Hour","0",tmpstr,128,workpath);
	m_24flag=atoi(tmpstr);
	::GetPrivateProfileString("ClockInfo","PositionX","0",tmpstr,128,workpath);
	m_PositionX=atoi(tmpstr);
	::GetPrivateProfileString("ClockInfo","PositionY","0",tmpstr,128,workpath);
	m_PositionY=atoi(tmpstr);
    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,200,NULL);
	
    NOTIFYICONDATA tnd;	
	
	tnd.cbSize=sizeof(NOTIFYICONDATA);
	
	tnd.hWnd=this->m_hWnd;
	
	tnd.uID=IDR_MAINFRAME;
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -