Wednesday, October 16, 2013

share my writed messagebox with duilib by cpp

1.todo

my partern need me replace all messagebox windows style with same as worker window style. our project is used duilib.

2.code

1.dialog_newpersonal.xml

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Window size="285,161" sizebox="6,6,6,6" caption="0,0,0,36" mininfo="80,60" roundcorner="5,5">
<VerticalLayout bordersize="1" width="330" height="198" bkcolor="#FFFFFFFF" bkcolor2="#FF477FFF" bordercolor="#FF4775CC">
<HorizontalLayout name="header" height="32" padding="1,0,1,2" bkcolor="#FF1E94DC">
<HorizontalLayout>
<Control width="10" />
<Label name="title" text="信息提示" textcolor="#FFFFFFFF" disabledtextcolor="#FFA7A6AA" font="1" />
</HorizontalLayout>
<HorizontalLayout width="49" height="25">
<Button name="minbtn" tooltip="最小化" visible="false" width="33" height="22" align="center" normalimage="file=&apos;btn_mini_normal.png&apos;" hotimage="btn_mini_highlight.png" pushedimage="btn_mini_down.png" />
<Button name="closebtn" tooltip="关闭" width="40" height="22" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file=&apos;btn_close_normal.png&apos;" hotimage="btn_close_highlight.png" pushedimage="btn_close_down.png" />
</HorizontalLayout>
</HorizontalLayout>
<HorizontalLayout name="bg" width="278" height="119" padding="2,2,2,2" bkcolor="#FFDDF2FF">
<Button name="btnOk" text="确定" float="true" pos="41,71,0,0" width="75" height="24" textpadding="4,2,4,3" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" font="1" align="center" normalimage="file=&apos;main\button_p.png&apos;" hotimage="file=&apos;main\button.png&apos; mask=&apos;0xffffffff&apos;" pushedimage="file=&apos;main\button_h.png&apos; mask=&apos;0xffffffff&apos;" />
<Label name="message" float="true" pos="6,5,0,0" width="265" height="53" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" font="1" align="center" />
<Button name="btnCancel" text="取消" float="true" pos="171,70,0,0" width="75" height="24" textpadding="4,2,4,3" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" font="1" align="center" normalimage="file=&apos;main\button_p.png&apos;" hotimage="file=&apos;main\button.png&apos; mask=&apos;0xffffffff&apos;" pushedimage="file=&apos;main\button_h.png&apos; mask=&apos;0xffffffff&apos;" />
</HorizontalLayout>
</VerticalLayout>
</Window>



2.Dialog_Info_Base.h

class CDialog_Info_Base : public CusWindowImplBase
{
public:
/// 构造函数
/// \param hwnd 窗口句柄
/// \param curDstId 当前联系人的用户ID
CDialog_Info_Base(HWND hwnd,const char* curDstId);

protected: // public:
HWND m_hMainWnd; /// 窗口句柄
std::wstring kIdNewOrUpdate; /// 当前加载的对话框类型ID
bool isCtrl; /// 是否按下ctrl键

std::string m_CurDstUId; /// 当前联系人的用户ID

std::wstring mExitPromotMessage; /// 退出窗口的提示消息

// 系统标题栏菜单
const TCHAR* kTitleControlName;
const TCHAR* kCloseButtonControlName;
const TCHAR* kMinButtonControlName;
const TCHAR* kMaxButtonControlName;
const TCHAR* kRestoreButtonControlName;

protected:

/// 取DUI窗口类名
/// \return 窗口类名
virtual LPCTSTR GetWindowClassName() const;

/// 取界面XML文件
/// \return 界面XML文件路径.默认返回 "dialog_info.xml"
virtual CDuiString GetSkinFile();

/// 初始化
virtual void Init();

/// 响应退出事件
virtual void OnExit(TNotifyUI& msg);

/// 响应通知事件
virtual void Notify(TNotifyUI& msg);

/// 响应界面消息
virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
};





3.Dialog_Info_Base.cpp

#include "stdafx.h"
#include "Dialog_Info_Base.h"
#include "UserUI.h"

CDialog_Info_Base::CDialog_Info_Base(HWND hwnd,const char* curDstId)
{
if(!curDstId)
curDstId = "";
swl_setkey((std::string("CDialog_Info_Base_")+curDstId).c_str());

kTitleControlName = _T("apptitle");
kCloseButtonControlName = _T("closebtn");
kMinButtonControlName = _T("minbtn");
kMaxButtonControlName = _T("maxbtn");
kRestoreButtonControlName = _T("restorebtn");

mExitPromotMessage = _T("确定关闭?");

isCtrl = false;
m_hMainWnd = hwnd;
kIdNewOrUpdate = _T("");

if(curDstId)
m_CurDstUId = curDstId;
else
ASSERT(0 && "当前联系人ID非法!");
}

