/*==============================
Roses.cpp -- 一千朵玫瑰
(c) Snow-dream, 2008
QQ:529698939
E-mail:xzm2@qq.com
编写日期:2008-12-3
VC 6.0环境下编译通过
===============================*/
#include <windows.h>
#include <stdlib.h>
#include "resource.h"
#define WS_EX_LAYERED 0x00080000
#define LWA_ALPHA 0x00000002
#define LWA_COLORKEY 0x00000001
#define ID_TIMER 1
#define IMGCOUNT 6 //图片数
//因为没有导出SetLayeredWindowAttributes API 所以要我们自己到user32.dll里搜索
typedef BOOL (WINAPI *LPFNSETLAYEREDWINDOWATTRIBUTES)
(HWND hwnd,COLORREF crKey,BYTE bAlpha,DWORD dwFlags) ;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
//设置窗口的透明度 hwnd是被设置的窗口 byAlpha是指示了透明度(0完全透明 255完全不透明)
BOOL TransparentWnd(HWND hwnd, BYTE byAlpha)
{
long lStyle;
HMODULE hModule;
BOOL bRet;
LPFNSETLAYEREDWINDOWATTRIBUTES lpSetLayeredWindowAttributes;
if(FALSE == IsWindow(hwnd))
{
MessageBox(NULL, TEXT("窗口句柄无效!"), NULL, 0) ;
return FALSE ;
}
hModule = (HMODULE)LoadLibrary(TEXT("user32.dll")) ;
if(hModule)
{
lpSetLayeredWindowAttributes = (LPFNSETLAYEREDWINDOWATTRIBUTES)
GetProcAddress(hModule, TEXT("SetLayeredWindowAttributes")) ;
if(!lpSetLayeredWindowAttributes)
{
MessageBox(hwnd, TEXT("未找到API SetLayeredWindowAttributes的地址!"),
NULL, MB_OK) ;
return FALSE ;
}
}
lStyle = GetWindowLong(hwnd, GWL_EXSTYLE) ;
if(!lStyle)
{
MessageBox(hwnd, TEXT("获取窗口扩展样式失败!"), NULL, MB_OK) ;
return FALSE ;
}
lStyle = lStyle | WS_EX_LAYERED ;
bRet = SetWindowLong(hwnd, GWL_EXSTYLE, lStyle) ;
if(!bRet)
{
MessageBox(hwnd, TEXT("设置窗口扩展样式失败!"), NULL, MB_OK) ;
return FALSE ;
}
bRet = lpSetLayeredWindowAttributes(hwnd, 0, byAlpha, LWA_ALPHA) ;
if(!bRet)
{
MessageBox(hwnd, TEXT("设置窗口透明度失败!"), NULL, MB_OK) ;
return FALSE ;
}
RedrawWindow(hwnd, NULL, NULL,
RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN) ;
FreeLibrary(hModule) ;
return TRUE ;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Roses") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
BOOL bRet ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, szAppName) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if(!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ;
return 0;
}
srand ((int)GetTickCount()) ; //初始化随机数发生器
hwnd = CreateWindow (szAppName, TEXT ("玫瑰花"),
WS_DLGFRAME | WS_POPUP,
0, 0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL, NULL, hInstance, NULL) ;
TransparentWnd(hwnd, 1) ; //设置窗口透明度
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOREDRAW) ; //窗口置顶
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while ( (bRet = GetMessage (&msg, NULL, 0, 0) ) != 0 )
{
if (bRet == -1)
{
MessageBox (hwnd, TEXT ("Such code can lead to fatal application errors"),
szAppName, MB_ICONERROR) ;
return -1 ;
}
else
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hBmpI[IMGCOUNT], hBmpM[IMGCOUNT] ;
HINSTANCE hInstance = NULL ;
HDC hdc, hdcMemImag, hdcMemMask ;
BITMAP bmp ;
int i, x, y ;
switch (message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
for(i=0; i<IMGCOUNT; i++)
{
TCHAR szBmpName[16] ;
wsprintf(szBmpName, TEXT("BM_I%d"), i) ;
hBmpI[i] = LoadBitmap (hInstance, szBmpName) ; //载入位图
wsprintf(szBmpName, TEXT("BM_M%d"), i) ;
hBmpM[i] = LoadBitmap (hInstance, szBmpName) ; //载入相应的屏蔽位图
}
SetTimer(hwnd, ID_TIMER, 100, NULL) ; //设置定时器100毫秒
return 0 ;
//按鼠标左键 或者 键盘 退出程序
case WM_LBUTTONDOWN:
case WM_KEYDOWN:
SendMessage(hwnd, WM_CLOSE, 0, 0) ;
return 0;
case WM_TIMER: //最主要的部分 显示透明位图
hdc = GetDC(hwnd) ;
i = rand()%IMGCOUNT ;
// 创建一个与指定设备兼容的内存设备上下文环境(DC)
hdcMemImag = CreateCompatibleDC (hdc) ;
DeleteObject(SelectObject (hdcMemImag, hBmpI[i])) ;
GetObject (hBmpI[i], sizeof (BITMAP), &bmp) ;
hdcMemMask = CreateCompatibleDC (hdc) ;
DeleteObject(SelectObject (hdcMemMask, hBmpM[i])) ;
ReleaseDC(hwnd, hdc) ; //释放设备上下文环境(DC)
//x、y为位图显示的坐标
x = rand()%GetSystemMetrics(SM_CXSCREEN) - bmp.bmWidth/2 ;
y = rand()%GetSystemMetrics(SM_CYSCREEN) - bmp.bmHeight/2 ;
hdc= GetDC(NULL) ;
//画出位图
BitBlt (hdc, x, y, bmp.bmWidth, bmp.bmHeight, hdcMemMask, 0, 0, SRCAND) ;
BitBlt (hdc, x, y, bmp.bmWidth, bmp.bmHeight, hdcMemImag, 0, 0, SRCPAINT) ;
//删除指定的设备上下文环境(DC)
DeleteDC (hdcMemMask) ;
DeleteDC (hdcMemImag) ;
ReleaseDC(NULL, hdc) ;
return 0 ;
case WM_DESTROY:
// 删除定时器
KillTimer(hwnd, ID_TIMER) ;
// 删除位图、屏蔽位图
for(i=0; i<IMGCOUNT; i++)
{
DeleteObject (hBmpI[i]) ;
DeleteObject (hBmpM[i]) ;
}
InvalidateRect(NULL, NULL, TRUE) ; //更新整个屏幕
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
评论16
最新资源