用kanzi做一个3d屏保
1. blender制作3d数字模型
下载一些好看的字体文件,用blender建模字体模型,导出fbx格式
2. 新建kanzi工程
导入fbx模型,创建节点,时分秒节点,最上面放一个按钮,用来点击
根据喜好给个火焰贴图,在shader里让uv随时间移动
screen背景透明
3. 代码
3.1 识别桌面分辨率
Application* createApplication()
{
//获取win 桌面分辨率
// 获取桌面窗口的句柄
HWND hDesktopWnd = GetDesktopWindow();
// 获取桌面窗口的设备上下文句柄
HDC hDesktopDC = GetDC(hDesktopWnd);
screenWidth = GetSystemMetrics(SM_CXSCREEN);
screenHeight = GetSystemMetrics(SM_CYSCREEN);
printf("screenWidth:%d, screenHeight:%d\n", screenWidth, screenHeight);
// 释放设备上下文句柄
ReleaseDC(hDesktopWnd, hDesktopDC);
return new DesktopTime;
}
3.2 设置窗口透明
virtual void onProjectLoaded() KZ_OVERRIDE
{
// Project file has been loaded from .kzb file.
// Add initialization code here.
// 使用FindWindow函数查找窗口句柄
hwnd = FindWindow(L"KanziWindow", NULL);
//Transparent background
LONG ret = ::GetWindowLong(hwnd, GWL_EXSTYLE);
ret = ret | WS_EX_LAYERED;
::SetWindowLong(hwnd, GWL_EXSTYLE, ret);
::SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 255, LWA_COLORKEY | LWA_ALPHA);
3.3 绑定按键
kanzi::TimerSubscriptionToken timerid = addTimerHandler(this->getMessageDispatcher(), kanzi::chrono::milliseconds(20), KZU_TIMER_MESSAGE_MODE_REPEAT, bind(&DesktopTime::onTimeOutEventHandler, this));
Button2DSharedPtr btn = getScreen()->lookupNode<Button2D>("#Button 2D");
if (btn) {
btn->addMessageHandler(Button2D::PressedMessage, bind(&DesktopTime::onBtnClicked, this, std::placeholders::_1));
}
点击屏幕控制开关
void onBtnClicked(ButtonConcept::PressedMessageArguments& messageArguments)
{
(void)messageArguments;
//kzLogDebug(("{} {} ", __FUNCTION__, isOpen));
isOpen = !isOpen;
3.4 定时器更新时间和动画坐标
void onTimeOutEventHandler()
{
m_anim->Update(20);
PageHostSharedPtr p = getScreen()->lookupNode<PageHost>("#RootPage");
if (p) {
//获取系统时间
SYSTEMTIME st;
GetLocalTime(&st);
//设置时间
p->setProperty(DynamicPropertyType<int>("DesktopTime.hour1"), st.wHour >= 10 ? (int)(st.wHour / 10) : 0);
p->setProperty(DynamicPropertyType<int>("DesktopTime.hour2"), st.wHour >= 10 ? st.wHour % 10 : st.wHour);
p->setProperty(DynamicPropertyType<int>("DesktopTime.minute1"), st.wMinute >= 10 ? st.wMinute / 10 : 0);
p->setProperty(DynamicPropertyType<int>("DesktopTime.minute2"), st.wMinute >= 10 ? st.wMinute % 10 : st.wMinute);
p->setProperty(DynamicPropertyType<int>("DesktopTime.sec1"), st.wSecond >= 10 ? st.wSecond / 10 : 0);
p->setProperty(DynamicPropertyType<int>("DesktopTime.sec2"), st.wSecond >= 10 ? st.wSecond % 10 : st.wSecond);
p->setProperty(DynamicPropertyType<int>("DesktopTime.ScreenPosY"), m_y - screenHeight);
}
4. 效果
运行程序,桌面显示全屏3d数字屏保
点击画面,屏保向上退出,在屏幕顶显示黑条
点击黑条,屏保向下出现