相信有好多朋友都知道这个程序 但是没给源码 呵呵 其实我们可以自己实现的 代码很简单(另外这里 玫瑰并不止1000朵 而是无数朵 因为简单起见 没加玫瑰的统计代码 呵呵)
现简单介绍实现过程
首先打开VC++ 6.0 新建个 “Win32 Application” 工程 工程名就叫“Roses”吧
然后要做的是 下载原来的那个“一千枝会动的玫瑰” 然后我们用“eXeScope”提取里面的6张位图 和相应的6张掩码位图 加入到我们的工程
分别给6张位图的ID 命名为:"BM_I0"、"BM_I1"、...、"BM_I5"
给6张位图对应的掩码位图ID 命名为:"BM_M0"、"BM_M1"、...、"BM_M5"
OK
因为那个“一千枝会动的玫瑰”运行后按鼠标左键 或者 键盘退出
所以个人觉得 此程序运行后是最大化的 并且程序的窗口是透明的,
但是窗口透明的话,画上去的位图不就看不到了,这个问题也是想了比较久。
最后想了个投机取巧的办法,给GetDC函数传NULL,以检索整个屏幕的设备上下文环境
不知道原程序是如何实现的
这样再画图就没问题了
好了下面代码:
- /*==============================
- Roses.cpp -- 一千朵玫瑰
- (c) Snow-dream, 2008
- QQ:529698939
- E-mail:[email protected]
- 编写日期: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) ;
- }
//资源文件 Roses.rc
- //Microsoft Developer Studio generated resource script.
- //
- #include "resource.h"
- #define APSTUDIO_READONLY_SYMBOLS
- /////////////////////////////////////////////////////////////////////////////
- //
- // Generated from the TEXTINCLUDE 2 resource.
- //
- #include "afxres.h"
- /////////////////////////////////////////////////////////////////////////////
- #undef APSTUDIO_READONLY_SYMBOLS
- /////////////////////////////////////////////////////////////////////////////
- // Chinese (中国) resources
- #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
- #ifdef _WIN32
- LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
- #pragma code_page(936)
- #endif //_WIN32
- /////////////////////////////////////////////////////////////////////////////
- //
- // Icon
- //
- // Icon with lowest ID value placed first to ensure application icon
- // remains consistent on all systems.
- ROSES ICON DISCARDABLE "rose.ico"
- #ifdef APSTUDIO_INVOKED
- /////////////////////////////////////////////////////////////////////////////
- //
- // TEXTINCLUDE
- //
- 1 TEXTINCLUDE DISCARDABLE
- BEGIN
- "resource.h/0"
- END
- 2 TEXTINCLUDE DISCARDABLE
- BEGIN
- "#include ""afxres.h""/r/n"
- "/0"
- END
- 3 TEXTINCLUDE DISCARDABLE
- BEGIN
- "/r/n"
- "/0"
- END
- #endif // APSTUDIO_INVOKED
- /////////////////////////////////////////////////////////////////////////////
- //
- // Bitmap
- //
- BM_I0 BITMAP DISCARDABLE "BM_I0.bmp"
- BM_I1 BITMAP DISCARDABLE "BM_I1.bmp"
- BM_I2 BITMAP DISCARDABLE "BM_I2.bmp"
- BM_I3 BITMAP DISCARDABLE "BM_I3.bmp"
- BM_I4 BITMAP DISCARDABLE "BM_I4.bmp"
- BM_I5 BITMAP DISCARDABLE "BM_I5.bmp"
- BM_M0 BITMAP DISCARDABLE "BM_M0.bmp"
- BM_M1 BITMAP DISCARDABLE "BM_M1.bmp"
- BM_M2 BITMAP DISCARDABLE "BM_M2.bmp"
- BM_M3 BITMAP DISCARDABLE "BM_M3.bmp"
- BM_M4 BITMAP DISCARDABLE "BM_M4.bmp"
- BM_M5 BITMAP DISCARDABLE "BM_M5.bmp"
- #endif // Chinese (中国) resources
- /////////////////////////////////////////////////////////////////////////////
- #ifndef APSTUDIO_INVOKED
- /////////////////////////////////////////////////////////////////////////////
- //
- // Generated from the TEXTINCLUDE 3 resource.
- //
- /////////////////////////////////////////////////////////////////////////////
- #endif // not APSTUDIO_INVOKED
//头文件 resource.h
- //{{NO_DEPENDENCIES}}
- // Microsoft Developer Studio generated include file.
- // Used by Roses.rc
- //
- // Next default values for new objects
- //
- #ifdef APSTUDIO_INVOKED
- #ifndef APSTUDIO_READONLY_SYMBOLS
- #define _APS_NEXT_RESOURCE_VALUE 114
- #define _APS_NEXT_COMMAND_VALUE 40001
- #define _APS_NEXT_CONTROL_VALUE 1000
- #define _APS_NEXT_SYMED_VALUE 101
- #endif
- #endif
//程序运行效果图
//程序完整源码下载地址(附加可执行文件)