duilib duilib 4. Concise Guide to Getting Started tutorial respond to button events

Hello World is a tutorial , there are a code like this: CControlUI * pWnd = new CButtonUI; that is, in fact, that entire area green background area are buttons. (Down briefly here, CControlUI is duilib the base class for all controls, buttons and CButtonUI is a class, more controls will be introduced one by one in a later tutorial.)
    So how do you respond to click on the message button do?
    We need some step:
    1, the function call message AddNotifier added duilib message loop
    2, to a unique set of control buttons ID (SetName function)
    3, in the Notify message processing function button clicks inside.
    code show as below:

  1. class CDuiFrameWnd : public CWindowWnd, public INotifyUI
  2. {
  3. public:
  4.     virtual LPCTSTR GetWindowClassName() const { return _T("DUIMainFrame"); }
  5.     virtual void    Notify(TNotifyUI& msg) 
  6.     {
  7.         if(msg.sType == _T("click"))
  8.         {
  9.             if(msg.pSender->GetName() == _T("btnHello")) 
  10.             {
  11.                 :: MessageBox (NULL, _T ( "I'm Button"), _T ( "click on the button"), NULL);
  12.             }
  13.         }
  14.     }
  15.     virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  16.     {
  17.         LRESULT lRes = 0;
  18.         if( uMsg == WM_CREATE ) 
  19.         {
  20.             CControlUI *pWnd = new CButtonUI;
  21.             pWnd-> SetName (_T ( "btnHello")); // Set the name of the control, the name used to identify each control, must be unique, which corresponds to the MFC control ID
  22.             pWnd-> SetText (_T ( "Hello World")); // set the text
  23.             pWnd-> SetBkColor (0xFF00FF00); // set the background color
  24.             m_PaintManager.Init(m_hWnd);
  25.             m_PaintManager.AttachDialog(pWnd);
  26.             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
  27.             return lRes;
  28.         }
  29.         if( m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes) ) 
  30.         {
  31.             return lRes;
  32.         }
  33.         return __super::HandleMessage(uMsg, wParam, lParam);
  34.     }
  35. protected:
  36.     CPaintManagerUI m_PaintManager;
  37. };
Copy the code


Run results shown in Figure:

Guess you like

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