📄 winipcfgdlg.cpp
字号:
// winipcfgDlg.cpp : implementation file
//
#include "stdafx.h"
#include "winipcfg.h"
#include "AboutDlg.h"
#include "winipcfgDlg.h"
#include ".\winipcfgdlg.h"
#include <Iptypes.h>
#include <Iphlpapi.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CWinIPCfgDlg dialog
CWinIPCfgDlg::CWinIPCfgDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWinIPCfgDlg::IDD, pParent)
, m_nDNSPtr(0)
{
// Load the two icon types
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hIconSmall = (HICON)::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
m_byteAdapterBuf = NULL;
}
void CWinIPCfgDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TEXT_HOSTNAME, m_textHostName);
DDX_Control(pDX, IDC_TEXT_DNSDERVER, m_textDNSServer);
DDX_Control(pDX, IDC_TEXT_NODETYPE, m_editNodeType);
DDX_Control(pDX, IDC_TEXT_NETBIOS, m_textNetBIOSScope);
DDX_Control(pDX, IDC_CHECK_IPROUTING, m_checkIPRouting);
DDX_Control(pDX, IDC_CHECK_NETBIOSRES, m_checkNetBIOSResolution);
DDX_Control(pDX, IDC_CHECK_WINSPROXY, m_checkWINSProxy);
DDX_Control(pDX, IDC_COMBO_NETCARD, m_cbNetCard);
DDX_Control(pDX, IDC_TEXT_ADAPTER, m_textAdapterAddress);
DDX_Control(pDX, IDC_TEXT_IPADDRESS, m_textIPAddress);
DDX_Control(pDX, IDC_TEXT_SUBNETMASK, m_textSubnetMask);
DDX_Control(pDX, IDC_TEXT_GATEWAY, m_textGateway);
DDX_Control(pDX, IDC_TEXT_TYPE, m_textType);
DDX_Control(pDX, IDC_TEXT_DHCPSERVER, m_textDHCPServer);
DDX_Control(pDX, IDC_TEXT_WINS_PRIMARY, m_textWINSPrimary);
DDX_Control(pDX, IDC_TEXT_WINS_SECONDARY, m_textWINSSecondary);
DDX_Control(pDX, IDC_TEXT_LEASE_OBTAINED, m_textLeaseObtained);
DDX_Control(pDX, IDC_TEXT_LEASE_EXPIRE, m_textLeaseExpires);
}
BEGIN_MESSAGE_MAP(CWinIPCfgDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON_DNSSERVERS, OnBnClickedButtonDNSServers)
ON_BN_CLICKED(ID_RELEASE, OnBnClickedRelease)
ON_BN_CLICKED(ID_RELEASEALL, OnBnClickedReleaseall)
ON_BN_CLICKED(ID_RENEW, OnBnClickedRenew)
ON_BN_CLICKED(ID_RENEWALL, OnBnClickedRenewall)
ON_WM_DESTROY()
ON_CBN_SELCHANGE(IDC_COMBO_NETCARD, OnCbnSelchangeComboNetcard)
END_MESSAGE_MAP()
// CWinIPCfgDlg message handlers
BOOL CWinIPCfgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CWinApp *pApp = AfxGetApp();
// Add "About..." and "Always on Top" menu items to system menu.
BOOL bTop = pApp->GetProfileInt(_T("Setup"), _T("Top"), FALSE);
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAlwaysOnTop((LPCTSTR)IDS_ALWAYSONTOP);
if (!strAlwaysOnTop.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING|(bTop?MF_CHECKED:0), IDM_ALWAYSONTOP, strAlwaysOnTop);
}
CString strAboutMenu((LPCTSTR)IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIconSmall, FALSE); // Set small icon
// TODO: Add extra initialization here
// Get all of the network info
DWORD dwError;
GetNICs();
// Select the first card
if( m_cbNetCard.GetCount() > 0 )
m_cbNetCard.SetCurSel(0);
// Show all the data entries
OnCbnSelchangeComboNetcard();
{
// Get the size needed for the network data
ULONG ulSize = 0;
(void)GetNetworkParams(NULL, &ulSize);
// allocate the buffer
BYTE *pFixedBuf = new BYTE[ulSize];
// Get the network data
switch( dwError=GetNetworkParams((FIXED_INFO *)pFixedBuf, &ulSize) )
{
case ERROR_SUCCESS:
break;
case ERROR_BUFFER_OVERFLOW:
case ERROR_INVALID_PARAMETER:
case ERROR_NO_DATA:
case ERROR_NOT_SUPPORTED:
default:
{
CString sError;
{
LPVOID lpMsgBuf;
::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
sError = (LPCTSTR)lpMsgBuf;
LocalFree( lpMsgBuf ); // Free the buffer.
}
MessageBox(sError);
EndDialog(IDCANCEL);
return TRUE;
}
break;
}
// Get usable pointer
FIXED_INFO *pFixedInfo = (FIXED_INFO *)pFixedBuf;
CString sTmp, sTmp2;
// Get the host.domain
sTmp = pFixedInfo->HostName;
sTmp2 = pFixedInfo->DomainName;
if( !sTmp2.IsEmpty() )
sTmp += _T(".") +sTmp2;
m_textHostName.SetWindowText(sTmp);
// What type of node?
switch(pFixedInfo->NodeType)
{
case BROADCAST_NODETYPE: sTmp.LoadString(IDS_BROADCAST_NODETYPE); break;
case PEER_TO_PEER_NODETYPE: sTmp.LoadString(IDS_PEER_TO_PEER_NODETYPE); break;
case MIXED_NODETYPE: sTmp.LoadString(IDS_MIXED_NODETYPE); break;
case HYBRID_NODETYPE: sTmp.LoadString(IDS_HYBRID_NODETYPE); break;
}
m_editNodeType.SetWindowText(sTmp);
// set the NetBIOS scope
sTmp = pFixedInfo->ScopeId;
m_textNetBIOSScope.SetWindowText(sTmp);
// Set the checks
m_checkIPRouting.SetCheck(pFixedInfo->EnableRouting==TRUE);
m_checkWINSProxy.SetCheck(pFixedInfo->EnableProxy==TRUE);
m_checkNetBIOSResolution.SetCheck(pFixedInfo->EnableDns==TRUE);
// Fill in the DNS stringlist
m_nDNSPtr = 0;
IP_ADDR_STRING *pIPAS = &pFixedInfo->DnsServerList;
while( pIPAS )
{
m_slDNS.AddTail(pIPAS->IpAddress.String);
pIPAS = pIPAS->Next;
}
// fill in the DNS server box
m_textDNSServer.SetWindowText(m_slDNS.GetHead());
// Clean up the network buffer
delete [] pFixedBuf;
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CWinIPCfgDlg::OnDestroy()
{
CleanupNICs();
CDialog::OnDestroy();
}
void CWinIPCfgDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
switch((nID & 0xFFF0))
{
case IDM_ALWAYSONTOP:
{
// Get the flag
CWinApp *pApp = AfxGetApp();
BOOL bTop = pApp->GetProfileInt(_T("Setup"), _T("Top"), FALSE);
// Invert
bTop = !bTop;
// Set the AlwaysOnTop flag
if( bTop )
SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
else
SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
// Save the new flag
pApp->WriteProfileInt(_T("Setup"), _T("Top"), bTop);
// Check the menu item
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
pSysMenu->CheckMenuItem(IDM_ALWAYSONTOP, MF_BYCOMMAND|(bTop?MF_CHECKED:MF_UNCHECKED));
}
}
break;
case IDM_ABOUTBOX:
{
CAboutDlg dlgAbout(this);
dlgAbout.DoModal();
}
break;
default:
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CWinIPCfgDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CWinIPCfgDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CWinIPCfgDlg::OnOK()
{
// TODO: Add your specialized code here and/or call the base class
CDialog::OnOK();
}
void CWinIPCfgDlg::GetNICs(void)
{
CleanupNICs();
// Get all of the network info
DWORD dwError;
// How much to allocate
ULONG ulSize = 0;
(void)GetAdaptersInfo(NULL, &ulSize);
// Allocate the buffer needed
// This is freed in the destructor
m_byteAdapterBuf = new BYTE[ulSize];
// Get the list of NICs
switch( dwError=GetAdaptersInfo((IP_ADAPTER_INFO *)m_byteAdapterBuf, &ulSize) )
{
case ERROR_SUCCESS:
break;
case ERROR_BUFFER_OVERFLOW:
case ERROR_INVALID_PARAMETER:
case ERROR_NO_DATA:
case ERROR_NOT_SUPPORTED:
default:
{
CString sError;
{
LPVOID lpMsgBuf;
::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
sError = (LPCTSTR)lpMsgBuf;
LocalFree( lpMsgBuf ); // Free the buffer.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -