Use DUILIB load the XML interface
The main purpose of this is to teach you how to load XML interface in their own projects, which is the most basic application, the interface control responds to what, I do not talk about, you know, after this, I will give you a other people write blog, look at his article, we should understand more clearly
First, the establishment project
Establish win32 applications, and named blog_try
Here are some points to note:
1. Select the .net framework 2.0 If you do not select this when developing, and you use the default 3.5, then when you publish APP, you will only cry, because, more than 3.5 only in the WIN 7 the operating system is running, it must pay attention. I have the blood of the lesson! ! !
2, the other fact, nothing of note, and select items under win32 win32
Here selection: windows application
Second, take the environment
1, part of the generated code to delete
To blog_try, the automatically generated code to puncturing in this way, i.e., in addition to comprising functions other than the header files and winmain, the other not
2, copy the relevant files to the project directory duilib
2.1, the header file duilib source project copied to our project directory of blog_try:
2.2, the lib file in the same directory project blog_try
2.3, the dll file in debug directory
Third, the function overrides file loading interface
1, adding the following code in stdafx.h to add header files and libraries containing the duilib
- // TODO: here refer to other procedures required header files
- #include "UIlib.h"
- using namespace DuiLib;
- #ifdef _DEBUG
- # ifdef _UNICODE
- # pragma comment(lib, "DuiLib_ud.lib")
- # else
- # pragma comment(lib, "DuiLib_d.lib")
- # endif
- #else
- # ifdef _UNICODE
- # pragma comment(lib, "DuiLib_u.lib")
- # else
- # pragma comment(lib, "DuiLib.lib")
- # endif
- #endif
2, adding blog_try.h CMainFrame class, this class inherits from CwindowWnd, INotifyUI, that is, we use this class CMainFrame to implement binding XML, add buttons, etc. in response to control.
- class CMainFrame :
- public DuiLib :: CWindowWnd, public INotifyUI // application window class CWindowWnd
- {
- public:
- CMainFrame(void);
- ~CMainFrame(void);
- };
Add implemented in blog_try.cpp
- CMainFrame::CMainFrame(void)
- {
- }
- CMainFrame::~CMainFrame(void)
- {
- }
Now blog_try.cpp code should be:
3, override the virtual function
Class new implementation, there are three virtual functions that must be rewritten, which are:
- LPCTSTR GetWindowClassName () const {} // This is a pure virtual function, we see UIBase.cpp in reference to it you can understand why have to implement it, because
- // when registering the window class name used in class (classname), is to use GetWindowClassName () to get the;
- // Analogously, we can () const {} to specify // category, rewrite this function by rewriting UINT GetClassStyle window class is registered in 360Demo in, you can go and see
- void Notify (TNotifyUI & msg) {} // the new class in this class can be implemented to achieve message interceptor, so that when the specified message, a response function we
- // This function is mainly used in response to the control handle to the window message is sent
- LRESULT HandleMessage (UINT uMsg, WPARAM wParam, LPARAM lParam) {} // message processing function, this process is mainly used to generate a message dialog window
The first is the rewrite of the GetWindowClassName (), that returns an arbitrary string as the class name registered class on it
code show as below:
- LPCTSTR CMainFrame :: GetWindowClassName () const // function to rewrite get the class name, which is the class name to specify the new window class
- {
- return _T("UIMainFrame");
- };
Then rewrite the Notify
code show as below:
- void CMainFrame::OnPrepare()
- {
- }
- void CMainFrame :: Notify (TNotifyUI & msg) // notification message processing window, in response to user input
- {
- if( msg.sType == _T("windowinit") ) OnPrepare();
- }
Finally, rewriting HandleMessage () is:
3.1 define a variable
- CPaintManagerUI m_pm;
This variable will be used to plot that we draw the various controls and the background image from its type name CPaintManagerUI can see that
3.2 The HandleMessage () {}
- LRESULT CMainFrame::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_pm.Init (m_hWnd); // main window class associated with the window handle
- CDialogBuilder builder;
- CControlUI * pRoot = builder.Create (_T ( "UISkin.xml"), (UINT) 0, NULL, & m_pm); // load the XML interface and dynamically create the elements, and the layout of interface elements, analysis of the core functions alone
- // Note: CDialogBuilder not a dialog class
- ASSERT(pRoot && "Failed to parse XML");
- if (NULL == pRoot) // If you can not exit the skin file
- {
- MessageBox(NULL,TEXT("Cant not find the skin!"),NULL,MB_ICONHAND);
- return 0;
- }
- m_pm.AttachDialog (pRoot); // attach control data to the HASH table of PROOT ...... As the junction box, to create a control tree
- m_pm.AddNotifier (this); // increase notification processing
- return 0;
- }
- LRESULT CMainFrame::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;
- default:
- bHandled = FALSE;
- }
- if( bHandled ) return lRes;
- if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;
- return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
- }
4, to achieve the main function WinMain ()
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- CPaintManagerUI :: (hInstance) SetInstance; // setup instance
- CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath()
- + _T ( "skin")); // instance handle associated with rendering class, get skin file directory (skin file is loaded in the OnCreate)
- HRESULT Hr = :: CoInitialize (NULL); // initialize COM library that provides support to load COM library
- if( FAILED(Hr) )
- return 0;
- CMainFrame * pMainFrame = new CMainFrame (); // create the application window class object
- if( pMainFrame == NULL )
- return 0;
- pMainFrame->Create(NULL, _T("AdderCalc"), UI_WNDSTYLE_DIALOG, 0);
- pMainFrame-> CenterWindow (); // The central window on the desktop
- pMainFrame-> ShowWindow (true); // display window
- CPaintManagerUI :: MessageLoop (); // enter the message loop
- :: CoUninitialize (); // exit the program and release the COM library
- return 0;
- }
As usual, the code Address: http://download.csdn.net/detail/harvic880925/5367129
Attached Reference: "DUIlib program summary .pdf" and the source code Download: http://download.csdn.net/detail/harvic880925/5367161
And error handling:
一, IDropTarget GetTxDropTarget * ();
Made an error: error C2143: syntax error: missing ';' (in front of the "*")
Solution: Add the header file: #include "OLEIDL.h"