直接上来贴码
头文件
#ifndef __UIIpAddress_H__ #define __UIIpAddress_H__ #pragma once namespace DuiLib { class CIpAddressUI : public CHorizontalLayoutUI { public: CIpAddressUI(); virtual void Init(); virtual void DoInit(); virtual void SetPos(RECT rc); virtual CDuiString GetText() const; virtual void DoEvent(TEventUI& event); virtual void SetText(LPCTSTR pstrText); protected: CEditUI *p; CEditUI *p2; CEditUI *p3; CEditUI *p4; }; } #endif // __UIEDIT_H__
#include "StdAfx.h" #include "..\stdafx.h" #include "UIIpAddress.h" #include <string> namespace DuiLib { CIpAddressUI::CIpAddressUI() { p=NULL; p2=NULL; p3=NULL; p4=NULL; } void CIpAddressUI::Init() { this->SetAttribute(_T("inset"),"1,1,1,1"); CHorizontalLayoutUI::Init(); //p- } void CIpAddressUI::DoInit() { CHorizontalLayoutUI::DoInit(); p=new CEditUI(); int width=this->GetWidth()/3-4; p->SetName("p"); p->SetAttribute("width","20"); p->SetAttribute("bkcolor","#00000000"); p->SetAttribute("texttype","integer");//扩展属性 见http://git.oschina.net/Qyerp/DuiLib p->SetAttribute("minvalue","0");//扩展属性 p->SetAttribute("maxvalue","255");//扩展属性 //p->SetPos(rcPos); this->Add(p); CLabelUI *dot1=new CLabelUI(); dot1->SetText("."); dot1->SetAttribute("padding","1,0,2,0"); dot1->SetAttribute("width","5"); this->Add(dot1); p2=new CEditUI(); p2->SetName("p2"); p2->SetAttribute("width","20"); p2->SetAttribute("bkcolor","#00000000"); p2->SetAttribute("texttype","integer"); p2->SetAttribute("minvalue","0"); p2->SetAttribute("maxvalue","255"); this->Add(p2); CLabelUI *dot2=new CLabelUI(); dot2->SetText("."); dot2->SetAttribute("padding","1,0,2,0"); dot2->SetAttribute("width","5"); this->Add(dot2); p3=new CEditUI(); p3->SetName("p3"); p3->SetAttribute("width","20"); p3->SetAttribute("bkcolor","#00000000"); p3->SetAttribute("texttype","integer"); p3->SetAttribute("minvalue","0"); p3->SetAttribute("maxvalue","255"); this->Add(p3); CLabelUI *dot3=new CLabelUI(); dot3->SetText("."); dot3->SetAttribute("padding","1,0,2,0"); dot3->SetAttribute("width","5"); this->Add(dot3); p4=new CEditUI(); p4->SetName("p4"); p4->SetAttribute("width","20"); p4->SetAttribute("bkcolor","#00000000"); p4->SetAttribute("texttype","integer"); p4->SetAttribute("minvalue","0"); p4->SetAttribute("maxvalue","255"); this->Add(p4); } void CIpAddressUI::SetPos(RECT rc) { CHorizontalLayoutUI::SetPos(rc); int width=this->GetWidth()/4-4; char abc[100]; _stprintf_s(abc,"%d",width); if(p!=NULL) { RECT rcCtrl = rc; rcCtrl.right=rcCtrl.left+width; p->SetPos(rcCtrl); rcCtrl.left=rcCtrl.right; rcCtrl.right=rcCtrl.left+width; p2->SetPos(rcCtrl); rcCtrl.left=rcCtrl.right; rcCtrl.right=rcCtrl.left+width; p3->SetPos(rcCtrl); rcCtrl.left=rcCtrl.right; rcCtrl.right=rcCtrl.left+width; p4->SetPos(rcCtrl); //p3->SetAttribute("width",abc); //p4->SetAttribute("width",abc);/**/ } } CDuiString CIpAddressUI::GetText() const { //m_sText=p->GetText()+_T(".")+p2->GetText()+p3->GetText()+p4->GetText(); return m_sText; } void CIpAddressUI::SetText(LPCTSTR pstrText) { CHorizontalLayoutUI::SetText(pstrText); CDuiString one=pstrText; int pot=one.Find("."); if(p!=NULL) p->SetText(one.Left(pot)); one=one.Right(one.GetLength()-pot-1); pot=one.Find("."); if(p2!=NULL) p2->SetText(one.Left(pot)); one=one.Right(one.GetLength()-pot-1); pot=one.Find("."); if(p3!=NULL) p3->SetText(one.Left(pot)); one=one.Right(one.GetLength()-pot-1); //pot=one.Find("."); if(p4!=NULL) p4->SetText(one); //one=one.Right(one.GetLength()-pot); } void CIpAddressUI::DoEvent(TEventUI& e) { //char *pppp=(char*)e.pSender->GetClass(); if(e.Type==UIEVENT__TEXTCHANGED) { if(_tcscmp(e.pSender->GetClass(),_T("EditUI"))==0) { CDuiString one=p->GetText(); CDuiString two=p2->GetText(); CDuiString three=p3->GetText(); CDuiString f=p4->GetText(); CDuiString dot=_T("."); CDuiString fff=one+dot+two+dot+three+dot+f; m_sText=fff.GetData(); } } if(e.Type==UIEVENT_KEYDOWN) { if((e.wParam==VK_TAB||e.wParam==VK_RIGHT)) { if(_tcscmp(e.pSender->GetName(),_T("p"))==0) { p2->SetFocus(); } if(_tcscmp(e.pSender->GetName(),_T("p2"))==0) { p3->SetFocus(); } if(_tcscmp(e.pSender->GetName(),_T("p3"))==0) { p4->SetFocus(); } if(_tcscmp(e.pSender->GetName(),_T("p4"))==0) { p->SetFocus(); } } if(e.wParam==VK_LEFT ) { if(_tcscmp(e.pSender->GetName(),_T("p"))==0) { p4->SetFocus(); } if(_tcscmp(e.pSender->GetName(),_T("p2"))==0) { p->SetFocus(); } if(_tcscmp(e.pSender->GetName(),_T("p3"))==0) { p2->SetFocus(); } if(_tcscmp(e.pSender->GetName(),_T("p4"))==0) { p3->SetFocus(); } } } } }