当前位置:   article > 正文

duilib的通用窗口类WindowImplBase

windowimplbase

前言

duilib程序中,编写自己的窗口类一般的继承关系有两种,一种是继承自CWindowWnd、INotifyUI、IMessageFilterUI,而第二种方式是继承自duilib封装好的通用窗口类WindowImplBase

WindowImplBase类

从源码中可以看到WindowImplBase其实是继承自一般能用到的所有基础类:

  1. class DUILIB_API WindowImplBase
  2. : public CWindowWnd
  3. , public CNotifyPump
  4. , public INotifyUI
  5. , public IMessageFilterUI
  6. , public IDialogBuilderCallback

优点

从上可以看到,一般编写自己的duilib窗口类所需要继承的几个类都已经被WindowImplBase继承了,而且已经做了部分封装,这样我们直接继承WindowImplBase不是方便了很多,而且只需要实现以下几个函数即可实现简单的duilib窗口,很简洁方便

  1. virtual CDuiString GetSkinFolder() = 0;
  2. virtual CDuiString GetSkinFile() = 0;
  3. virtual LPCTSTR GetWindowClassName(void) const = 0 ;

缺点

WindowImplBase通用窗口类目前不完全统计bug较多,好多地方需要自己修改,这个就要根据自己的需求不同自己去调整该类的源码,达到自己想要的效果。

示例

下面实现一个简单的例子:

MainFrame.h

  1. #include "resource.h"
  2. class CMainFrame : public WindowImplBase
  3. {
  4. public:
  5. CMainFrame();
  6. ~CMainFrame();
  7. public:
  8. LPCTSTR GetWindowClassName() const;
  9. virtual CDuiString GetSkinFile();
  10. virtual CDuiString GetSkinFolder();
  11. virtual LRESULT HandleCustomMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  12. virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  13. public:
  14. protected:
  15. void Notify(TNotifyUI& msg);
  16. bool OnBtnClickOfEnter(TNotifyUI& msg);
  17. };

MainFrame.cpp

  1. #include "stdafx.h"
  2. #include "MainFrame.h"
  3. #include "DefControlName.h"
  4. #include "PersonalCenterDialog.h"
  5. HWND g_hMainFrame = NULL;
  6. CMainFrame::CMainFrame()
  7. {
  8. }
  9. CMainFrame::~CMainFrame()
  10. {
  11. PostQuitMessage(0);
  12. }
  13. LPCTSTR CMainFrame::GetWindowClassName() const
  14. {
  15. return _T("App");
  16. }
  17. CDuiString CMainFrame::GetSkinFile()
  18. {
  19. return _T("MainFrame.xml");
  20. }
  21. CDuiString CMainFrame::GetSkinFolder()
  22. {
  23. return _T("skin\\");
  24. }
  25. //duilib自身消息
  26. void CMainFrame::Notify(TNotifyUI& msg)
  27. {
  28. if (_tcsicmp(msg.sType, _T("windowinit")) == 0)
  29. {
  30. }
  31. else if (_tcsicmp(msg.sType, _T("killfocus")) == 0)
  32. {
  33. }
  34. else if (_tcsicmp(msg.sType, _T("click")) == 0)
  35. {
  36. if (_tcsicmp(msg.pSender->GetName(), kMainFrameBtnClose) == 0)
  37. {
  38. Close();
  39. }
  40. else if (_tcsicmp(msg.pSender->GetName(), kMainFrameBtnEnter) == 0)
  41. {
  42. OnBtnClickOfEnter(msg);
  43. }
  44. }
  45. else if (_tcsicmp(msg.sType, _T("timer")) == 0)
  46. { }
  47. else if (_tcsicmp(msg.sType, _T("selectchanged")) == 0)
  48. { }
  49. else if (_tcsicmp(msg.sType, _T("itemactivate")) == 0)
  50. { }
  51. else if (_tcsicmp(msg.sType, _T("itemclick")) == 0)
  52. { }
  53. }
  54. bool CMainFrame::OnBtnClickOfEnter(TNotifyUI & msg)
  55. {
  56. CPersonalCenterDialog* pDialog = new CPersonalCenterDialog();
  57. if (pDialog == NULL)
  58. return false;
  59. DWORD dwStyle = UI_WNDSTYLE_FRAME;
  60. dwStyle = dwStyle^WS_MAXIMIZEBOX;
  61. #if defined(WIN32) && !defined(UNDER_CE)
  62. pDialog->Create(NULL, _T("个人中心"), dwStyle | WS_POPUP, NULL, 0, 0, 0, 0);
  63. #else
  64. pDialog->Create(NULL, _T("个人中心"), dwStyle | WS_POPUP, NULL, 0, 0, 0, 0);
  65. #endif
  66. pDialog->CenterWindow();
  67. ::ShowWindow(g_hMainFrame, SW_HIDE);
  68. ::ShowWindow(*pDialog, SW_SHOW);
  69. return true;
  70. }
  71. //处理自定义消息
  72. LRESULT CMainFrame::HandleCustomMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  73. {
  74. switch (uMsg)
  75. {
  76. /*case WM_APPOBD_MSG_SHOW_MAIN_FRAME:
  77. {
  78. ::ShowWindow(m_hWnd, SW_SHOW);
  79. break;
  80. }*/
  81. default:
  82. break;
  83. }
  84. return 0;
  85. }
  86. //处理事件消息
  87. LRESULT CMainFrame::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  88. {
  89. switch (uMsg)
  90. {
  91. case WM_DEVICECHANGE:
  92. {
  93. break;
  94. }
  95. //case WM_CLOSE:
  96. // PostQuitMessage(0);
  97. // break;
  98. default:
  99. break;
  100. }
  101. return WindowImplBase::HandleMessage(uMsg, wParam, lParam);
  102. }

