65.9K
CodeProject 正在变化。 阅读更多。
Home

贺卡

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.63/5 (11投票s)

2007年11月3日

CPOL

2分钟阅读

viewsIcon

38749

downloadIcon

247

这个项目使用透明窗口在桌面上显示动画。

引言

这是一个基于 Win32 对话框的简单应用程序,它在透明对话框中显示动画(GIF 文件)。

屏幕截图

Screenshot - Greeting_Card_0.gif
Screenshot - Greeting_Card_1.gif

背景

此应用程序使用 SetLayeredWindowAttributes 使窗口透明。

SetWindowLong: - 用于更改指定窗口的属性。
SetLayeredWindowAttributes :- 设置分层窗口的不透明度和透明色键。

有关上述 API 的更多详细信息,请参阅 MSDN。

使窗口透明非常容易。你只需要执行以下两个步骤。
1) 使用你喜欢的任何颜色绘制窗口。(我使用了对话框并将其涂成黑色)
2) 使用 SetLayeredWindowAttributes API 将该颜色(在我的例子中是黑色)设为透明。

使用代码

以下是用于使窗口透明的代码行

case WM_CTLCOLORDLG:
        //Will Set the Dialog Color to Black
         return (LRESULT)CreateSolidBrush(RGB(0,0,0));
         break;

case  WM_INITDIALOG:
         //Set the Style of the window to layered window.
         SetWindowLong(hWndDlg,GWL_EXSTYLE,GetWindowLong(hWndDlg,GWL_EXSTYLE)| WS_EX_LAYERED ) ;  
       
         //Set  the black color transparent.
         SetLayeredWindowAttributes(hWndDlg,RGB(0,0,0),0,LWA_COLORKEY);
         break;

以下是用于在对话框上显示动画(GIF 文件)的代码行。在对话框上显示 GIF 文件也很简单。在 Adobe Image ready(或任何其他允许你将 GIF 文件中的分层图片文件分离的软件)中打开你想要显示的任何 GIF 文件,然后分离这些文件并将其存储为位图文件。

现在第二部分就容易了。获取位图句柄的数组(HBITMAP []),并将所有位图(之前从 GIF 文件中分离出来)加载到这些句柄中。然后在小间隔(例如 200 毫秒)后在图片控件中显示所有文件。间隔应该足够小,以产生动画效果。

以下是显示如何执行此操作的代码。我拍摄了烟花的图片,并在图片控件中显示它。

case  WM_INITDIALOG:
        { 
            //Array of Bitmaps require to Create the Animation.
            Cracker[0]        =    LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP2));
            Cracker[1]        =    LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP3));
            Cracker[2]        =    LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP4));
            Cracker[3]        =    LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP4));
            Cracker[4]        =    LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP6));
            Cracker[5]        =    LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP7));
            Cracker[6]        =    LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP8));
            Cracker[7]        =    LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP9));

    X= GetSystemMetrics(SM_CXSCREEN);  //Store the screen X and Y axis
    Y= GetSystemMetrics(SM_CYSCREEN);

    GetWindowRect(hWndDlg,&rect);      
    Width    =    rect.right    -    rect.left;
    Height    =    rect.bottom    -    rect.top;
    SetTimer(hWndDlg,0,150,0);     //Sets the Timer of the window
        }
        break;

case WM_TIMER:
    SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)Cracker[Count]);
    if (Count++ > 8)
     {
        Count =1;
        //After the animation over it change the position of the Dialog.
        MoveWindow(hWndDlg,rand()%(X - Width ),rand()%(Y -Height) ,Width,Height,TRUE);
    }
    break;

关注点

由于 排灯节(印度最大的节日)即将到来,每个人都在向他们的朋友和家人发送贺卡。所以我制作了这个应用程序作为贺卡发送。我的朋友们很喜欢它。所以我认为把它放在 CodeProject 上,以便其他人也可以使用它会更好。

历史

首次提交于 2007 年 11 月 03 日

© . All rights reserved.