duilib入门笔记(二)——按钮事件

Home / Hackintosh MrLee 2015-6-10 5353

上一节中我们在界面中添加了一个很大的按钮,光按钮还没有事件就不能与用户互动,那么事件应该怎么实现呢? 我们需要几个步骤: 1、调用AddNotifier函数将消息加入duilib的消息循环 2、给按钮设置一个唯一的控件ID(SetName函数) 3、在Notify函数里处理按钮点击消息。 具体代码如下:
// MyApp.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "MyApp.h"
#define S(s) _T(s)			//定义一个宏

class CDuiFrameWnd : public CWindowWnd, public INotifyUI
{
public:
	virtual LPCTSTR GetWindowClassName() const { return S("DUIMainFrame"); }
	virtual void    Notify(TNotifyUI& msg) 
	{
		//处理通知事件
		if (msg.sType == S("click"))
		{
			if (msg.pSender->GetName() == S("test"))
			{
				MessageBox(NULL,S("Test"),NULL,MB_OK);
			}
		}
	}
	virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		//处理对应的消息
		LRESULT lRes = 0;
		if( uMsg == WM_CREATE ) 
		{
			CControlUI *pWnd = new CButtonUI;
			pWnd->SetText(S("Hello World"));   // 设置文字
			pWnd->SetBkColor(0xFF00FF00);       // 设置背景色
			pWnd->SetName(S("test"));
			m_PaintManager.Init(m_hWnd);
			m_PaintManager.AddNotifier(this);//添加通知事件
			m_PaintManager.AttachDialog(pWnd);
			return lRes;
		}
		if( m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes) ) 
		{
			return lRes;
		}
		return __super::HandleMessage(uMsg, wParam, lParam);
	}
protected:
	CPaintManagerUI m_PaintManager;
};
int APIENTRY _tWinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPTSTR    lpCmdLine,
	int       nCmdShow)
{
	CPaintManagerUI::SetInstance(hInstance);
	CDuiFrameWnd duiFrame;
	duiFrame.Create(NULL, S("DuiLib程序"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
	duiFrame.ShowModal();
	return 0;
}
运行效果图:

QQ截图20150610155329


怎么样?是不是很简单呢!已经有了基本了按钮控制,不过这个界面似乎还是有点小丑,特别是标题这块?如果用MFC开发的话,肯定是禁用标题栏,然后自己绘制标题栏的按钮来美化自己的APP,不过duilib绘制标题是小意思。
在HandleMessage函数里屏蔽以下三个消息即可 WM_NCACTIVATE、WM_NCCALCSIZE、WM_NCPAINT
// 以下3个消息WM_NCACTIVATE、WM_NCCALCSIZE、WM_NCPAINT用于屏蔽系统标题栏
		else if( uMsg == WM_NCACTIVATE ) 
		{
			if( !::IsIconic(m_hWnd) ) 
			{
				return (wParam == 0) ? TRUE : FALSE;
			}
		}
		else if( uMsg == WM_NCCALCSIZE ) 
		{
			return 0;
		}
		else if( uMsg == WM_NCPAINT ) 
		{
			return 0;
		}
运行图:

QQ截图20150610155843


现在是不是对duilib库有了初步的了解?要想界面做的炫还得继续往下学习
// MyApp.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "MyApp.h"
#define S(s) _T(s)			//定义一个宏

class CDuiFrameWnd : public CWindowWnd, public INotifyUI
{
public:
	virtual LPCTSTR GetWindowClassName() const { return S("DUIMainFrame"); }
	virtual void    Notify(TNotifyUI& msg) 
	{
		//处理通知事件
		if (msg.sType == S("click"))
		{
			if (msg.pSender->GetName() == S("test"))
			{
				MessageBox(NULL,S("Test"),NULL,MB_OK);
			}
		}
	}
	virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		//处理对应的消息
		LRESULT lRes = 0;
		if( uMsg == WM_CREATE ) 
		{
			CControlUI *pWnd = new CButtonUI;
			pWnd->SetText(S("Hello World"));   // 设置文字
			pWnd->SetBkColor(0xFF00FF00);       // 设置背景色
			pWnd->SetName(S("test"));
			m_PaintManager.Init(m_hWnd);
			m_PaintManager.AddNotifier(this);//添加通知事件
			m_PaintManager.AttachDialog(pWnd);
			return lRes;
		}
		// 以下3个消息WM_NCACTIVATE、WM_NCCALCSIZE、WM_NCPAINT用于屏蔽系统标题栏
		else if( uMsg == WM_NCACTIVATE ) 
		{
			if( !::IsIconic(m_hWnd) ) 
			{
				return (wParam == 0) ? TRUE : FALSE;
			}
		}
		else if( uMsg == WM_NCCALCSIZE ) 
		{
			return 0;
		}
		else if( uMsg == WM_NCPAINT ) 
		{
			return 0;
		}
		if( m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes) ) 
		{
			return lRes;
		}
		return __super::HandleMessage(uMsg, wParam, lParam);
	}
protected:
	CPaintManagerUI m_PaintManager;
};
int APIENTRY _tWinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPTSTR    lpCmdLine,
	int       nCmdShow)
{
	CPaintManagerUI::SetInstance(hInstance);
	CDuiFrameWnd duiFrame;
	duiFrame.Create(NULL, S("DuiLib程序"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE,300,300,800,300);
	duiFrame.ShowModal();
	return 0;
}

本文链接:https://www.it72.com/3347.htm

推荐阅读
最新回复 (0)
返回