我的环境:WIN7 X64 + VS2010
------------------------------------------------------------------------------------------------------------------------------------------------------------
最近在看易语言的官方教程时(即:《十天学会易语言图解教程》),在:第10章-API应用 中有个水纹特效的例子,
看后觉得很是不错,于是想将其搬到VC中来,无奈教程里面只要一个dll文件:WaterDll.dll
在VC中使用dll时,一般都要有相应的xxx.h头文件和xxx.lib库文件,既然没有头文件和库文件,我只能动态调用了。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
效果截图:
-----------------------------------------------------------------------------------------------------------------------------------------------------------
为了方便动态调用WaterDll.dll,我封装了一个类CWaterWave:
WaterWave.h内容如下:
#pragma once
/************************************************************************/
/* http://blog.csdn.net/friendan */
/************************************************************************/
class CWaterWave
{
public:
CWaterWave(void);
~CWaterWave(void);
public:
// 各个参数是什么,没找到官方解释,自己多试几个参数看看效果吧
int WaterInit(int bitmap);
int WaterMouseAction(int hdc, int sx, int sy, int mx, int my, int half, int deep);
int WaterTimer(int hdc, int sx, int sy);
private:
// 函数原型定义
typedef int (_stdcall *PFN_WaterInit)(int bitmap);
typedef int (_stdcall *PFN_WaterMouseAction)(int hdc, int sx, int sy, int mx, int my, int half, int deep);
typedef int (_stdcall *PFN_WaterTimer)(int hdc, int sx, int sy);
// 函数指针
PFN_WaterInit m_pfnWaterInit;
PFN_WaterMouseAction m_pfnWaterMouseAction;
PFN_WaterTimer m_pfnWaterTimer;
HMODULE m_hWaterDll;
};
----------------------------------------------------------------------
WaterWave.cpp文件内容如下:
#include "StdAfx.h"
#include "WaterWave.h"
CWaterWave::CWaterWave(void)
{
m_pfnWaterInit = NULL;
m_pfnWaterMouseAction = NULL;
m_pfnWaterTimer = NULL;
m_hWaterDll = LoadLibrary("WaterDll.dll");
m_pfnWaterInit = (PFN_WaterInit)GetProcAddress(m_hWaterDll, "WaterInit");
m_pfnWaterMouseAction = (PFN_WaterMouseAction)GetProcAddress(m_hWaterDll, "WaterMouseAction");;
m_pfnWaterTimer = (PFN_WaterTimer)GetProcAddress(m_hWaterDll, "WaterTimer");;
}
CWaterWave::~CWaterWave(void)
{
if (m_hWaterDll != NULL)
{
FreeLibrary(m_hWaterDll);
m_hWaterDll = NULL;
}
}
int CWaterWave::WaterInit( int bitmap )
{
if (m_pfnWaterInit == NULL)
{
return -1;
}
return m_pfnWaterInit(bitmap);
}
int CWaterWave::WaterMouseAction( int hdc, int sx, int sy, int mx, int my, int half, int deep )
{
if (m_pfnWaterMouseAction == NULL)
{
return -1;
}
return m_pfnWaterMouseAction(hdc, sx, sy, mx, my, half, deep);
}
int CWaterWave::WaterTimer( int hdc, int sx, int sy )
{
if (m_pfnWaterTimer == NULL)
{
return -1;
}
return m_pfnWaterTimer(hdc, sx, sy);
}
-----------------------------------------------------------------------------------------------------
封装好了类CWaterWave,在MFC中调用就方便多了,其实WaterDll.dll里面的接口不多,我们只要调用以下3个接口,就可以实现水纹特效了:
WaterInit
WaterMouseAction
WaterTimer
-----------------------------------------------------------------------------------
1. 在我们的对话框头文件WaterWindowDlg.h声明一个对象:
CWaterWave m_waterWave;
------------------------------------------------------------
2. 在 工程的资源中插入一副位图,ID设置为:IDB_BMP_SEA
------------------------------------------------------------------------------------------------
3. 在对话框的OnInitDialog函数里面增加以下代码:
// 水纹特效初始化
int iBitmap = (int)::LoadImage(::GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BMP_SEA), IMAGE_BITMAP, 0, 0, LR_VGACOLOR);
if (iBitmap != NULL)
{
m_waterWave.WaterInit(iBitmap);
}
// 开个定时器来处理水纹
SetTimer(2015, 10, NULL);
-----------------------------------------------------------------
4. 定时器处理过程代码如下:
// 定时器
void CWaterWindowDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
HDC hDCWnd = ::GetDC(this->GetSafeHwnd());
m_waterWave.WaterTimer((INT)hDCWnd, 0, 0);
::ReleaseDC(this->GetSafeHwnd(), hDCWnd);
CDialogEx::OnTimer(nIDEvent);
}
------------------------------------------------------------------------
5. 处理鼠标在窗体的移动事件,在该事件中显示水纹:
// 鼠标移动事件
void CWaterWindowDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
HDC hDCWnd = ::GetDC(this->GetSafeHwnd());
m_waterWave.WaterMouseAction ((INT)hDCWnd, 0, 0, point.x, point.y, 5, 100);
::ReleaseDC(this->GetSafeHwnd(), hDCWnd);
CDialogEx::OnMouseMove(nFlags, point);
}
----------------------------------------------------------------
以上例子下载:
http://download.csdn.net/detail/friendan/8891081