Duilib 入门第一个程序,可拖动的窗口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | // Win32_01.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "Win32_01.h" #include "../DuiLib/UIlib.h" #include <algorithm> #ifdef _DEBUG # ifdef _UNICODE # pragma comment(lib, "../lib/DuiLib_ud.lib") # else # pragma comment(lib, "../lib/DuiLib_d.lib") # endif #else # ifdef _UNICODE # pragma comment(lib, "../lib/DuiLib_u.lib") # else # pragma comment(lib, "../lib/DuiLib.lib") # endif #endif using namespace DuiLib; class CDuiFrameWnd : public CWindowWnd, public INotifyUI { public : CDuiFrameWnd(); ~CDuiFrameWnd(); virtual LPCTSTR GetWindowClassName() const ; virtual CDuiString GetSkinFile() { return _T( "duilib.xml" ); } virtual CDuiString GetSkinFolder() { return _T( "" ); } virtual void Notify(TNotifyUI & msg); virtual LRESULT HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT CDuiFrameWnd::OnCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled); virtual LRESULT OnNcHitTest( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled); LRESULT CDuiFrameWnd::OnSize( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled); LRESULT CDuiFrameWnd::OnNcActivate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled); LRESULT CDuiFrameWnd::OnNcCalcSize( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled); LRESULT CDuiFrameWnd::OnSysCommand( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled); LRESULT CDuiFrameWnd::OnGetMinMaxInfo( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled); private : BOOL IsInStaticControl(CControlUI * pControl); protected : CPaintManagerUI m_PaintManager; CControlUI *m_pHelloBtn; CControlUI *m_pRoot; }; CDuiFrameWnd::CDuiFrameWnd() { m_pRoot = NULL; m_pHelloBtn = NULL; } CDuiFrameWnd::~CDuiFrameWnd() { } //cpp文件 LPCTSTR CDuiFrameWnd::GetWindowClassName() const { return _T( "DuiFrameWnd" ); } void CDuiFrameWnd::Notify(TNotifyUI & msg) { if (msg.sType == _T( "click" )) { if (msg.pSender->GetName() == _T( "closebtn" )) { ::PostQuitMessage(WM_QUIT); } if (msg.pSender->GetName() == _T( "btnHello" )) { MessageBox(NULL, _T( "我是按钮" ), _T( "点击了按钮" ), NULL); } } } LRESULT CDuiFrameWnd::OnCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) { LONG styleValue = ::GetWindowLong(* this , GWL_STYLE); styleValue &= ~WS_CAPTION; ::SetWindowLong(* this , GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN); m_PaintManager.Init(m_hWnd); //主窗口类与窗口句柄关联 CDialogBuilder builder; CControlUI* pRoot = builder.Create(_T( "layout.xml" ), ( UINT )0, NULL, &m_PaintManager); //加载XML并动态创建界面无素,与布局界面元素,核心函数单独分析 //注意:CDialogBuilder 并不是一个对话框类 ASSERT(pRoot && "Failed to parse XML" ); if (NULL == pRoot) //如果找不到皮肤文件则退出 { MessageBox(NULL, TEXT( "Cant not find the skin!" ), NULL, MB_ICONHAND); return 0; } m_PaintManager.AttachDialog(pRoot); //附加控件数据到HASH表中……为pRoot作为对话框结点,为其创建控件树 m_PaintManager.AddNotifier( this ); //增加通知处理 return 0; } LRESULT CDuiFrameWnd::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT lRes = 0; BOOL bHandled = TRUE; switch (uMsg) { case WM_CREATE: lRes = OnCreate(uMsg, wParam, lParam, bHandled); break ; case WM_DESTROY: ::PostQuitMessage(0); bHandled = FALSE; break ; case WM_NCHITTEST: lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break ; case WM_KEYDOWN: if (wParam == VK_ESCAPE) //ESC { Close(); } break ; case WM_SIZE: lRes = OnSize(uMsg, wParam, lParam, bHandled); break ; case WM_NCACTIVATE: lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break ; case WM_GETMINMAXINFO: lRes = true ; OnGetMinMaxInfo(uMsg, wParam, lParam, bHandled); break ; case WM_SYSCOMMAND: lRes = OnSysCommand(uMsg, wParam, lParam, bHandled); break ; case WM_NCCALCSIZE: lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break ; default : bHandled = FALSE; break ; } if (bHandled) return lRes; if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes) != 0) return lRes; return CWindowWnd::HandleMessage(uMsg, wParam, lParam); } LRESULT CDuiFrameWnd::OnGetMinMaxInfo( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) { MONITORINFO oMonitor = {}; oMonitor.cbSize = sizeof (oMonitor); ::GetMonitorInfo(::MonitorFromWindow(* this , MONITOR_DEFAULTTOPRIMARY), &oMonitor); CDuiRect rcWork = oMonitor.rcWork; rcWork.Offset(-rcWork.left, -rcWork.top); LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam; lpMMI->ptMaxPosition.x = rcWork.left; lpMMI->ptMaxPosition.y = rcWork.top; lpMMI->ptMaxSize.x = rcWork.right; lpMMI->ptMaxSize.y = rcWork.bottom; bHandled = FALSE; return 0; } LRESULT CDuiFrameWnd::OnSysCommand( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) { if (wParam == SC_CLOSE) { bHandled = TRUE; SendMessage(WM_CLOSE); return 0; } #if defined(WIN32) && !defined(UNDER_CE) BOOL bZoomed = ::IsZoomed(* this ); LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam); if (::IsZoomed(* this ) != bZoomed) { } #else LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam); #endif return lRes; } LRESULT CDuiFrameWnd::OnNcCalcSize( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) { return 0; // wParam为TRUE时,返回0将会使窗口的大小变为客户区的大小,也就是说这将把窗口的标题栏、窗口边框移除,只显示客户区 } LRESULT CDuiFrameWnd::OnNcActivate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) { if (::IsIconic(* this )) bHandled = FALSE; return (wParam == 0) ? TRUE : FALSE; } LRESULT CDuiFrameWnd::OnSize( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) { SIZE szRoundCorner = m_PaintManager.GetRoundCorner(); // GetRoundCorner用来获取xml中的Window标签中roundcorner属性值,该值指示圆角的长宽 if (!::IsIconic(* this ) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0)) { CDuiRect rcWnd; ::GetWindowRect(* this , &rcWnd); rcWnd.Offset(-rcWnd.left, -rcWnd.top); // rcWnd.right就成为了窗口的宽度了 rcWnd.right++; rcWnd.bottom++; HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy); ::SetWindowRgn(* this , hRgn, TRUE); // 窗口圆角化处理 ::DeleteObject(hRgn); } bHandled = FALSE; return 0; } LRESULT CDuiFrameWnd::OnNcHitTest( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) { POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam); ::ScreenToClient(* this , &pt); RECT rcClient; ::GetClientRect(* this , &rcClient); if (!::IsZoomed(* this )) { RECT rcSizeBox = m_PaintManager.GetSizeBox(); // GetSizeBox用来获取xml中Window标签的sizebox属性,该属性指示你的鼠标移动到窗口边框多少个像素会变成指示符(这个指示符表示可以改变窗口大小的指示符) if (pt.y < rcClient.top + rcSizeBox.top) { if (pt.x < rcClient.left + rcSizeBox.left) return HTTOPLEFT; if (pt.x > rcClient.right - rcSizeBox.right) return HTTOPRIGHT; return HTTOP; } else if (pt.y > rcClient.bottom - rcSizeBox.bottom) { if (pt.x < rcClient.left + rcSizeBox.left) return HTBOTTOMLEFT; if (pt.x > rcClient.right - rcSizeBox.right) return HTBOTTOMRIGHT; return HTBOTTOM; } if (pt.x < rcClient.left + rcSizeBox.left) return HTLEFT; if (pt.x > rcClient.right - rcSizeBox.right) return HTRIGHT; } RECT rcCaption = m_PaintManager.GetCaptionRect(); // GetCaptionRect用来获取xml中Window标签的caption属性,该属性指示标题栏的大小 if (pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right && pt.y >= rcCaption.top && pt.y < rcCaption.bottom) { CControlUI* pControl = static_cast <CControlUI*>(m_PaintManager.FindControl(pt)); if (pControl && _tcsicmp(pControl->GetClass(), _T( "ButtonUI" )) != 0 && _tcsicmp(pControl->GetClass(), _T( "OptionUI" )) != 0) return HTCAPTION; } return HTCLIENT; } int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { CPaintManagerUI::SetInstance(hInstance); CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + _T( "skin" )); HRESULT Hr = ::CoInitialize(NULL); if (FAILED(Hr)) return 0; CDuiFrameWnd * duiFrame = new CDuiFrameWnd(); if (duiFrame == NULL) return 0; //#define UI_WNDSTYLE_FRAME (WS_VISIBLE | WS_OVERLAPPEDWINDOW) duiFrame->Create(NULL, _T( "测试" ), UI_WNDSTYLE_EX_FRAME, WS_EX_WINDOWEDGE); duiFrame->CenterWindow(); //将窗口放到桌面中央 duiFrame->ShowWindow( true ); CPaintManagerUI::MessageLoop(); ::CoUninitialize(); return 0; } |
layout.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <? xml version="1.0" encoding="utf-8"?> < Window size="800,600" sizebox="4,4,4,4" caption="0,0,0,32" mininfo="600,400"> < VerticalLayout bkcolor="#FF008080" bkcolor2="#FFAAAAA0"> < HorizontalLayout height="32" bkcolor="#FFE6E6DC" bkcolor2="#FFAAAAA0"> < VerticalLayout /> < VerticalLayout width="77"> < Button name="minbtn" text="-" tooltip="最小化" float="true" pos="0,5,0,0" width="23" height="19" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage=" file='SysBtn\MinNormal.bmp' " hotimage=" file='SysBtn\MinFocus.bmp' " pushedimage=" file='SysBtn\MinFocus.bmp' " /> < Button name="maxbtn" text="口" tooltip="最大化" float="true" pos="22,5,0,0" width="23" height="19" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage=" file='SysBtn\MaxNormal.bmp' " hotimage=" file='SysBtn\MaxFocus.bmp' " pushedimage=" file='SysBtn\MaxFocus.bmp' " /> < Button name="restorebtn" tooltip="还原" visible="false" float="true" pos="22,5,0,0" width="23" height="19" align="center" normalimage=" file='SysBtn\StoreNormal.bmp' " hotimage=" file='SysBtn\StoreFocus.bmp' " pushedimage=" file='SysBtn\StoreFocus.bmp' " /> < Button name="closebtn" text="x" tooltip="关闭" float="true" pos="44,5,0,0" width="28" height="19" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage=" file='SysBtn\CloseNormal.bmp' " hotimage=" file='SysBtn\CloseFocus.bmp' " pushedimage=" file='SysBtn\CloseFocus.bmp' " /> </ VerticalLayout > </ HorizontalLayout > < Button name="btnHello" text="hello " float="true" pos="446,314,0,0" width="60" height="30" bkcolor="#FFFF0000" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" /> </ VerticalLayout > </ Window > |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?