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

📄 dbclientdlg.cpp

📁 功能:首先
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////
//
//	名称: CDBClientDlg
//
//	功能: 接收XML库文件,并根据XML库格式生成Mdb数据库
//
//	注释:
//
//	编写: 徐景周
//
//  日期: 2005.05.30
//
////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DBClient.h"
#include "DBClientDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDBClientDlg dialog

CDBClientDlg::CDBClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDBClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDBClientDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDBClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDBClientDlg)
	DDX_Control(pDX, IDCANCEL, m_Cancel);
	DDX_Control(pDX, IDC_ACCEPT, m_Accept);
	DDX_Control(pDX, ID_BULIDDB, m_BulidDB);
	DDX_Control(pDX, IDC_IPADDRESS, m_ctlTargetIP);
	DDX_Control(pDX, IDC_LISTACCESS, m_AccessList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDBClientDlg, CDialog)
	//{{AFX_MSG_MAP(CDBClientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_BULIDDB, OnBulidDB)
	ON_BN_CLICKED(IDC_ACCEPT, OnAcceptXML)
	ON_NOTIFY(IPN_FIELDCHANGED, IDC_IPADDRESS, OnFieldchangedIpaddress)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDBClientDlg message handlers
// 初始化对话框
BOOL CDBClientDlg::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

	// 初始化阴影位图按钮
    m_Accept.SetTextColor( RGB(0,0,0) );
	m_Accept.SetToolTipText( "接收XML库文件" );  
	m_Accept.SetShade( SHS_HARDBUMP, 10, 20, 5, RGB(55,55,255) );

	m_BulidDB.SetTextColor( RGB(0,0,0) );
	m_BulidDB.SetToolTipText( "生成数据库" );  
	m_BulidDB.SetShade( SHS_HARDBUMP, 10, 20, 5, RGB(55,55,255) );

	m_Cancel.SetTextColor( RGB(0,0,0) );
	m_Cancel.SetToolTipText( "退出" );  
	m_Cancel.SetShade( SHS_HARDBUMP, 10, 20, 5, RGB(55,55,255) ); 
	
	// 初始化IP本机地址
	QueryLocalIP();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDBClientDlg::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 CDBClientDlg::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 CDBClientDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//////////////////////////////////////////////////////////////////////////////
//名称:QueryLocalIP
//功能:查询本机IP及主机名
//作者:徐景周(jingzhou_xu@163.com)
//组织:未来工作室(Future Studio)
//日期:2005.05.30
/////////////////////////////////////////////////////////////////////////////
void CDBClientDlg::QueryLocalIP() 
{
	char szHostName[128];
	if( gethostname( szHostName, 128 ) == 0 )
	{
		struct hostent *pHost;
		pHost = gethostbyname( szHostName );
		for( int i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; ++i )
		{
			CString strIP	= inet_ntoa( *(struct in_addr*)pHost->h_addr_list[i] );
			DWORD dwIP		= ntohl( inet_addr( strIP.GetBuffer( MAX_PATH ) ) );
			// IP控件赋值
			m_ctlTargetIP.SetAddress( dwIP );
			m_strTargetIP	= strIP;
		}
	}
}

#define PRE_AGREED_PORT		8686		// 指定端口号
#define RECV_BUFFER_SIZE	4096		// 缓冲区大小
//////////////////////////////////////////////////////////////////////////////
//名称:GetFileFromRemoteSender
//功能:通过IP接收XML库文件,并保存为指定文件名
//作者:徐景周(jingzhou_xu@163.com)
//组织:未来工作室(Future Studio)
//日期:2005.05.30
/////////////////////////////////////////////////////////////////////////////
BOOL CDBClientDlg::GetFileFromRemoteSender(CString strIP, CString fName)
{
	// 创建客户端socket
	AfxSocketInit( NULL );	// 初始化socket
	CSocket sockClient;
	sockClient.Create();
	
	// 连接到指定IP和端口号
	sockClient.Connect( strIP, PRE_AGREED_PORT );	// PRE_AGREED_PORT 端口号被指定为 8686
	
	
	// 传送是否成功
	BOOL bRet = TRUE;								
	
	int dataLength, cbBytesRet, cbLeftToReceive;	// 接收数据长度及进度
	
	BYTE* recdData = NULL;							// 接收数据缓冲
	
	CFile destFile;
	CFileException fe;
	BOOL bFileIsOpen = FALSE;
	
	// 打开或创建指定文件,用来接收数据
	if( !( bFileIsOpen = destFile.Open( fName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary, &fe ) ) )
	{
		TCHAR strCause[256];
		fe.GetErrorMessage( strCause, 255 );
		TRACE( "GetFileFromRemoteSender encountered an error while opening the local file\n"
			"\tFile name = %s\n\tCause = %s\n\tm_cause = %d\n\tm_IOsError = %d\n",
			fe.m_strFileName, strCause, fe.m_cause, fe.m_lOsError );
		
		
		bRet = FALSE;
		goto PreReturnCleanup;
	}
	
	// 首先,获取文件长度
	cbLeftToReceive = sizeof( dataLength );
	do
	{
		BYTE* bp = (BYTE*)(&dataLength) + sizeof(dataLength) - cbLeftToReceive;
		cbBytesRet = sockClient.Receive( bp, cbLeftToReceive );
		
		// 是否出错
		if( cbBytesRet == SOCKET_ERROR || cbBytesRet == 0 )
		{
			int iErr = ::GetLastError();
			TRACE( "GetFileFromRemoteSite returned a socket error while getting file length\n"
				"\tNumber of Bytes received (zero means connection was closed) = %d\n"
				"\tGetLastError = %d\n", cbBytesRet, iErr );
			
			bRet = FALSE;
			goto PreReturnCleanup;
		}
		
		// 接收成功,剩余长度
		cbLeftToReceive -= cbBytesRet;
		
	}
	while( cbLeftToReceive > 0 );
	
	dataLength = ntohl( dataLength );
	
	// 然后,每次接收指定缓冲的数据
	recdData		= new byte[RECV_BUFFER_SIZE];
	cbLeftToReceive = dataLength;
	
	do
	{	
		int iiGet, iiRecd;
		
		iiGet = (cbLeftToReceive<RECV_BUFFER_SIZE) ? cbLeftToReceive : RECV_BUFFER_SIZE ;
		iiRecd = sockClient.Receive( recdData, iiGet );
		
		// 是否错误
		if ( iiRecd == SOCKET_ERROR || iiRecd == 0 )
		{
			int iErr = ::GetLastError();
			TRACE( "GetFileFromRemoteSite returned a socket error while getting chunked file data\n"
				"\tNumber of Bytes received (zero means connection was closed) = %d\n"
				"\tGetLastError = %d\n", iiRecd, iErr );
			
			bRet = FALSE;
			goto PreReturnCleanup;
		}
		
		// 接收成功, 剩余字节数
		destFile.Write( recdData, iiRecd ); // 写入文件
		cbLeftToReceive -= iiRecd;
		
	} 
	while( cbLeftToReceive > 0 );
	
PreReturnCleanup:		// 结束及清理
	
	// 释放内存及关闭打开句柄
	delete[] recdData;		
	
	if ( bFileIsOpen )
		destFile.Close();
	
	sockClient.Close();
	
	return bRet;
}

//////////////////////////////////////////////////////////////////////////////
//名称:OnAcceptXML
//功能:通过Socket接收XML库文件
//作者:徐景周(jingzhou_xu@163.com)
//组织:未来工作室(Future Studio)
//日期:2005.05.30
/////////////////////////////////////////////////////////////////////////////
void CDBClientDlg::OnAcceptXML() 
{
	// 接收要保存的XML文件
	CFileDialog dlg( FALSE, "Xml", "*.xml", OFN_HIDEREADONLY, "*.xml||" );
	dlg.m_ofn.lpstrTitle = "保存接收的XML格式库";
	if( IDCANCEL == dlg.DoModal() )
		return ;

	CString strXmlName	= dlg.GetPathName();
	if( GetFileFromRemoteSender( m_strTargetIP, strXmlName ) )
		AfxMessageBox( strXmlName + "XML库文件接收成功!" );
	else
		AfxMessageBox( strXmlName + "XML库文件接收失败!" );
}

