GetNetSettingDlg.cpp

Upload User: lidebao1
Upload Date: 2013-05-01
Package Size: 2437k
Code Size: 7k
Category: Windows Develop
Development Platform: Visual C++
  1. // GetNetSettingDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "GetNetSetting.h"
  5. #include "GetNetSettingDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CGetNetSettingDlg dialog
  50. CGetNetSettingDlg::CGetNetSettingDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CGetNetSettingDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CGetNetSettingDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CGetNetSettingDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CGetNetSettingDlg)
  63. DDX_Control(pDX, IDC_NETCARDLIST, m_listNetcard);
  64. //}}AFX_DATA_MAP
  65. }
  66. BEGIN_MESSAGE_MAP(CGetNetSettingDlg, CDialog)
  67. //{{AFX_MSG_MAP(CGetNetSettingDlg)
  68. ON_WM_SYSCOMMAND()
  69. ON_WM_PAINT()
  70. ON_WM_QUERYDRAGICON()
  71. ON_BN_CLICKED(IDC_QUERYINFO, OnQueryinfo)
  72. //}}AFX_MSG_MAP
  73. END_MESSAGE_MAP()
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CGetNetSettingDlg message handlers
  76. BOOL CGetNetSettingDlg::OnInitDialog()
  77. {
  78. CDialog::OnInitDialog();
  79. // Add "About..." menu item to system menu.
  80. // IDM_ABOUTBOX must be in the system command range.
  81. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  82. ASSERT(IDM_ABOUTBOX < 0xF000);
  83. CMenu* pSysMenu = GetSystemMenu(FALSE);
  84. if (pSysMenu != NULL)
  85. {
  86. CString strAboutMenu;
  87. strAboutMenu.LoadString(IDS_ABOUTBOX);
  88. if (!strAboutMenu.IsEmpty())
  89. {
  90. pSysMenu->AppendMenu(MF_SEPARATOR);
  91. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  92. }
  93. }
  94. // Set the icon for this dialog.  The framework does this automatically
  95. //  when the application's main window is not a dialog
  96. SetIcon(m_hIcon, TRUE); // Set big icon
  97. SetIcon(m_hIcon, FALSE); // Set small icon
  98. m_clsNetSetting.GetSetting();
  99. int nTotalcards = m_clsNetSetting.m_TotalNetCards;
  100. for ( int i = 0 ; i < nTotalcards;i++)
  101. {
  102. m_listNetcard.AddString(m_clsNetSetting.m_Cards[i].szDescription);
  103. }
  104. return TRUE;  // return TRUE  unless you set the focus to a control
  105. }
  106. void CGetNetSettingDlg::OnSysCommand(UINT nID, LPARAM lParam)
  107. {
  108. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  109. {
  110. CAboutDlg dlgAbout;
  111. dlgAbout.DoModal();
  112. }
  113. else
  114. {
  115. CDialog::OnSysCommand(nID, lParam);
  116. }
  117. }
  118. // If you add a minimize button to your dialog, you will need the code below
  119. //  to draw the icon.  For MFC applications using the document/view model,
  120. //  this is automatically done for you by the framework.
  121. void CGetNetSettingDlg::OnPaint() 
  122. {
  123. if (IsIconic())
  124. {
  125. CPaintDC dc(this); // device context for painting
  126. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  127. // Center icon in client rectangle
  128. int cxIcon = GetSystemMetrics(SM_CXICON);
  129. int cyIcon = GetSystemMetrics(SM_CYICON);
  130. CRect rect;
  131. GetClientRect(&rect);
  132. int x = (rect.Width() - cxIcon + 1) / 2;
  133. int y = (rect.Height() - cyIcon + 1) / 2;
  134. // Draw the icon
  135. dc.DrawIcon(x, y, m_hIcon);
  136. }
  137. else
  138. {
  139. CDialog::OnPaint();
  140. }
  141. }
  142. // The system calls this to obtain the cursor to display while the user drags
  143. //  the minimized window.
  144. HCURSOR CGetNetSettingDlg::OnQueryDragIcon()
  145. {
  146. return (HCURSOR) m_hIcon;
  147. }
  148. void CGetNetSettingDlg::OnQueryinfo() 
  149. {
  150.     int nIndex = m_listNetcard.GetCurSel();
  151. if (nIndex == LB_ERR)
  152. {
  153. AfxMessageBox("Please select a netcard!");
  154. }
  155. dlg.Create(IDD_CARDINFO);
  156. dlg.SetWindowText(m_clsNetSetting.m_Cards[nIndex].szDescription);
  157. dlg.m_staticHostName.SetWindowText(m_clsNetSetting.m_szHostName);
  158. dlg.m_staticDomain.SetWindowText(m_clsNetSetting.m_szDomain);
  159. dlg.m_staticIPAddress.SetWindowText(m_clsNetSetting.m_Cards[nIndex].szIpAddress);
  160. if (m_clsNetSetting.m_EnableDNS == 0)
  161. dlg.m_staticIsDns.SetWindowText("否");
  162. if (m_clsNetSetting.m_EnableDNS == 1)
  163. dlg.m_staticIsDns.SetWindowText("是");
  164. else 
  165.         dlg.m_staticIsDns.SetWindowText("未知");
  166. if (m_clsNetSetting.m_IPEnableRouter)
  167. dlg.m_staticIsRoute.SetWindowText("是");
  168. else
  169. dlg.m_staticIsRoute.SetWindowText("否");
  170.     
  171. TCHAR szAddr[128];
  172. wsprintf(szAddr,_T("%02x-%02x-%02x-%02x-%02x-%02x"),m_clsNetSetting.m_Cards[nIndex].szMacAddr[0],
  173. m_clsNetSetting.m_Cards[nIndex].szMacAddr[1],m_clsNetSetting.m_Cards[nIndex].szMacAddr[2],
  174. m_clsNetSetting.m_Cards[nIndex].szMacAddr[3],m_clsNetSetting.m_Cards[nIndex].szMacAddr[4],
  175. m_clsNetSetting.m_Cards[nIndex].szMacAddr[5]);
  176. _tcsupr(szAddr);
  177. dlg.m_addrMAC.SetWindowText(szAddr);
  178.     
  179. dlg.m_listctrlIP.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  180.     dlg.m_listctrlIP.InsertColumn(0,_T("IP地址"),LVCFMT_IMAGE|LVCFMT_LEFT);
  181. dlg.m_listctrlIP.InsertColumn(1,_T("子网掩码"));
  182. dlg.m_listctrlIP.SetColumnWidth(0,110);
  183. dlg.m_listctrlIP.SetColumnWidth(1,110);
  184.     
  185. dlg.m_listctrlIP.DeleteAllItems();
  186. CStringArray strAIpAddr,strAMask,strADnsServer,strGateWay;
  187. SplitString(m_clsNetSetting.m_Cards[nIndex].szGateWay,strGateWay);
  188. dlg.m_listNetGate.ResetContent();
  189. for(int i = 0; i < strGateWay.GetSize(); i++)
  190. dlg.m_listNetGate.AddString(strGateWay[i]);
  191. SplitString(m_clsNetSetting.m_Cards[nIndex].szDNSNameServer,strADnsServer);
  192. dlg.m_listDNS.ResetContent();
  193. for(i = 0; i < strADnsServer.GetSize(); i++)
  194. dlg.m_listDNS.AddString(strADnsServer[i]);
  195. SplitString(m_clsNetSetting.m_Cards[nIndex].szIpAddress,strAIpAddr);
  196. SplitString(m_clsNetSetting.m_Cards[nIndex].szIpMask,strAMask);
  197. dlg.m_listctrlIP.DeleteAllItems();
  198. for(i = 0; i < strAIpAddr.GetSize(); i++)
  199. {
  200. dlg.m_listctrlIP.InsertItem(i,strAIpAddr[i],0);
  201. dlg.m_listctrlIP.SetItemText(i,1,strAMask[i]);
  202. }
  203. strAIpAddr.RemoveAll();
  204. strAMask.RemoveAll();
  205. strADnsServer.RemoveAll();
  206. strGateWay.RemoveAll();
  207. dlg.ShowWindow(TRUE);
  208. }
  209. void CGetNetSettingDlg::SplitString(LPCTSTR lpszString, CStringArray &strArray)
  210. {
  211.     CString strTmp;
  212. TCHAR szTmp[128];
  213. int length = _tcslen(lpszString);
  214. int i = 0;
  215. int k = 0;
  216. while(i < length+1)
  217. {
  218. if(lpszString[i] != _T(' ') && lpszString[i] != _T(',') && lpszString[i] != _T(''))
  219. {
  220. szTmp[k] = lpszString[i];
  221. k++;
  222. }
  223. else
  224. {
  225. szTmp[k] = _T('');
  226. strArray.Add(szTmp);
  227. k = 0;
  228. }
  229. i++;
  230. }
  231. }