duilib duilib Concise Guide to Getting Started tutorial 6.XML configuration interface

In front of those tutorials are to make the transition from small partners win32, MFC to duilib, so that we feel duilib not so strange, if we are now also duilib very strange, it shows the previous tutorial does not do well, please in The following message, I will be 11 to view and make improvements. From the moment the beginning of the tutorial is to witness the miracle of it ~ (^ o ^) / ~ 

    in fact duilib interface production methods is the flagship XML + UI engine + win32 framework, in fact, and browser HTML + CSS + rendering engine in much the same way, you can it is understood as a very mini browser.
    And after the write interface with duilib, most of which are written in XML, similar to writing HTML, this may make the habit MFC windows interface and other partners a little uncomfortable, need to be overcome to overcome, I believe that after watching the tutorial Alberl, it You will get used to it -

    in front of the screen production methods used by the tutorial we should not strange, then use XML to achieve if, like?  
    Is actually very simple,
    1, the if (uMsg == WM_CREATE) inside the code into the following:

  1. if( uMsg == WM_CREATE ) 
  2.         {
  3.             m_PaintManager.Init(m_hWnd);
  4.             CDialogBuilder builder;
  5.             // duilib.xml into the required directory exe; CControlUI * pRoot = builder.Create (_T ( "duilib.xml"), (UINT) 0, NULL, & m_PaintManager)
  6.             ASSERT(pRoot && "Failed to parse XML");
  7.             m_PaintManager.AttachDialog(pRoot);
  8.             m_PaintManager.AddNotifier (this); // add message response controls, etc., which will convey the message to the message loop duilib we can do in the Notify message processing function in
  9.             return lRes;
  10.         }
Copy the code


   2, is provided XML Path: CPaintManagerUI :: SetInstance (hInstance); SetResourcePath call the following function:

  1. CPaintManagerUI::SetInstance(hInstance);
  2.     CPaintManagerUI :: SetResourcePath (CPaintManagerUI :: GetInstancePath ()); // set the default path of the resource (here set and exe in the same directory)
Copy the code


3, the establishment of XML: Create a new XML, a file named "duilib.xml", save it as UTF-8 format ( do not use windows notepad editor, can use UltraEdit, EditPlus editor, etc. ), as follows:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Window size = "800,600"> <-! Initial window size ->
  3.     <HorizontalLayout bkcolor = "# FF00FF00"> <-! The whole background of the window ->
  4.     </HorizontalLayout>
  5. </Window>
Copy the code




4, the "duilib.xml" put under the exe directory.   Now you can see a green window, it is not very simple.
  So we continue to add Hello World button bar ~ O (∩_∩) O ~
  above only describes the XML window size and background color, but does not add buttons, let's add a button Hello, just add the XML inside line to:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Window size = "800,600"> <-! Initial window size ->
  3.     <HorizontalLayout bkcolor = "# FF00FF00"> <-! The whole background of the window ->
  4.         <Button name = "btnHello" text = "Hello World" /> <-! Properties of the button, such as name, text ->
  5.     </HorizontalLayout>
  6. </Window>
Copy the code





 Run it again exe, is not it saw a familiar picture, how to write XML very simple ~ O (∩_∩) O ~:
 

  But we can see that after into XML, after the launch window is not centered, and how to do it?
In duiFrame.ShowModal line above plus duiFrame.CenterWindow (); it can
main.cpp complete code is as follows:



  1. #pragma once
  2. #include 
  3. using namespace DuiLib;
  4. #ifdef _DEBUG
  5. #   ifdef _UNICODE
  6. #       pragma comment(lib, "DuiLib_ud.lib")
  7. #   else
  8. #       pragma comment(lib, "DuiLib_d.lib")
  9. #   endif
  10. #else
  11. #   ifdef _UNICODE
  12. #       pragma comment(lib, "DuiLib_u.lib")
  13. #   else
  14. #       pragma comment(lib, "DuiLib.lib")
  15. #   endif
  16. #endif
  17. class CDuiFrameWnd : public CWindowWnd, public INotifyUI
  18. {
  19. public:
  20.     virtual LPCTSTR GetWindowClassName() const { return _T("DUIMainFrame"); }
  21.     virtual void    Notify(TNotifyUI& msg) 
  22.     {
  23.         if(msg.sType == _T("click"))
  24.         {
  25.             if(msg.pSender->GetName() == _T("btnHello")) 
  26.             {
  27.                 :: MessageBox (NULL, _T ( "I'm Button"), _T ( "click on the button"), NULL);
  28.             }
  29.         }
  30.     }
  31.     virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  32.     {
  33.         LRESULT lRes = 0;
  34.         if( uMsg == WM_CREATE ) 
  35.         {
  36.             m_PaintManager.Init(m_hWnd);
  37.             CDialogBuilder builder;
  38.             // duilib.xml into the required directory exe; CControlUI * pRoot = builder.Create (_T ( "duilib.xml"), (UINT) 0, NULL, & m_PaintManager)
  39.             ASSERT(pRoot && "Failed to parse XML");
  40.             m_PaintManager.AttachDialog(pRoot);
  41.             m_PaintManager.AddNotifier (this); // add message response controls, etc., which will convey the message to the message loop duilib we can do in the Notify message processing function in
  42.             return lRes;
  43.         }
  44.         // The following three messages WM_NCACTIVATE, WM_NCCALCSIZE, WM_NCPAINT for shielding system title bar
  45.         else if( uMsg == WM_NCACTIVATE ) 
  46.         {
  47.             if( !::IsIconic(m_hWnd) ) 
  48.             {
  49.                 return (wParam == 0) ? TRUE : FALSE;
  50.             }
  51.         }
  52.         else if( uMsg == WM_NCCALCSIZE ) 
  53.         {
  54.             return 0;
  55.         }
  56.         else if( uMsg == WM_NCPAINT ) 
  57.         {
  58.             return 0;
  59.         }
  60.         if( m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes) ) 
  61.         {
  62.             return lRes;
  63.         }
  64.         return __super::HandleMessage(uMsg, wParam, lParam);
  65.     }
  66. protected:
  67.     CPaintManagerUI m_PaintManager;
  68. };
  69. int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  70. {
  71.     CPaintManagerUI::SetInstance(hInstance);
  72.     CPaintManagerUI :: SetResourcePath (CPaintManagerUI :: GetInstancePath ()); // set the default path of the resource (here set and exe in the same directory)
  73.     CDuiFrameWnd duiFrame;
  74.     duiFrame.Create(NULL, _T("DUIWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
  75.     duiFrame.CenterWindow();
  76.     duiFrame.ShowModal();
  77.     return 0;
  78. }
Copy the code


Note: The above has been given a complete code, due to the front of the line 17 is #include and other information, the basic will not change, so the rest of the tutorial is no longer listed in the code.

Guess you like

Origin www.cnblogs.com/blogpro/p/11427052.html