//////////////////////////////////////////////////////////////////////////////
//名称:OnBulidDB
//功能:根据接收的XML库文件,生成数据库
//作者:徐景周(jingzhou_xu@163.com)
//组织:未来工作室(Future Studio)
//日期:2005.05.30
/////////////////////////////////////////////////////////////////////////////
void CDBClientDlg::OnBulidDB() 
{
	// 获取XML文件
	CFileDialog dlg( TRUE, "Xml", "*.xml", OFN_HIDEREADONLY, "*.xml||" );
	dlg.m_ofn.lpstrTitle = "生成XML格式数据库";
	if( IDCANCEL == dlg.DoModal() )
		return ;

	CString strXmlName	= dlg.GetPathName();

	// 读取XML库,并生成Mdb数据库
	TRY
	{
		CBulidDB *pDBBulid = new CBulidDB();
		// 生成并显示数据库内容
		if( pDBBulid->Load_XML_Document( strXmlName ) )
			ReadAccessDB( *pDBBulid );

		delete pDBBulid;
	}
	CATCH( CException, pEx )
	{

		pEx->ReportError();
	}
	END_CATCH
	
}

//////////////////////////////////////////////////////////////////////////////
//名称:OnReadAccess
//功能:从Access文件中读取相应数据并显示出来
//作者:徐景周(jingzhou_xu@163.com)
//组织:未来工作室(Future Studio)
//日期:2005.05.30
/////////////////////////////////////////////////////////////////////////////
void CDBClientDlg::ReadAccessDB( CBulidDB& dbBulid ) 
{
	COleVariant var;		// 字段类型
	var.ChangeType( VT_BSTR, NULL );

	// 清空列表框
	m_AccessList.ResetContent();

	// 获取主程序所在路径,存在sPath中
	CString sPath;
	GetModuleFileName( NULL, sPath.GetBufferSetLength( MAX_PATH + 1 ), MAX_PATH );
	sPath.ReleaseBuffer();
	int nPos;
	nPos	= sPath.ReverseFind( '\\' );
	sPath	= sPath.Left( nPos );

	CString strFile = sPath + "\\" + dbBulid.Get_DBName();
	CDaoDatabase db;					// 数据库
	CDaoRecordset RecSet( &db );		// 记录集
	db.Open( strFile );					// 打开已创建的数据库及表

	CString strQuery = "SELECT * FROM " + dbBulid.Get_TableName();
	RecSet.Open( AFX_DAO_USE_DEFAULT_TYPE, strQuery, NULL );

	CString strName, strAge;
	while( !RecSet.IsEOF() )			// 有没有到表结尾
	{
		RecSet.GetFieldValue( "Name", var );
		strName = (LPCSTR)var.pbstrVal;
		RecSet.GetFieldValue( "Age", var );
		strAge = (LPCSTR)var.pbstrVal;
		m_AccessList.AddString( strName + " --> "+strAge );

		RecSet.MoveNext();
	}

	// 关闭记录集及库
	RecSet.Close();
	db.Close();
}

// IP地址改变时
void CDBClientDlg::OnFieldchangedIpaddress(NMHDR* pNMHDR, LRESULT* pResult) 
{
	DWORD dwIP;
	m_ctlTargetIP.GetAddress( dwIP );
	IN_ADDR ia;
	ia.S_un.S_addr	= htonl( dwIP );
	m_strTargetIP	= inet_ntoa( ia );
	
	*pResult = 0;
}

⌨️ 快捷键说明

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