Ease-缓动

示例代码:

#pragma comment(lib, "XCGUI.lib")
#include "xcgui.h"
class CMyWindowDemo
{
public:
HWINDOW m_hWindow;
ease_flag_ m_easeFlag; //缓动方式
int m_easeType; //缓动类型
int m_pos; //当前位置
int m_time; //缓动点数量
int m_time_pos; //当前点
RECT m_rect; //窗口客户区坐标
int m_windowType; //窗口水平或垂直缓动
CMyWindowDemo()
{
m_easeFlag= easeOut;
m_easeType = 11;
m_pos = 0;
m_time = 60;
m_time_pos = 0;
m_windowType = 2;
Init();
}
void Init()
{
m_hWindow = XWnd_Create(0, 0, 700, 450, L"炫彩界面库(XCGUI) - 缓动测试");
XWnd_EnableDragWindow(m_hWindow, TRUE);
XWnd_EnableDragBorder(m_hWindow, FALSE);
int left =30;
int top = 35;
CreateButton(2, 11, left, top, 100, L"Linear"); left += 105;
CreateButton(2, 12, left, top, 100, L"Quadratic"); left += 105;
CreateButton(2, 13, left, top, 100, L"Cubic"); left += 105;
CreateButton(2, 14, left, top, 100, L"Quartic"); left += 105;
CreateButton(2, 15, left, top, 100, L"Quintic"); left += 105;
left = 30;
top += 30;
CreateButton(2, 16, left, top, 100, L"Sinusoidal"); left += 105;
CreateButton(2, 17, left, top, 100, L"Exponential"); left += 105;
CreateButton(2, 18, left, top, 100, L"Circular"); left += 105;
left = 30;
top += 30;
CreateButton(2, 19, left, top, 100, L"Elastic"); left += 105;
CreateButton(2, 20, left, top, 100, L"Back"); left += 105;
CreateButton(2, 21, left, top, 100, L"Bounce"); left += 105;
left = 30;
top += 40;
CreateButton(1, 0, left, top, 100, L"easeIn"); left += 105;
CreateButton(1, 1, left, top, 100, L"easeOut"); left += 105;
CreateButton(1, 2, left, top, 100, L"easeInOut"); left += 105;
HELE hButton = XBtn_Create(445, top, 100, 24, L"快速", m_hWindow);
XBtn_SetType(hButton, button_type_check);
XBtn_SetCheck(hButton, TRUE);
XEle_RegEventCPP(hButton, XE_BUTTON_CHECK, &CMyWindowDemo::OnBtnCheckSlow);
hButton = XBtn_Create(600, 10, 60, 50, L"Exit", m_hWindow);
XBtn_SetType(hButton, button_type_close);
hButton = XBtn_Create(445, 65, 100, 24, L"从左向右", m_hWindow);
XEle_RegEventCPP(hButton, XE_BUTTON_CHECK, &CMyWindowDemo::OnBtnCheck_LeftToRight);
hButton = XBtn_Create(445, 92, 100, 24, L"从上向下", m_hWindow);
XBtn_SetTypeEx(hButton, button_type_radio); XBtn_SetGroupID(hButton, 3); XBtn_SetCheck(hButton, TRUE);
XEle_RegEventCPP(hButton, XE_BUTTON_CHECK, &CMyWindowDemo::OnBtnCheck_TopToBottom);
hButton = XBtn_Create(550, 65, 110, 50, L"Run - 窗口缓动", m_hWindow);
XEle_RegEventCPP(hButton, XE_BNCLICK, &CMyWindowDemo::OnBtnStartWindow);
hButton = XBtn_Create(550, 120, 110, 50, L"Run - 缓动曲线", m_hWindow);
XEle_RegEventCPP(hButton, XE_BNCLICK, &CMyWindowDemo::OnBtnStart);
XWnd_AdjustLayout(m_hWindow);
XWnd_ShowWindow(m_hWindow,SW_SHOW);
XWnd_RegEventCPP(m_hWindow, WM_PAINT, &CMyWindowDemo::OnDrawWindow);
RECT rect;
GetWindowRect(XWnd_GetHWND(m_hWindow), &rect);
int y = 0;
for (int t = 0; t <= 30; t++)
{
float v = XEase_Bounce((float)t / 30, easeOut);
y = v* rect.top;
XWnd_Move(m_hWindow, rect.left, y);
XWnd_Redraw(m_hWindow,TRUE);
Sleep(10);
}
}
void CreateButton(int nGroup, int id, int x, int y, int cx, const wchar_t* title)
{
HELE hButton = XBtn_Create(x, y, cx, 22, title, m_hWindow);
XBtn_SetGroupID(hButton, nGroup); XEle_SetID(hButton, id);
if (1 == id || 21==id) XBtn_SetCheck(hButton, TRUE);
XEle_RegEventCPP1(hButton, XE_BUTTON_CHECK, &CMyWindowDemo::OnButtonCheck);
}
int OnButtonCheck(HELE hButton, BOOL bCheck, BOOL *pbHandled)
{
if (!bCheck) return 0;
int id = XEle_GetID(hButton);
if (id <= 2)
m_easeFlag=(ease_flag_)id;
else
m_easeType = id-10;
XWnd_Redraw(m_hWindow, TRUE);
return 0;
}
int OnBtnCheckSlow(BOOL bCheck, BOOL *pbHandled)
{
if (bCheck)
m_time = 60;
else
m_time = 120;
return 0;
}
int OnBtnStart(BOOL *pbHandled)
{
float width = 400.0f;
for (int t = 0; t <= m_time; t++)
{
float v = 0;
switch (m_easeType)
{
case 1: v = XEase_Linear((float)t / m_time); break;
case 2: v = XEase_Quad((float)t / m_time, m_easeFlag); break;
case 3: v = XEase_Cubic((float)t / m_time, m_easeFlag); break;
case 4: v = XEase_Quart((float)t / m_time, m_easeFlag); break;
case 5: v = XEase_Quint((float)t / m_time, m_easeFlag); break;
case 6: v = XEase_Sine((float)t / m_time, m_easeFlag); break;
case 7: v = XEase_Expo((float)t / m_time, m_easeFlag); break;
case 8: v = XEase_Circ((float)t / m_time, m_easeFlag); break;
case 9: v = XEase_Elastic((float)t / m_time, m_easeFlag); break;
case 10:v = XEase_Back((float)t / m_time, m_easeFlag); break;
case 11:v = XEase_Bounce((float)t / m_time, m_easeFlag); break;
}
m_pos = v* width;
m_time_pos = t;
Sleep(10);
RECT rc = m_rect;
rc.top = 170;
XWnd_RedrawRect(m_hWindow, &rc, TRUE);
}
return 0;
}
int OnBtnStartWindow(BOOL *pbHandled)
{
RECT rect;
GetWindowRect(XWnd_GetHWND(m_hWindow), &rect);
int time = m_time / 2;
for (int t = 0; t <= time; t++)
{
float v = 0;
switch (m_easeType)
{
case 1: v = XEase_Linear((float)t / time); break;
case 2: v = XEase_Quad((float)t / time, m_easeFlag); break;
case 3: v = XEase_Cubic((float)t / time, m_easeFlag); break;
case 4: v = XEase_Quart((float)t / time, m_easeFlag); break;
case 5: v = XEase_Quint((float)t / time, m_easeFlag); break;
case 6: v = XEase_Sine((float)t / time, m_easeFlag); break;
case 7: v = XEase_Expo((float)t / time, m_easeFlag); break;
case 8: v = XEase_Circ((float)t / time, m_easeFlag); break;
case 9: v = XEase_Elastic((float)t / time, m_easeFlag); break;
case 10:v = XEase_Back((float)t / time, m_easeFlag); break;
case 11:v = XEase_Bounce((float)t / time, m_easeFlag); break;
}
if (1 == m_windowType)
{
int x = v * rect.left;
XWnd_Move(m_hWindow, x, rect.top);
} else
{
int y = v * rect.top;
XWnd_Move(m_hWindow, rect.left, y);
}
XWnd_Redraw(m_hWindow, TRUE);
Sleep(10);
}
return 0;
}
int OnBtnCheck_LeftToRight(BOOL bCheck, BOOL *pbHandled)
{
if(bCheck)
m_windowType = 1;
return 0;
}
int OnBtnCheck_TopToBottom(BOOL bCheck, BOOL *pbHandled)
{
if(bCheck)
m_windowType = 2;
return 0;
}
int OnDrawWindow(HDRAW hDraw, BOOL* pbHandled)
{
*pbHandled = TRUE;
RECT rect;
XWnd_GetClientRect(m_hWindow, &rect);
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(230, 230, 230,255));
XDraw_FillRect(hDraw, &rect);
m_rect = rect;
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(200, 200, 200,255));
XDraw_FrameRect(hDraw, &rect);
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(0, 0, 200,255));
XDraw_TextOutEx(hDraw, 260, 10, L"炫彩界面库(XCGUI) - 缓动测试");
RECT rc;
rc.left = 150;
rc.top = 190;
rc.right = rc.left+400 + 30;
rc.bottom = rc.top + 50;
{
RECT rcBorder = rc;
rcBorder.left-=2;
rcBorder.top-=2;
rcBorder.right+=2;
rcBorder.bottom+=2;
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(0, 0, 200,255));
XDraw_FrameRect(hDraw, &rcBorder);
}
RECT rcFill=rc;
rcFill.left = rcFill.left + m_pos;
rcFill.right = rcFill.left + 30;
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(128, 0, 0,255));
XDraw_FillRect(hDraw, &rcFill);
RECT rcBorder_Line;
rcBorder_Line.left = 150;
rcBorder_Line.right = 150 + 400;
rcBorder_Line.top = 255;
rcBorder_Line.bottom = 255+ 180;
{
RECT rcBorder = rcBorder_Line;
rcBorder.right++;
rcBorder.bottom++;
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(180, 180, 180,255));
XDraw_FrameRect(hDraw, &rcBorder);
}
POINT pts[121];
int x = rcBorder_Line.left;
int y = rcBorder_Line.bottom;
for (int t = 0; t <= m_time; t++)
{
float v = 0;
switch (m_easeType)
{
case 1: v = XEase_Linear((float)t / m_time); break;
case 2: v = XEase_Quad((float)t / m_time, m_easeFlag); break;
case 3: v = XEase_Cubic((float)t / m_time, m_easeFlag); break;
case 4: v = XEase_Quart((float)t / m_time, m_easeFlag); break;
case 5: v = XEase_Quint((float)t / m_time, m_easeFlag); break;
case 6: v = XEase_Sine((float)t / m_time, m_easeFlag); break;
case 7: v = XEase_Expo((float)t / m_time, m_easeFlag); break;
case 8: v = XEase_Circ((float)t / m_time, m_easeFlag); break;
case 9: v = XEase_Elastic((float)t / m_time, m_easeFlag); break;
case 10:v = XEase_Back((float)t / m_time, m_easeFlag); break;
case 11:v = XEase_Bounce((float)t / m_time, m_easeFlag); break;
}
pts[t].x = rc.left + t / (float)m_time * 400;
pts[t].y= rcBorder_Line.bottom - v * 180;
}
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(128, 0, 0,255));
int left = rc.left + m_time_pos / (float)m_time *400.0f;
XDraw_DrawLine(hDraw, left, rcBorder_Line.top, left, rcBorder_Line.bottom);
XDraw_DrawCurve(hDraw, pts, m_time+1, 0.5);
return 0;
}
};
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
XInitXCGUI(TRUE);
CMyWindowDemo MyWindow;
return 0;
}
#define XE_BNCLICK
按钮点击事件.
Definition: XCGUI.h:939
#define XE_BUTTON_CHECK
按钮选中事件
Definition: XCGUI.h:943
@ button_type_radio
单选按钮
Definition: XCGUI.h:295
@ button_type_close
窗口关闭按钮
Definition: XCGUI.h:299
@ button_type_check
多选按钮
Definition: XCGUI.h:297
BOOL WINAPI XBtn_SetCheck(HELE hEle, BOOL bCheck)
按钮_置选中 设置选中状态.
Definition: ButtonUI.cpp:1800
void WINAPI XBtn_SetTypeEx(HELE hEle, XC_OBJECT_TYPE_EX nType)
按钮_置类型扩展 设置按钮类型并自动修改样式和文本对齐方式.
Definition: ButtonUI.cpp:1838
HELE WINAPI XBtn_Create(int x, int y, int cx, int cy, const wchar_t *pName, HXCGUI hParent)
按钮_创建 创建按钮元素
Definition: ButtonUI.cpp:1765
void WINAPI XBtn_SetGroupID(HELE hEle, int nID)
按钮_置组ID 设置组ID.
Definition: ButtonUI.cpp:1848
void WINAPI XDraw_DrawLine(HDRAW hDraw, int x1, int y1, int x2, int y2)
绘制_线条 绘制线条.
Definition: DrawDC.cpp:2916
void WINAPI XDraw_DrawCurve(HDRAW hDraw, in_buffer_ POINT *points, int count, float tension)
@绘制_曲线 D2D暂时留空
Definition: DrawDC.cpp:2940
void WINAPI XDraw_FillRect(HDRAW hDraw, RECT *pRect)
@绘制_填充矩形
Definition: DrawDC.cpp:2633
void WINAPI XDraw_EnableSmoothingMode(HDRAW hDraw, BOOL bEnable)
绘制_启用平滑模式 启用平滑模式.
Definition: DrawDC.cpp:2613
void WINAPI XDraw_TextOutEx(HDRAW hDraw, int xStart, int yStart, const wchar_t *pString)
@绘制_文本扩展
Definition: DrawDC.cpp:3408
void WINAPI XDraw_SetBrushColor(HDRAW hDraw, COLORREF color)
绘制_置画刷颜色 设置画刷颜色.
Definition: DrawDC.cpp:2536
ease_flag_
Definition: XCGUI.h:1942
@ easeOut
从快到慢
Definition: XCGUI.h:1933
float WINAPI XEase_Expo(float pos, ease_type_ flag)
@缓动_Expo 突击曲线, 突然一下
Definition: Ease.cpp:260
float WINAPI XEase_Circ(float pos, ease_type_ flag)
@缓动_Circ 圆环, 好比绕过一个圆环
Definition: Ease.cpp:271
float WINAPI XEase_Elastic(float pos, ease_type_ flag)
@缓动_Elastic 强力回弹
Definition: Ease.cpp:282
float WINAPI XEase_Cubic(float pos, ease_type_ flag)
@缓动_Cubic 三次方曲线 圆弧
Definition: Ease.cpp:216
float WINAPI XEase_Sine(float pos, ease_type_ flag)
@缓动_Sine 正弦曲线, 在末端变化
Definition: Ease.cpp:249
float WINAPI XEase_Quart(float pos, ease_type_ flag)
@缓动_Quart 四方曲线
Definition: Ease.cpp:227
float WINAPI XEase_Quint(float pos, ease_type_ flag)
@缓动_Quint 五次方曲线
Definition: Ease.cpp:238
float WINAPI XEase_Back(float pos, ease_type_ flag)
@缓动_Back 回弹, 比较缓慢
Definition: Ease.cpp:293
float WINAPI XEase_Bounce(float pos, ease_type_ flag)
@缓动_Bounce 弹跳 模拟小球落地弹跳
Definition: Ease.cpp:304
float WINAPI XEase_Linear(float pos)
@缓动_Linear 线性
Definition: Ease.cpp:196
float WINAPI XEase_Quad(float pos, ease_type_ flag)
@缓动_Quad 二次方曲线
Definition: Ease.cpp:205
void WINAPI XExitXCGUI()
炫彩_退出 退出界面库释放资源. 不可在dllmain()中卸载, 否则会死锁
Definition: App.cpp:572
void WINAPI XRunXCGUI()
炫彩_运行 运行消息循环,当炫彩窗口数量为0时退出.
Definition: App.cpp:564
BOOL WINAPI XInitXCGUI(BOOL bD2D)
炫彩_初始化 初始化界面库.
Definition: App.cpp:557
XEle_RegEventCPP1(hEle, nEvent, memberFunction)
元素_注册事件CPP1 注册元素事件,将类成员函数作为事件响应函数.函数不省略参数.
XWnd_RegEventCPP(hWindow, nEvent, memberFunction)
窗口_注册事件CPP 注册窗口事件,将类成员函数作为事件响应函数.函数省略参数窗口自身句柄hWindow.
XEle_RegEventCPP(hEle, nEvent, memberFunction)
元素_注册事件CPP 注册元素事件,将类成员函数作为事件响应函数.函数省略元素自身句柄hEle.
BOOL WINAPI XWnd_GetClientRect(HWINDOW hWindow, __out RECT *pRect)
窗口_取客户区坐标 获取窗口客户区坐标;如果是阴影窗口,那么不包含阴影部分.
Definition: WindowUI.cpp:6830
HWND WINAPI XWnd_GetHWND(HWINDOW hWindow)
窗口_取HWND 获取HWND句柄.
Definition: WindowUI.cpp:6168
HWINDOW WINAPI XWnd_Create(int x, int y, int cx, int cy, const wchar_t *pTitle, HWND hWndParent, int XCStyle)
窗口_创建 创建窗口
Definition: WindowUI.cpp:5964
void WINAPI XWnd_EnableDragWindow(HWINDOW hWindow, BOOL bEnable)
窗口_启用拖动窗口 启用拖动窗口.
Definition: WindowUI.cpp:6186
void WINAPI XWnd_RedrawRect(HWINDOW hWindow, RECT *pRect, BOOL bUpdate)
窗口_重绘指定区域 重绘窗口指定区域.
Definition: WindowUI.cpp:6085
void WINAPI XWnd_AdjustLayout(HWINDOW hWindow)
窗口_调整布局 调整窗口布局, 当窗口显示状态才有效.
Definition: WindowUI.cpp:6718
BOOL WINAPI XWnd_ShowWindow(HWINDOW hWindow, int nCmdShow)
@窗口_显示扩展 显示隐藏及控制窗口最大化还原等
Definition: WindowUI.cpp:6387
void WINAPI XWnd_EnableDragBorder(HWINDOW hWindow, BOOL bEnable)
窗口_启用拖动边框 启用拖动窗口边框.
Definition: WindowUI.cpp:6177
void WINAPI XWnd_Redraw(HWINDOW hWindow, BOOL bUpdate)
窗口_重绘 重绘窗口.
Definition: WindowUI.cpp:6075

截图: