UIIPAddress.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include "StdAfx.h"
  2. #pragma comment( lib, "ws2_32.lib" )
  3. DWORD GetLocalIpAddress()
  4. {
  5. WORD wVersionRequested = MAKEWORD(2, 2);
  6. WSADATA wsaData;
  7. if (WSAStartup(wVersionRequested, &wsaData) != 0)
  8. return 0;
  9. char local[255] = {0};
  10. gethostname(local, sizeof(local));
  11. hostent* ph = gethostbyname(local);
  12. if (ph == NULL)
  13. return 0;
  14. in_addr addr;
  15. memcpy(&addr, ph->h_addr_list[0], sizeof(in_addr));
  16. DWORD dwIP = MAKEIPADDRESS(addr.S_un.S_un_b.s_b1, addr.S_un.S_un_b.s_b2, addr.S_un.S_un_b.s_b3, addr.S_un.S_un_b.s_b4);
  17. return dwIP;
  18. }
  19. namespace DuiLib
  20. {
  21. //CDateTimeUI::m_nDTUpdateFlag
  22. #define IP_NONE 0
  23. #define IP_UPDATE 1
  24. #define IP_DELETE 2
  25. #define IP_KEEP 3
  26. class CIPAddressWnd : public CWindowWnd
  27. {
  28. public:
  29. CIPAddressWnd();
  30. void Init(CIPAddressUI* pOwner);
  31. RECT CalPos();
  32. LPCTSTR GetWindowClassName() const;
  33. LPCTSTR GetSuperClassName() const;
  34. void OnFinalMessage(HWND hWnd);
  35. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  36. LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  37. protected:
  38. CIPAddressUI* m_pOwner;
  39. HBRUSH m_hBkBrush;
  40. bool m_bInit;
  41. };
  42. CIPAddressWnd::CIPAddressWnd() : m_pOwner(NULL), m_hBkBrush(NULL), m_bInit(false)
  43. {
  44. }
  45. void CIPAddressWnd::Init(CIPAddressUI* pOwner)
  46. {
  47. m_pOwner = pOwner;
  48. m_pOwner->m_nIPUpdateFlag = IP_NONE;
  49. if (m_hWnd == NULL)
  50. {
  51. INITCOMMONCONTROLSEX CommCtrl;
  52. CommCtrl.dwSize=sizeof(CommCtrl);
  53. CommCtrl.dwICC=ICC_INTERNET_CLASSES;//Ö¸¶¨Class
  54. if(InitCommonControlsEx(&CommCtrl))
  55. {
  56. RECT rcPos = CalPos();
  57. UINT uStyle = WS_CHILD | WS_TABSTOP | WS_GROUP;
  58. Create(m_pOwner->GetManager()->GetPaintWindow(), NULL, uStyle, 0, rcPos);
  59. }
  60. SetWindowFont(m_hWnd, m_pOwner->GetManager()->GetFontInfo(m_pOwner->GetFont())->hFont, TRUE);
  61. }
  62. if (m_pOwner->GetText().IsEmpty())
  63. m_pOwner->m_dwIP = GetLocalIpAddress();
  64. ::SendMessage(m_hWnd, IPM_SETADDRESS, 0, m_pOwner->m_dwIP);
  65. ::ShowWindow(m_hWnd, SW_SHOW);
  66. ::SetFocus(m_hWnd);
  67. m_bInit = true;
  68. }
  69. RECT CIPAddressWnd::CalPos()
  70. {
  71. CDuiRect rcPos = m_pOwner->GetPos();
  72. return rcPos;
  73. }
  74. LPCTSTR CIPAddressWnd::GetWindowClassName() const
  75. {
  76. return _T("IPAddressWnd");
  77. }
  78. LPCTSTR CIPAddressWnd::GetSuperClassName() const
  79. {
  80. return WC_IPADDRESS;
  81. }
  82. void CIPAddressWnd::OnFinalMessage(HWND /*hWnd*/)
  83. {
  84. // Clear reference and die
  85. if( m_hBkBrush != NULL ) ::DeleteObject(m_hBkBrush);
  86. m_pOwner->m_pWindow = NULL;
  87. delete this;
  88. }
  89. LRESULT CIPAddressWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  90. {
  91. LRESULT lRes = 0;
  92. BOOL bHandled = TRUE;
  93. if( uMsg == WM_KILLFOCUS )
  94. {
  95. bHandled = TRUE;
  96. return 0;
  97. lRes = OnKillFocus(uMsg, wParam, lParam, bHandled);
  98. }
  99. else if (uMsg == WM_KEYUP && (wParam == VK_DELETE || wParam == VK_BACK))
  100. {
  101. LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  102. m_pOwner->m_nIPUpdateFlag = IP_DELETE;
  103. m_pOwner->UpdateText();
  104. PostMessage(WM_CLOSE);
  105. return lRes;
  106. }
  107. else if (uMsg == WM_KEYUP && wParam == VK_ESCAPE)
  108. {
  109. LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  110. m_pOwner->m_nIPUpdateFlag = IP_KEEP;
  111. PostMessage(WM_CLOSE);
  112. return lRes;
  113. }
  114. else if( uMsg == OCM_COMMAND ) {
  115. if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_KILLFOCUS )
  116. {
  117. lRes = OnKillFocus(uMsg, wParam, lParam, bHandled);
  118. }
  119. }
  120. else bHandled = FALSE;
  121. if( !bHandled ) return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  122. return lRes;
  123. }
  124. LRESULT CIPAddressWnd::OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  125. {
  126. HWND hWndFocus = GetFocus();
  127. while (hWndFocus)
  128. {
  129. if (GetFocus() == m_hWnd)
  130. {
  131. bHandled = TRUE;
  132. return 0;
  133. }
  134. hWndFocus = GetParent(hWndFocus);
  135. }
  136. LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  137. if (m_pOwner->m_nIPUpdateFlag == IP_NONE)
  138. {
  139. ::SendMessage(m_hWnd, IPM_GETADDRESS, 0, (LPARAM)&m_pOwner->m_dwIP);
  140. m_pOwner->m_nIPUpdateFlag = IP_UPDATE;
  141. m_pOwner->UpdateText();
  142. }
  143. ::ShowWindow(m_hWnd, SW_HIDE);
  144. return lRes;
  145. }
  146. //////////////////////////////////////////////////////////////////////////
  147. //
  148. IMPLEMENT_DUICONTROL(CIPAddressUI)
  149. CIPAddressUI::CIPAddressUI()
  150. {
  151. m_dwIP = GetLocalIpAddress();
  152. m_bReadOnly = false;
  153. m_pWindow = NULL;
  154. m_nIPUpdateFlag=IP_UPDATE;
  155. UpdateText();
  156. m_nIPUpdateFlag = IP_NONE;
  157. }
  158. LPCTSTR CIPAddressUI::GetClass() const
  159. {
  160. return _T("DateTimeUI");
  161. }
  162. LPVOID CIPAddressUI::GetInterface(LPCTSTR pstrName)
  163. {
  164. if( _tcscmp(pstrName, DUI_CTR_IPADDRESS) == 0 ) return static_cast<CIPAddressUI*>(this);
  165. return CLabelUI::GetInterface(pstrName);
  166. }
  167. DWORD CIPAddressUI::GetIP()
  168. {
  169. return m_dwIP;
  170. }
  171. void CIPAddressUI::SetIP(DWORD dwIP)
  172. {
  173. m_dwIP = dwIP;
  174. UpdateText();
  175. }
  176. void CIPAddressUI::SetReadOnly(bool bReadOnly)
  177. {
  178. m_bReadOnly = bReadOnly;
  179. Invalidate();
  180. }
  181. bool CIPAddressUI::IsReadOnly() const
  182. {
  183. return m_bReadOnly;
  184. }
  185. void CIPAddressUI::UpdateText()
  186. {
  187. if (m_nIPUpdateFlag == IP_DELETE)
  188. SetText(_T(""));
  189. else if (m_nIPUpdateFlag == IP_UPDATE)
  190. {
  191. TCHAR szIP[MAX_PATH] = {0};
  192. in_addr addr;
  193. addr.S_un.S_addr = m_dwIP;
  194. _stprintf(szIP, _T("%d.%d.%d.%d"), addr.S_un.S_un_b.s_b4, addr.S_un.S_un_b.s_b3, addr.S_un.S_un_b.s_b2, addr.S_un.S_un_b.s_b1);
  195. SetText(szIP);
  196. }
  197. }
  198. void CIPAddressUI::DoEvent(TEventUI& event)
  199. {
  200. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  201. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  202. else CLabelUI::DoEvent(event);
  203. return;
  204. }
  205. if( event.Type == UIEVENT_SETCURSOR && IsEnabled() )
  206. {
  207. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_IBEAM)));
  208. return;
  209. }
  210. if( event.Type == UIEVENT_WINDOWSIZE )
  211. {
  212. if( m_pWindow != NULL ) m_pManager->SetFocusNeeded(this);
  213. }
  214. if( event.Type == UIEVENT_SCROLLWHEEL )
  215. {
  216. if( m_pWindow != NULL ) return;
  217. }
  218. if( event.Type == UIEVENT_SETFOCUS && IsEnabled() )
  219. {
  220. if( m_pWindow )
  221. {
  222. return;
  223. }
  224. m_pWindow = new CIPAddressWnd();
  225. ASSERT(m_pWindow);
  226. m_pWindow->Init(this);
  227. m_pWindow->ShowWindow();
  228. }
  229. if( event.Type == UIEVENT_KILLFOCUS && IsEnabled() )
  230. {
  231. Invalidate();
  232. }
  233. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK || event.Type == UIEVENT_RBUTTONDOWN)
  234. {
  235. if( IsEnabled() ) {
  236. GetManager()->ReleaseCapture();
  237. if( IsFocused() && m_pWindow == NULL )
  238. {
  239. m_pWindow = new CIPAddressWnd();
  240. ASSERT(m_pWindow);
  241. }
  242. if( m_pWindow != NULL )
  243. {
  244. m_pWindow->Init(this);
  245. m_pWindow->ShowWindow();
  246. }
  247. }
  248. return;
  249. }
  250. if( event.Type == UIEVENT_MOUSEMOVE )
  251. {
  252. return;
  253. }
  254. if( event.Type == UIEVENT_BUTTONUP )
  255. {
  256. return;
  257. }
  258. if( event.Type == UIEVENT_CONTEXTMENU )
  259. {
  260. return;
  261. }
  262. if( event.Type == UIEVENT_MOUSEENTER )
  263. {
  264. return;
  265. }
  266. if( event.Type == UIEVENT_MOUSELEAVE )
  267. {
  268. return;
  269. }
  270. CLabelUI::DoEvent(event);
  271. }
  272. void CIPAddressUI::SetAttribute( LPCTSTR pstrName, LPCTSTR pstrValue )
  273. {
  274. CLabelUI::SetAttribute(pstrName, pstrValue);
  275. }
  276. }