Windows介面編程第六篇 動畫啟動效果(動畫效果顯示及隱藏視窗)

來源:互聯網
上載者:User

本文配套程式為:http://download.csdn.net/detail/morewindows/5128647

轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/8656068

歡迎關注微博:http://weibo.com/MoreWindows

 

前面已經有五篇文章介紹了Windows介面編程,目錄如下:

1. 《Windows介面編程第一篇 位元影像背景與位元影像畫刷》

2. 《Windows介面編程第二篇 半透明表單》

3. 《Windows介面編程第三篇 異形表單普通版》

4. 《Windows介面編程第四篇 異形表單高富帥版》

5. 《Windows介面編程第五篇 靜態控制項背景透明化》

 

下面再來三篇,分別為:

1.Windows介面編程第六篇 動畫啟動效果(動畫效果顯示及隱藏視窗)

2.Windows介面編程第七篇 檔案拖拽(檔案拖放)

3.Windows介面編程第八篇 listbox彩色顯示隔行變色

 

動畫效果顯示及隱藏視窗及大大美化程式介面,像QQ的登入視窗也是使用了動畫效果顯示及隱藏視窗。

本篇《Windows介面編程第六篇 動畫啟動效果(動畫效果顯示及隱藏視窗)》就來介紹下如何使用動畫效果來顯示隱藏視窗。為了方便代碼複用,本人已經將其封裝成一個視窗動畫效果類,先來看看這個視窗動畫效果類的實現吧。

標頭檔CAnimateWindow.h 

#pragma once//Windows介面編程第六篇 動畫啟動效果(動畫效果顯示及隱藏視窗)// http://blog.csdn.net/morewindows/article/details/8656068//By MoreWindows-(http://blog.csdn.net/MoreWindows)class CAnimateWindow{public:CAnimateWindow(HWND hwnd = NULL);void SetWindowHwnd(HWND hwnd);HWND GetWindowHwnd();BOOL AnimateWindow(DWORD dwTime = 400, BOOL bShow = TRUE, BOOL bSlide = TRUE);private:int GetRandomNumber();private:int   m_nAnimateType;HWND  m_hwndWindow;};

CPP檔案CAnimateWindow.cpp 

//Windows介面編程第六篇 動畫啟動效果(動畫效果顯示及隱藏視窗)// http://blog.csdn.net/morewindows/article/details/8656068//By MoreWindows-(http://blog.csdn.net/MoreWindows)#include <windows.h>#include <stdlib.h>#include <time.h>#include "CAnimateWindow.h"CAnimateWindow::CAnimateWindow(HWND hwnd){m_hwndWindow = hwnd;m_nAnimateType = GetRandomNumber();}void CAnimateWindow::SetWindowHwnd(HWND hwnd){m_hwndWindow = hwnd;}HWND CAnimateWindow::GetWindowHwnd(){return m_hwndWindow;}BOOL CAnimateWindow::AnimateWindow(DWORD dwTime, BOOL bShow, BOOL bSlide){DWORD dwFlags;dwFlags = bShow ? AW_ACTIVATE : AW_HIDE;dwFlags |= bSlide ? AW_SLIDE : AW_BLEND;if (m_nAnimateType == 0)dwFlags |= AW_HOR_POSITIVE;else if (m_nAnimateType == 1)dwFlags |= AW_VER_POSITIVE;elsedwFlags |= AW_CENTER;return ::AnimateWindow(m_hwndWindow, dwTime, dwFlags);}int CAnimateWindow::GetRandomNumber(){srand((UINT)time(NULL));return rand() % 3;}

代碼非常簡單,主要使用的是AnimateWindow函數。這個函數的函數原型如下:

BOOLAnimateWindow(        

    HWNDhwnd,

    DWORDdwTime,

    DWORDdwFlags

);

這個函數也極其簡單,三個參數,一個表示視窗控制代碼,一個表示動畫期間(毫秒),最後一個參數表示動畫效果。可以有以下參數:

AW_SLIDE

Uses slide animation. By default, roll animation is used. This flag is ignored when used with AW_CENTER.

AW_ACTIVATE

Activates the window. Do not use this value with AW_HIDE.

AW_BLEND

Uses a fade effect. This flag can be used only if hwnd is a top-level window.

AW_HIDE

Hides the window. By default, the window is shown.

AW_CENTER

Makes the window appear to collapse inward if AW_HIDE is used or expand outward if the AW_HIDE is not used. The various direction flags have no effect.

AW_HOR_POSITIVE

Animates the window from left to right. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.

AW_HOR_NEGATIVE

Animates the window from right to left. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.

AW_VER_POSITIVE

Animates the window from top to bottom. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.

AW_VER_NEGATIVE

Animates the window from bottom to top. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.

參數有點多但歸納一下就知道分為水平方向,垂直方向,由裡向外或由外向裡這幾種。

 

下面給出這個動畫顯示視窗類別的使用範例。

代碼中的CenterWindow是用來將視窗置中顯示的,這個函數中的GetSystemMetrics(SM_CXSCREEN);和GetSystemMetrics(SM_CYSCREEN);可以參考《VC++擷取螢幕大小第一篇 像素大小GetSystemMetrics》。

//Windows介面編程第六篇 動畫啟動效果(動畫效果顯示及隱藏視窗)// http://blog.csdn.net/morewindows/article/details/8656068//By MoreWindows-(http://blog.csdn.net/MoreWindows) #include "stdafx.h"#include "resource.h"#include "CAnimateWindow.h"BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);//使視窗置中void CenterWindow(HWND hwnd);int APIENTRY WinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPSTR     lpCmdLine,                     int       nCmdShow){ // TODO: Place code here.DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);return 0;}BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){static HBRUSH s_hBrushDlgBackGround;static CAnimateWindow   s_aw;switch (message){case WM_INITDIALOG:{SetWindowText(hDlg, "視窗的動畫顯示和隱藏 - MoreWindows");// 載入背影圖片HINSTANCE hinstance = (HINSTANCE)GetWindowLong(hDlg, GWL_HINSTANCE);HBITMAP hBitmap = LoadBitmap(hinstance, MAKEINTRESOURCE(IDB_BITMAP1));s_hBrushDlgBackGround = CreatePatternBrush(hBitmap);CenterWindow(hDlg);s_aw.SetWindowHwnd(hDlg);s_aw.AnimateWindow(600);}return FALSE;case WM_COMMAND:switch (LOWORD(wParam)){case IDOK:case IDCANCEL:s_aw.AnimateWindow(400, FALSE);EndDialog(hDlg, FALSE);return TRUE;}break;case WM_CTLCOLORDLG:return (BOOL)s_hBrushDlgBackGround;}return FALSE;}//使視窗置中void CenterWindow(HWND hwnd){RECT   rcDlg;int    nDlgWidth, nDlgHight;int    nScreenWidth, nScreenHight;nScreenWidth = GetSystemMetrics(SM_CXSCREEN);nScreenHight = GetSystemMetrics(SM_CYSCREEN);GetWindowRect(hwnd, &rcDlg);nDlgWidth = rcDlg.right - rcDlg.left;nDlgHight = rcDlg.bottom - rcDlg.top;MoveWindow(hwnd, (nScreenWidth  - nDlgWidth) / 2, (nScreenHight - nDlgHight) / 2, nDlgWidth, nDlgHight, FALSE);} 

運行效果如下所示,當然動畫效果不好,大家可以從http://download.csdn.net/detail/morewindows/5128647下載(帶源碼且免積分),然後自己運行看下效果。

 

歡迎繼續參考下面兩篇《Windows介面編程第七篇檔案拖拽(檔案拖放)》和

《Windows介面編程第八篇 listbox彩色顯示隔行變色》

地址分別為

1.http://blog.csdn.net/morewindows/article/details/8634451

2.http://blog.csdn.net/morewindows/article/details/8656061

 

本文配套程式為:http://download.csdn.net/detail/morewindows/5128647

轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/8656068

歡迎關注微博:http://weibo.com/MoreWindows

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.