系统相关
首页 > 系统相关> > Windows界面编程第六篇 动画启动效果(动画效果显示及隐藏窗口)

Windows界面编程第六篇 动画启动效果(动画效果显示及隐藏窗口)

作者:互联网

分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net

本文配套程序下载地址为: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;
	else
		dwFlags |= 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

 

 

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net

标签:动画,http,Windows,编程,AW,net,第六篇
来源: https://www.cnblogs.com/heishanglaoyao/p/10488829.html