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

📄 client.cpp

📁 用CJ60Lib界面库制作的SQL数据库客户与服务器程序。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// client.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "client.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "clientDoc.h"
#include "clientView.h"
#include "HelpDoc.h"
#include "HelpView.h"
#include "SelDoc.h"
#include "SelView.h"

#include "Msg.h"
#include "LogDlg.h"
#include "Error.h"

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

/////////////////////////////////////////////////////////////////////////////
// Global function
void Message( LPCTSTR text )
{
	CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
	pMainFrm->m_TabBar.InsertText( text );
}

/////////////////////////////////////////////////////////////////////////////
// CClientApp

BEGIN_MESSAGE_MAP(CClientApp, CWinApp)
	//{{AFX_MSG_MAP(CClientApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_CONNECT, OnConnect)
	ON_UPDATE_COMMAND_UI(ID_CONNECT, OnUpdateConnect)
	ON_COMMAND(ID_DISCONNECT, OnDisconnect)
	ON_UPDATE_COMMAND_UI(ID_DISCONNECT, OnUpdateDisconnect)
	ON_COMMAND(ID_HELP_ME, OnHelpMe)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientApp construction

CClientApp::CClientApp()
{
	dir[ 0 ] = '\0';
	::GetCurrentDirectory(1024, dir);

	m_pArchiveIn = NULL;
	m_pArchiveOut = NULL;
	m_pFile = NULL;
	m_pSocket = NULL;

	m_bConnection = FALSE;

	m_address = _T( "" );
	m_port = 0;
	m_name = _T( "" );
	m_type = 0;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CClientApp object

CClientApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CClientApp initialization

BOOL CClientApp::InitInstance()
{
	if (!AfxSocketInit())
	{
		AfxMessageBox( "Socket init failed!" );
		return FALSE;
	}

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("miniSQL Client"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_CLIENTTYPE,
		RUNTIME_CLASS(CClientDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CClientView));
	AddDocTemplate(pDocTemplate);

	// add CHelpDoc and CHelpView into the new document
	AddDocTemplate(
		new CMultiDocTemplate(
		IDR_HELPTYPE,
		RUNTIME_CLASS(CHelpDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CHelpView)) );

	// add CSelDoc and CSelView into the new document
	AddDocTemplate(
		new CMultiDocTemplate(
		IDR_SELTYPE,
		RUNTIME_CLASS(CSelDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CSelView)) );

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;

	// Enable drag/drop open
	m_pMainWnd->DragAcceptFiles();

	// Enable DDE Execute open
	EnableShellOpen();
	RegisterShellFileTypes(TRUE);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The main window has been initialized, so show and update it.
	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

	return Logon();
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CHyperLink	m_hyperLink;
	//}}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)
	virtual BOOL OnInitDialog();
	//}}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)
	DDX_Control(pDX, IDC_URL_MAIL, m_hyperLink);
	//}}AFX_DATA_MAP
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_hyperLink.SetURL(_T("mailto:luxiaochun_1111@163.com"));
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CClientApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CClientApp message handlers

CMsg* CClientApp::ReceiveMsg()
{
	static CMsg msg;

	TRY
	{
		msg.m_msgList.RemoveAll();
		msg.Serialize( *m_pArchiveIn );
	}
	CATCH( CFileException, e )
	{
		msg.m_bClose = TRUE;
		m_pArchiveOut->Abort();

		Message( "Server has reset the connection!\nClosing connection..." );
	}
	END_CATCH

	return &msg;
}

void CClientApp::ProcessPendingRead()
{
	CMsg* pMsg;
	do
	{
		pMsg = ReceiveMsg();

		if( pMsg->m_bClose )
		{
			delete m_pArchiveIn;
			m_pArchiveIn = NULL;
			delete m_pArchiveOut;
			m_pArchiveOut = NULL;
			delete m_pFile;
			m_pFile = NULL;
			delete m_pSocket;
			m_pSocket = NULL;

			m_bConnection = FALSE;
			CString strPort;
			strPort.Format( "%d", m_port );
			CString text;
			text += (CString)"Server (" + m_address + ":" + strPort
					+ ") has stopped services...\n";
			Message( text );
			return ;
		}

		switch( pMsg->m_cmdID )
		{
		case LOGON:
			{
				m_type = (DWORD)pMsg->m_nCount;
				if( !m_type )
				{
					AfxMessageBox( "用户不存在或密码错误!" );
					Logon();
				}
				else
					m_bConnection = TRUE;
			}
			break;
		case FILEBAR_INIT:
			{
				CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
				POSITION pos = pMsg->m_msgList.GetHeadPosition();

				while( pos )
				{
					CString tname = pMsg->m_msgList.GetNext( pos );
					pMainFrm->m_FileBar.insert_tree( tname, pMainFrm->m_FileBar.hitem0 );
				}
				pMainFrm->m_FileBar.ExpandTree();
			}
			break;
		case FILEBAR_EXPAND:
			{
				HTREEITEM hItem = m_treeList.RemoveHead();
				CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();

				POSITION pos = pMsg->m_msgList.GetHeadPosition();
				for( int i = 0; i < pMsg->m_nCount; i++ )
				{
					CString attr;
					attr = pMsg->m_msgList.GetNext( pos );

					pMainFrm->m_FileBar.InsertAttr( attr, hItem );
				}

				while( pos )
				{
					CString index;
					index = pMsg->m_msgList.GetNext( pos );

					pMainFrm->m_FileBar.InsertIndex( index, hItem );
				}

				pMainFrm->m_FileBar.m_TreeCtrl.Expand( hItem, TVE_EXPAND );
			}
			break;
		case FAILED:
			{
				CClientDoc* pDoc = ( CClientDoc* )m_ClientDocList.RemoveHead();
				POSITION pos = pDoc->GetFirstViewPosition();
				CClientView* pView = ( CClientView* )pDoc->GetNextView( pos );
				ASSERT_VALID( pView );

⌨️ 快捷键说明

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