/// 取DUI窗口类名
LPCTSTR CDialog_Info_Base::GetWindowClassName() const
{
return _T("TXGuiFoundation");
}

/// 取界面XML文件
CDuiString CDialog_Info_Base::GetSkinFile()
{
return _T("dialog_info.xml");//个人通讯录查看/编辑联系人对话框
}

/// 响应界面消息
LRESULT CDialog_Info_Base::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(uMsg==WM_KEYDOWN){
if(wParam==VK_CONTROL){
isCtrl = true;
}
}else if(uMsg==WM_KEYUP){

if(wParam==VK_CONTROL){
isCtrl = false;
}
}
return __super::HandleMessage(uMsg, wParam, lParam);
}

/// 响应退出事件
void CDialog_Info_Base::OnExit(TNotifyUI& msg)
{
Close();
}

/// 初始化
void CDialog_Info_Base::Init(){
__super::Init();
}


void CDialog_Info_Base::Notify(TNotifyUI& msg)
{
if (_tcsicmp(msg.sType, _T("click")) == 0)
{
if (_tcsicmp(msg.pSender->GetName(), kCloseButtonControlName) == 0)
{
if(!mExitPromotMessage.empty()){
int iResult = UserUI::MessageBox(this->m_hWnd, mExitPromotMessage.c_str(), _T("信息提示"), MB_OKCANCEL);
if(iResult ==1)
{
OnExit(msg);
}
}else{
OnExit(msg);
}
}else if (_tcsicmp(msg.pSender->GetName(), kMinButtonControlName) == 0)
{
::ShowWindow(m_hWnd, SW_MINIMIZE);
}
else if (_tcsicmp(msg.pSender->GetName(), kMaxButtonControlName) == 0)
{
SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
else if (_tcsicmp(msg.pSender->GetName(), kRestoreButtonControlName) == 0)
{
SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
}
}
}



4.class CMyMessageBox code:

class CMyMessageBox : public CDialog_Info_Base{
private:
UINT mType;
std::wstring mTitle,mMessage;
public:
CMyMessageBox(HWND hwnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) : CDialog_Info_Base(hwnd,0){
mExitPromotMessage.clear();

mType = uType;
mTitle = lpText;
mMessage = lpCaption;
}
virtual CDuiString GetSkinFile(){
return _T("dialog_messagebox.xml");
}

virtual void Init(){
CDialog_Info_Base::Init();

CControlUI* pControl;

if(!mTitle.empty()){
pControl = paint_manager_.FindControl(_T("title"));
if(pControl)
pControl->SetText(mTitle.c_str());
}

if(!mMessage.empty()){
pControl = paint_manager_.FindControl(_T("message"));
if(pControl)
pControl->SetText(mMessage.c_str());
}

if(mType == MB_OK){
pControl = paint_manager_.FindControl(_T("btnCancel"));
if(pControl)
pControl->SetVisible(false);

pControl = paint_manager_.FindControl(_T("btnOk"));
if(pControl){
SIZE pos = pControl->GetFixedXY();
pos.cx += 60;
pControl->SetFixedXY(pos);
}
}
}

LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(uMsg==WM_KEYUP){
if(wParam==VK_RETURN){
PostMessage(WM_CLOSE,IDOK,0);
}
}
return __super::HandleMessage(uMsg, wParam, lParam);
}

void Notify(TNotifyUI& msg)
{
if (_tcsicmp(msg.sType, _T("click")) == 0)
{
if (_tcsicmp(msg.pSender->GetName(), _T("btnOk")) == 0)
{
PostMessage(WM_CLOSE,IDOK,0);
}else if (_tcsicmp(msg.pSender->GetName(), _T("btnCancel")) == 0)
{
PostMessage(WM_CLOSE,IDCANCEL,0);
}
}
__super::Notify(msg);
}
};



5.MessageBox function


need namespace to fixed function name confit

/// 弹出消息对话框
int MessageBox( HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType){
int ret = -1;
if(!hWnd && systeminfo){
hWnd = systeminfo->MainHwnd;
}
CMyMessageBox* wnd = new CMyMessageBox(hWnd,lpText,lpCaption,uType);
do{
if(!wnd)
break;
if(!wnd->Create(hWnd, _T("Dialog"), UI_WNDSTYLE_DIALOG, UI_WNDSTYLE_EX_DIALOG))
break;
wnd->CenterWindow();
ret = (int)wnd->ShowModal();
}while(false);
if(wnd)
delete wnd;
return ret;
}

3.result


screenshots:


image




image


test:

if(IDOK == UserUI::MessageBox(this->m_hWnd, _T("确认关闭!"), _T("提示"),MB_OKCANCEL))
// code...

No comments: