📄 powernetconfigdlg.cpp
字号:
// PowerNetConfigDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PowerNetConfig.h"
#include "PowerNetConfigDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <atlbase.h>
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPowerNetConfigDlg dialog
CPowerNetConfigDlg::CPowerNetConfigDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPowerNetConfigDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPowerNetConfigDlg)
m_strName = _T("");
m_strPort = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPowerNetConfigDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPowerNetConfigDlg)
DDX_Control(pDX, IDC_GATEWAY, m_ctrlGateway);
DDX_Control(pDX, IDC_SUBNETMASK, m_ctrlSubNetMask);
DDX_Control(pDX, IDC_PROXY, m_ctrlProxy);
DDX_Control(pDX, IDC_IP, m_ctrlIP);
DDX_Control(pDX, IDC_LIST, m_ctrlConfigList);
DDX_Text(pDX, IDC_NAME, m_strName);
DDX_Text(pDX, IDC_PORT, m_strPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPowerNetConfigDlg, CDialog)
//{{AFX_MSG_MAP(CPowerNetConfigDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(ID_DELETE, OnDelete)
ON_BN_CLICKED(ID_APPLY, OnApply)
ON_CBN_SELCHANGE(IDC_LIST, OnSelchangeList)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPowerNetConfigDlg message handlers
BOOL CPowerNetConfigDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// 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 strAboutMenu;
strAboutMenu.LoadString(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_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//读取所需数据
ReadData();
//加入到列表中去
CComboBox* pList=(CComboBox*)GetDlgItem(IDC_LIST);
POSITION pos;
pos=m_list.GetHeadPosition();
while (pos)
{
CString str;
TCPIPInfo info;
info=m_list.GetNext(pos);
str=info.m_strName;
pList->AddString(str);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CPowerNetConfigDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
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 CPowerNetConfigDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (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 to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPowerNetConfigDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPowerNetConfigDlg::OnAdd()
{
//从对话框中获取相应的数据
GetConfigData();
//判断各个选项是否为空
if (m_strName.IsEmpty() || m_strIP.IsEmpty() || m_strSubNetMask.IsEmpty() ||
m_strGateway.IsEmpty() || m_strProxy.IsEmpty() || m_strPort.IsEmpty())
{
AfxMessageBox("各个选项不能为空",MB_OK);
return;
}
POSITION pos,oldPos;
pos=m_list.GetHeadPosition();
BOOL bFlag=FALSE;
while(pos)
{
oldPos = pos;
TCPIPInfo info;
info=m_list.GetNext(pos);
if (info.m_strName.CompareNoCase(m_strName)==0)
{
bFlag = TRUE;
break;
}
}
if(bFlag) //该配置名已存在于列表中
{
int bOK=AfxMessageBox(_T("该配置名已存在,要覆盖原有数据吗?"),MB_OKCANCEL);
if (bOK==IDOK)
{
//先删除原来的元素
m_list.RemoveAt(oldPos);
//再把修改后的数据加入
TCPIPInfo info;
info.m_strName=m_strName;
info.m_strIP=m_strIP;
info.m_strSubNetMask=m_strSubNetMask;
info.m_strDefaultGateWay=m_strGateway;
info.m_strProxy=m_strProxy;
info.m_strPort=m_strPort;
m_list.AddHead(info);
SaveData();
return;
}
else return;
}
else //如果不在列表中则直接加入即可
{
TCPIPInfo info;
info.m_strName=m_strName;
info.m_strIP=m_strIP;
info.m_strSubNetMask=m_strSubNetMask;
info.m_strDefaultGateWay=m_strGateway;
info.m_strProxy=m_strProxy;
info.m_strPort=m_strPort;
m_list.AddHead(info);
SaveData();
CComboBox* pList=(CComboBox*)GetDlgItem(IDC_LIST);
pList->AddString(m_strName);
}
}
void CPowerNetConfigDlg::OnDelete()
{
CComboBox* pList=(CComboBox*)GetDlgItem(IDC_LIST);
CString strCurr;
pList->GetWindowText(strCurr);
POSITION pos,oldPos;
pos=m_list.GetHeadPosition();
while (pos)
{
oldPos=pos;
CString str;
TCPIPInfo info;
info=m_list.GetNext(pos);
str=info.m_strName;
if (strCurr.CompareNoCase(str)==0)
break;
}
m_list.RemoveAt(oldPos);
pList->DeleteString(pList->GetCurSel());
SaveData();
}
void CPowerNetConfigDlg::OnApply()
{
//取得对话框数据
GetConfigData();
//判断各个条目是否为空
if (m_strName.IsEmpty() || m_strIP.IsEmpty() || m_strSubNetMask.IsEmpty() ||
m_strGateway.IsEmpty() || m_strProxy.IsEmpty() || m_strPort.IsEmpty())
{
AfxMessageBox("各个选项不能为空",MB_OK);
return;
}
SetTCPIPInfo(m_strIP,m_strSubNetMask,m_strGateway);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -