⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynamyipview.cpp

📁 动态改变IP地址 本人的IP地址时常变动
💻 CPP
字号:
// DynaMyIPView.cpp : implementation of the CDynaMyIPView class
//

#include "stdafx.h"
#include "DynaMyIP.h"

#include "DynaMyIPDoc.h"
#include "DynaMyIPView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDynaMyIPView

IMPLEMENT_DYNCREATE(CDynaMyIPView, CFormView)

BEGIN_MESSAGE_MAP(CDynaMyIPView, CFormView)
	//{{AFX_MSG_MAP(CDynaMyIPView)
	ON_BN_CLICKED(IDC_BTNSETUP, OnBtnsetup)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDynaMyIPView construction/destruction

CDynaMyIPView::CDynaMyIPView()
	: CFormView(CDynaMyIPView::IDD)
{
	//{{AFX_DATA_INIT(CDynaMyIPView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CDynaMyIPView::~CDynaMyIPView()
{
}

void CDynaMyIPView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDynaMyIPView)
	DDX_Control(pDX, IDC_IPNETMASK, m_IPNETMASK);
	DDX_Control(pDX, IDC_IPGATEWAY, m_IPGATEWAY);
	DDX_Control(pDX, IDC_IPADDRESS, m_IPADDRESS);
	DDX_Control(pDX, IDC_BMPBACKGROUND, m_bmpBack);
	//}}AFX_DATA_MAP
}

BOOL CDynaMyIPView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

bool CDynaMyIPView::FileExits(const LPCTSTR FileName)
{  
  HANDLE Handle;
  WIN32_FIND_DATA FindData;
  //LOCALFIocalFileTime TFileTime;
  Handle = FindFirstFile(FileName, &FindData);
  if (Handle != INVALID_HANDLE_VALUE)
  {
    FindClose(Handle);

	TRACE1("%d...",
		FindData.dwFileAttributes,
		);
	
	TRACE1("%d.....",
		FILE_ATTRIBUTE_DIRECTORY);

	TRACE1("%d.......",
		FindData.dwFileAttributes && FILE_ATTRIBUTE_DIRECTORY);


    if ((FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) 
    {
		return true;
	}
  }
  return  false;
}

void CDynaMyIPView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
		
	CWnd * pWnd = GetDlgItem(IDC_BMPBACKGROUND);
	pWnd->SetWindowPos(&wndBottom,0,0,0,0,SWP_NOSIZE+SWP_NOMOVE);

	if (FileExits(FILENAME) )
	{
		CFile cfile(FILENAME,CFile::modeRead);
		cfile.Seek(0,CFile::begin);
	
		CArchive arOut(&cfile,CArchive::load);

		arOut >> nField0,nField1,nField2,nField3;
		Serialize(arOut);
		m_IPADDRESS.SetAddress(nField0,nField1,nField2,nField3);

		arOut >> nField0,nField1,nField2,nField3;
		Serialize(arOut);
		m_IPNETMASK.SetAddress(nField0,nField1,nField2,nField3);

		arOut >> nField0,nField1,nField2,nField3;
		Serialize(arOut);
		m_IPGATEWAY.SetAddress(nField0,nField1,nField2,nField3);

		arOut.Close();
		cfile.Close();
			
	}
	else
	{
		m_IPADDRESS.SetAddress(10,168,0,130);
		m_IPNETMASK.SetAddress(255,255,255,0);
		m_IPGATEWAY.SetAddress(10,168,0,1);
	}

}

/////////////////////////////////////////////////////////////////////////////
// CDynaMyIPView diagnostics

#ifdef _DEBUG
void CDynaMyIPView::AssertValid() const
{
	CFormView::AssertValid();
}

void CDynaMyIPView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CDynaMyIPDoc* CDynaMyIPView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDynaMyIPDoc)));
	return (CDynaMyIPDoc*)m_pDocument;
}
#endif //_DEBUG


void CDynaMyIPView::OnBtnsetup() 
{
	// TODO: Add your control notification handler code here
	CString sAddr,sMask,sGate;

	CFile cfile(FILENAME,CFile::modeWrite|CFile::modeCreate);
	cfile.Seek(0,CFile::begin);

	CArchive arIn(&cfile,CArchive::store);

	m_IPADDRESS.GetAddress(nField0,nField1,nField2,nField3); 
	sAddr.Format("%d.%d.%d.%d ",nField0,nField1,nField2,nField3); 
	arIn << nField0, nField1, nField2, nField3;
	Serialize(arIn);

	m_IPNETMASK.GetAddress(nField0,nField1,nField2,nField3); 
	sMask.Format("%d.%d.%d.%d ",nField0,nField1,nField2,nField3); 
	arIn << nField0, nField1, nField2, nField3;
	Serialize(arIn);

	m_IPGATEWAY.GetAddress(nField0,nField1,nField2,nField3); 
	sGate.Format("%d.%d.%d.%d ",nField0,nField1,nField2,nField3); 
	arIn << nField0, nField1, nField2, nField3;
	Serialize(arIn);

	sAddr = " /c netsh interface ip set address \"本地连接\" static " +
		sAddr + sMask + sGate +"1";
	TRACE0(sAddr);

	arIn.Close();
	cfile.Close();

	ShellExecute(NULL,"open","cmd.exe",sAddr,NULL,SW_HIDE);

}

void CDynaMyIPView::Serialize(CArchive& ar) 
{
	if (ar.IsStoring())
	{	// storing code
		ar<<nField0;
		ar<<nField1;
		ar<<nField2;
		ar<<nField3;
	}
	else
	{	// loading code
		ar>>nField0;
		ar>>nField1;
		ar>>nField2;
		ar>>nField3;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -