UIIPAddressEx.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include "StdAfx.h"
  2. #include <Shlwapi.h>
  3. #pragma comment(lib, "ws2_32.lib")
  4. #pragma comment(lib, "shlwapi.lib")
  5. namespace DuiLib
  6. {
  7. IMPLEMENT_DUICONTROL(CIPAddressExUI)
  8. CIPAddressExUI::CIPAddressExUI()
  9. {
  10. m_nActiveSection = 0;
  11. SetReadOnly(true);
  12. m_nFirst = 0;
  13. m_nSecond = 0;
  14. m_nThird = 0;
  15. m_nFourth = 0;
  16. UpdateText();
  17. }
  18. LPCTSTR CIPAddressExUI::GetClass() const
  19. {
  20. return _T("IPAddressExUI");
  21. }
  22. LPVOID CIPAddressExUI::GetInterface(LPCTSTR pstrName)
  23. {
  24. if( _tcscmp(pstrName, DUI_CTR_IPADDRESS) == 0 )
  25. {
  26. return static_cast<CIPAddressExUI*>(this);
  27. }
  28. return CEditUI::GetInterface(pstrName);
  29. }
  30. UINT CIPAddressExUI::GetControlFlags() const
  31. {
  32. if( !IsEnabled() )
  33. {
  34. return CControlUI::GetControlFlags();
  35. }
  36. return UIFLAG_SETCURSOR | UIFLAG_TABSTOP;
  37. }
  38. void CIPAddressExUI::GetNumInput(TCHAR chKey)
  39. {
  40. if (chKey == 0x30 || chKey == VK_NUMPAD0) {m_chNum = '0';}
  41. else if (chKey == 0x31 || chKey == VK_NUMPAD1) {m_chNum = '1';}
  42. else if (chKey == 0x32 || chKey == VK_NUMPAD2) {m_chNum = '2';}
  43. else if (chKey == 0x33 || chKey == VK_NUMPAD3) {m_chNum = '3';}
  44. else if (chKey == 0x34 || chKey == VK_NUMPAD4) {m_chNum = '4';}
  45. else if (chKey == 0x35 || chKey == VK_NUMPAD5) {m_chNum = '5';}
  46. else if (chKey == 0x36 || chKey == VK_NUMPAD6) {m_chNum = '6';}
  47. else if (chKey == 0x37 || chKey == VK_NUMPAD7) {m_chNum = '7';}
  48. else if (chKey == 0x38 || chKey == VK_NUMPAD8) {m_chNum = '8';}
  49. else if (chKey == 0x39 || chKey == VK_NUMPAD9) {m_chNum = '9';}
  50. m_strNum += m_chNum;
  51. CharToInt();
  52. if ((m_strNum.GetLength() == 3) && (m_nActiveSection < 4))
  53. {
  54. m_nActiveSection++;
  55. m_strNum.Empty();
  56. }
  57. }
  58. void CIPAddressExUI::CharToInt()
  59. {
  60. TCHAR szNum[MAX_PATH] = {0};
  61. lstrcpyn(szNum, m_strNum.GetData(), MAX_PATH);
  62. int nSection = _ttoi(szNum);
  63. if (nSection <= 0)
  64. {
  65. nSection = 0;
  66. }
  67. else if (nSection > 255)
  68. {
  69. nSection = 255;
  70. }
  71. switch (m_nActiveSection)
  72. {
  73. case 1:
  74. m_nFirst = nSection;
  75. break;
  76. case 2:
  77. m_nSecond = nSection;
  78. break;
  79. case 3:
  80. m_nThird = nSection;
  81. break;
  82. case 4:
  83. m_nFourth = nSection;
  84. break;
  85. default:
  86. break;
  87. }
  88. UpdateText();
  89. }
  90. void CIPAddressExUI::DoEvent(TEventUI& event)
  91. {
  92. if( event.Type == UIEVENT_KILLFOCUS && IsEnabled() )
  93. {
  94. m_nActiveSection = 0;
  95. Invalidate();
  96. }
  97. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK || event.Type == UIEVENT_RBUTTONDOWN)
  98. {
  99. if( !IsEnabled() )
  100. {
  101. return;
  102. }
  103. m_strNum.Empty();
  104. POINT p = event.ptMouse;
  105. RECT r = GetPos();
  106. // 判断焦点范围确定哪一段被选中
  107. int nFocus = (r.right - r.left) / 4;
  108. if(p.x - r.left <= nFocus)
  109. {
  110. m_nActiveSection = 1;
  111. }
  112. else if((p.x - r.left > nFocus) && (p.x - r.left <= nFocus * 2))
  113. {
  114. m_nActiveSection = 2;
  115. }
  116. else if ((p.x - r.left > nFocus * 2) && (p.x - r.left <= nFocus * 3))
  117. {
  118. m_nActiveSection = 3;
  119. }
  120. else
  121. {
  122. m_nActiveSection = 4;
  123. }
  124. UpdateText();
  125. }
  126. else if( event.Type == UIEVENT_SCROLLWHEEL )
  127. {
  128. if( !IsEnabled() )
  129. {
  130. return;
  131. }
  132. if( event.wParam )
  133. {
  134. DecNum();
  135. }
  136. else
  137. {
  138. IncNum();
  139. }
  140. }
  141. else if( event.Type == UIEVENT_KEYDOWN )
  142. {
  143. if( !IsEnabled() )
  144. {
  145. return;
  146. }
  147. // 删除
  148. if ((event.chKey == VK_DELETE) || (event.chKey == VK_BACK))
  149. {
  150. switch (m_nActiveSection)
  151. {
  152. case 1:
  153. m_nFirst = 0;
  154. break;
  155. case 2:
  156. m_nSecond = 0;
  157. break;
  158. case 3:
  159. m_nThird = 0;
  160. break;
  161. case 4:
  162. m_nFourth = 0;
  163. break;
  164. default:
  165. break;
  166. }
  167. m_strNum.Empty();
  168. UpdateText();
  169. }
  170. // 获取输入字符
  171. if ((m_nActiveSection == 1) && (event.chKey >= 0x30) && (event.chKey <= 0x39) ||
  172. (m_nActiveSection == 1) && (event.chKey >= VK_NUMPAD0) && (event.chKey <= VK_NUMPAD9))
  173. {
  174. GetNumInput(event.chKey);
  175. }
  176. else if ((m_nActiveSection == 2) && (event.chKey >= 0x30) && (event.chKey <= 0x39) ||
  177. (m_nActiveSection == 2) && (event.chKey >= VK_NUMPAD0) && (event.chKey <= VK_NUMPAD9))
  178. {
  179. GetNumInput(event.chKey);
  180. }
  181. else if ((m_nActiveSection == 3) && (event.chKey >= 0x30) && (event.chKey <= 0x39) ||
  182. (m_nActiveSection == 3) && (event.chKey >= VK_NUMPAD0) && (event.chKey <= VK_NUMPAD9))
  183. {
  184. GetNumInput(event.chKey);
  185. }
  186. else if ((m_nActiveSection == 4) && (event.chKey >= 0x30) && (event.chKey <= 0x39) ||
  187. (m_nActiveSection == 4) && (event.chKey >= VK_NUMPAD0) && (event.chKey <= VK_NUMPAD9))
  188. {
  189. GetNumInput(event.chKey);
  190. }
  191. if( event.chKey == VK_UP )
  192. {
  193. IncNum();
  194. }
  195. else if( event.chKey == VK_DOWN )
  196. {
  197. DecNum();
  198. }
  199. else if( event.chKey == VK_LEFT )
  200. {
  201. if( m_nActiveSection > 1 )
  202. {
  203. if (!m_strNum.IsEmpty())
  204. {
  205. CharToInt();
  206. m_strNum.Empty();
  207. }
  208. m_nActiveSection--;
  209. Invalidate();
  210. }
  211. }
  212. else if( event.chKey == VK_RIGHT )
  213. {
  214. if( m_nActiveSection < 4 )
  215. {
  216. if (!m_strNum.IsEmpty())
  217. {
  218. CharToInt();
  219. m_strNum.Empty();
  220. }
  221. m_nActiveSection++;
  222. Invalidate();
  223. }
  224. }
  225. else if ((event.chKey == VK_OEM_PERIOD) || (event.chKey == VK_DECIMAL))
  226. {
  227. if( m_nActiveSection < 4 )
  228. {
  229. if (!m_strNum.IsEmpty())
  230. {
  231. CharToInt();
  232. m_strNum.Empty();
  233. }
  234. m_nActiveSection++;
  235. Invalidate();
  236. }
  237. }
  238. }
  239. CLabelUI::DoEvent(event);
  240. }
  241. void CIPAddressExUI::PaintText(HDC hDC)
  242. {
  243. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  244. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  245. if( m_sText.IsEmpty() ) return;
  246. RECT rc = m_rcItem;
  247. rc.left += m_rcTextPadding.left;
  248. rc.right -= m_rcTextPadding.right;
  249. rc.top += m_rcTextPadding.top;
  250. rc.bottom -= m_rcTextPadding.bottom;
  251. DWORD dwTextColor = IsEnabled() ? m_dwTextColor : m_dwDisabledTextColor;
  252. HFONT hOldFont = (HFONT)::SelectObject(hDC, m_pManager->GetFont(m_iFont));
  253. char szFirst[8] = {0};
  254. char szSecond[8] = {0};
  255. char szThird[8] = {0};
  256. char szFourth[8] = {0};
  257. char szDivide[8] = {"."};
  258. wsprintfA(szFirst, "%d", m_nFirst);
  259. wsprintfA(szSecond, "%d", m_nSecond);
  260. wsprintfA(szThird, "%d", m_nThird);
  261. wsprintfA(szFourth, "%d", m_nFourth);
  262. SIZE First;
  263. SIZE Second;
  264. SIZE Third;
  265. SIZE Fourth;
  266. SIZE divideSize;
  267. GetTextExtentPointA(hDC, szFirst, 3, &First);
  268. GetTextExtentPointA(hDC, szSecond, 3, &Second);
  269. GetTextExtentPointA(hDC, szThird, 3, &Third);
  270. GetTextExtentPointA(hDC, szFourth, 3, &Fourth);
  271. GetTextExtentPointA(hDC, szFourth, 1, &divideSize);
  272. ::SetBkMode(hDC, TRANSPARENT);
  273. ::SetTextColor(hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
  274. //Start Test Draw point (".")
  275. RECT rcPoint = rc;
  276. RECT rcIP = rc;
  277. int nIPAddrWidth = rcPoint.right - rcPoint.left;
  278. int nPointPos = nIPAddrWidth / 4;
  279. for (int i = 0; i < 3; i++)
  280. {
  281. rcPoint.left += nPointPos;
  282. ::DrawTextA(hDC, szDivide, 1, &rcPoint, DT_SINGLELINE | m_uTextStyle | DT_NOPREFIX);
  283. }
  284. //End
  285. if (m_nFirst == 0 &&
  286. m_nSecond == 0 &&
  287. m_nThird == 0 &&
  288. m_nFourth == 0 &&
  289. m_nActiveSection == 0
  290. )
  291. {
  292. return;
  293. }
  294. int nIPPos = nPointPos / 2;
  295. if( 1 == m_nActiveSection && IsEnabled() )
  296. {
  297. ::SetBkMode(hDC, OPAQUE);
  298. ::SetBkColor(hDC, RGB(51, 153, 255));
  299. ::SetTextColor(hDC, RGB(255, 255, 255));
  300. }
  301. rcIP.left = rc.left + nIPPos;
  302. ::DrawTextA(hDC, szFirst, 3, &rcIP, DT_SINGLELINE | m_uTextStyle | DT_NOPREFIX);
  303. rc.left += nPointPos;
  304. ::SetBkMode(hDC, TRANSPARENT);
  305. ::SetTextColor(hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
  306. if( 2 == m_nActiveSection && IsEnabled() )
  307. {
  308. ::SetBkMode(hDC, OPAQUE);
  309. ::SetBkColor(hDC, RGB(51, 153, 255));
  310. ::SetTextColor(hDC, RGB(255, 255, 255));
  311. }
  312. rcIP.left = rc.left + nIPPos;
  313. ::DrawTextA(hDC, szSecond, 3, &rcIP, DT_SINGLELINE | m_uTextStyle | DT_NOPREFIX);
  314. rc.left += nPointPos;
  315. ::SetBkMode(hDC, TRANSPARENT);
  316. ::SetTextColor(hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
  317. if( 3 == m_nActiveSection && IsEnabled() )
  318. {
  319. ::SetBkMode(hDC, OPAQUE);
  320. ::SetBkColor(hDC, RGB(51, 153, 255));
  321. ::SetTextColor(hDC, RGB(255, 255, 255));
  322. }
  323. rcIP.left = rc.left + nIPPos;
  324. ::DrawTextA(hDC, szThird, 3, &rcIP, DT_SINGLELINE | m_uTextStyle | DT_NOPREFIX);
  325. rc.left += nPointPos;
  326. ::SetBkMode(hDC, TRANSPARENT);
  327. ::SetTextColor(hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
  328. if( 4 == m_nActiveSection && IsEnabled() )
  329. {
  330. ::SetBkMode(hDC, OPAQUE);
  331. ::SetBkColor(hDC, RGB(51, 153, 255));
  332. ::SetTextColor(hDC, RGB(255, 255, 255));
  333. }
  334. rcIP.left = rc.left + nIPPos;
  335. ::DrawTextA(hDC, szFourth, 3, &rcIP, DT_SINGLELINE | m_uTextStyle | DT_NOPREFIX);
  336. ::SetBkMode(hDC, TRANSPARENT);
  337. ::SetTextColor(hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
  338. ::SelectObject(hDC, hOldFont);
  339. }
  340. void CIPAddressExUI::SetIP(LPCTSTR lpIP)
  341. {
  342. CDuiString sIP = lpIP;
  343. std::vector<CDuiString>vIPs = StrSplit(sIP, _T("."));
  344. if (vIPs.size() == 4) {
  345. m_nFirst = _ttoi(vIPs[0]);
  346. m_nSecond = _ttoi(vIPs[1]);
  347. m_nThird = _ttoi(vIPs[2]);
  348. m_nFourth = _ttoi(vIPs[3]);
  349. }
  350. UpdateText();
  351. }
  352. CDuiString CIPAddressExUI::GetIP()
  353. {
  354. CDuiString strIP;
  355. strIP.Format(_T("%d.%d.%d.%d"), m_nFirst, m_nSecond, m_nThird, m_nFourth);
  356. return strIP;
  357. }
  358. void CIPAddressExUI::UpdateText()
  359. {
  360. TCHAR szIP[MAX_PATH] = {0};
  361. _stprintf(szIP, _T("%d.%d.%d.%d"), m_nFirst, m_nSecond, m_nThird, m_nFourth);
  362. SetText(szIP);
  363. }
  364. void CIPAddressExUI::IncNum()
  365. {
  366. if( m_nActiveSection == 1 )
  367. {
  368. if (m_nFirst < 255)
  369. {
  370. m_nFirst++;
  371. }
  372. }
  373. else if(m_nActiveSection == 2)
  374. {
  375. if(m_nSecond < 255)
  376. {
  377. m_nSecond++;
  378. }
  379. }
  380. else if(m_nActiveSection == 3)
  381. {
  382. if(m_nThird < 255)
  383. {
  384. m_nThird++;
  385. }
  386. }
  387. else if(m_nActiveSection == 4)
  388. {
  389. if(m_nFourth < 255)
  390. {
  391. m_nFourth++;
  392. }
  393. }
  394. UpdateText();
  395. }
  396. void CIPAddressExUI::DecNum()
  397. {
  398. if(m_nActiveSection == 1)
  399. {
  400. if (m_nFirst > 0)
  401. {
  402. m_nFirst--;
  403. }
  404. }
  405. else if(m_nActiveSection == 2)
  406. {
  407. if(m_nSecond > 0)
  408. {
  409. m_nSecond--;
  410. }
  411. }
  412. else if(m_nActiveSection == 3)
  413. {
  414. if(m_nThird > 0)
  415. {
  416. m_nThird--;
  417. }
  418. }
  419. else if(m_nActiveSection == 4)
  420. {
  421. if(m_nFourth > 0)
  422. {
  423. m_nFourth--;
  424. }
  425. }
  426. UpdateText();
  427. }
  428. }