stdafx.h

  1. // stdafx.h : 标准系统包含文件的包含文件,
  2. // 或是经常使用但不常更改的
  3. // 特定于项目的包含文件
  4. //
  5. #pragma once
  6. #include "targetver.h"
  7. #define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
  8. // Windows 头文件:
  9. #include <windows.h>
  10. #include "..\DuiLib\UIlib.h"
  11. using namespace DuiLib;
  12. #ifdef _DEBUG
  13. # ifdef _UNICODE
  14. # pragma comment(lib, "..\\Lib\\DuiLib_ud.lib")
  15. # else
  16. # pragma comment(lib, "..\\Lib\\DuiLib_d.lib")
  17. # endif
  18. #else
  19. # ifdef _UNICODE
  20. # pragma comment(lib, "..\\Lib\\DuiLib_u.lib")
  21. # else
  22. # pragma comment(lib, "..\\Lib\\DuiLib.lib")
  23. # endif
  24. #endif
  25. extern HWND g_hMainFrame;
  26. // TODO: 在此处引用程序需要的其他头文件

main.cpp

  1. #include "stdafx.h"
  2. #include "MainFrame.h"
  3. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)
  4. {
  5. CPaintManagerUI::SetInstance(hInstance);
  6. CWndShadow::Initialize(hInstance);
  7. CMainFrame* pFrame = new CMainFrame();
  8. if (pFrame == NULL)
  9. return 0;
  10. DWORD dwStyle = UI_WNDSTYLE_FRAME;
  11. dwStyle = dwStyle^WS_MAXIMIZEBOX;
  12. pFrame->Create(NULL, _T("云端软件"), dwStyle, WS_EX_STATICEDGE | WS_EX_APPWINDOW, 1, 1, 0, 0); //倒数第3第4个参数可以设置duilib左上角的坐标,倒数第1第2个参数因为继承了WindowImpBase,
  13. //WindowImpBase类中在创建窗口大小时会取xml文件中的窗口大小数据,顾此处两个参数值无效。
  14. pFrame->CenterWindow();
  15. ::ShowWindow(*pFrame, SW_SHOW);
  16. CPaintManagerUI::MessageLoop(); //消息循环
  17. ::CoUninitialize();
  18. return 0;
  19. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/942878
推荐阅读
  

闽ICP备14008679号