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

📄 mainfrm.cpp

📁 Visual C++ 实践与提高--数据库篇的源代码。很好的东西。欢迎下载。
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "ADOQry.h"

#include "MainFrm.h"

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

extern CADOQryApp theApp;
LPCTSTR g_szUntitled = _T("Untitled");
int g_nUntitledBump = 0;
/////////////////////////////////////////////////////////////////////////////
// CMDIClientWnd

CMDIClientWnd::CMDIClientWnd()
{
}

CMDIClientWnd::~CMDIClientWnd()
{
}

BEGIN_MESSAGE_MAP(CMDIClientWnd, CWnd)
	//{{AFX_MSG_MAP(CMDIClientWnd)
	ON_WM_PAINT()
	ON_WM_WINDOWPOSCHANGING()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMDIClientWnd message handlers

void CMDIClientWnd::OnPaint() 
{
	CRect rect;
	GetClientRect(&rect);
	BITMAP bm;
	m_bitmap.GetBitmap(&bm);
	int nX = rect.right - bm.bmWidth - rect.Width()/30;
	int nY = rect.bottom - bm.bmHeight - rect.Height()/20;
	CDC dcImage;
	CPaintDC dc(this);
	if(dcImage.CreateCompatibleDC(&dc))
	{
		CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap);
		//为块拷贝
		dc.BitBlt(nX, nY, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
		dcImage.SelectObject(pOldBitmap);
	}
}

void CMDIClientWnd::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos) 
{
	CWnd::OnWindowPosChanging(lpwndpos);
	if(lpwndpos != NULL && !(lpwndpos->flags & SWP_NOMOVE))
	{	 
		CWnd* pParent = GetParent();	
		if(pParent != NULL)
		{
			CRect rect;						
			GetWindowRect(&rect);				
			pParent->ScreenToClient(&rect);	
			pParent->InvalidateRect(&rect);
			PostMessage(WM_PAINT);
		}
	}
}


/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_FILE_CONNECT, OnFileConnect)
	ON_COMMAND(ID_FILE_DISCONNECT, OnFileDisconnect)
	ON_COMMAND(ID_FILE_DISCONNECT_ALL, OnFileDisconnectAll)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame():
	m_pWndCurrentChild(NULL),
	m_bCloseAfterCancel(FALSE),
	m_bDisconnectAllAfterCancel(FALSE)
{	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	//创建状态栏	
	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE(_T("Failed to create status bar\n"));
		return -1;      // fail to create
	}
	
	//设置状态栏信息
	int nIndex = 0;
	UINT nID = ID_SEPARATOR;
	UINT nStyle = -1;
	int nWidth = -1;
	m_wndStatusBar.GetPaneInfo(nIndex, nID, nStyle, nWidth);
	m_wndStatusBar.SetPaneInfo(nIndex, nID, SBPS_NORMAL | SBPS_STRETCH, nWidth);
	m_wndStatusBar.SetPaneInfo( 1, 
								ID_INDICATOR_NUM_CONNECTIONS, 
								SBPS_NORMAL, 131
								);

	//“背景绣字”实现
	if(!m_wndMDIClient.m_bitmap.LoadBitmap(IDB_BITMAP1))
		return -1;
	if(!m_wndMDIClient.SubclassWindow(m_hWndMDIClient))
	{ 
		TRACE(_T("Failed to subclass MDI client window\n"));
        return -1;                                     
    }

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

//函数功能:连结数据源
CDocument * CMainFrame::ConnectEx( LPCTSTR lpszConnectString, 
								   LPCTSTR lpszFileName, 
								   LPCTSTR lpszInitialCatalog)
{
	CWaitCursor wait;	
	m_wndStatusBar.SetPaneText(0, _T("Establishing new connection..."));

	CDocument* pDoc = NULL;
	//限定连结的最大数目
	if(m_listChildFrame.size() > 511)
	{
		CString sMsg = _T("Connection limit reached. You must close some connections ");
				sMsg +=_T("before attempting to open any more connections.");
		AfxMessageBox(sMsg, MB_ICONSTOP);
	}
	else
	{
		CString sTitle;
		//为每个连结创建一文档
		pDoc = theApp.m_pDocTemplate->CreateNewDocument();
		ASSERT_VALID(pDoc);
		if(pDoc == NULL)
			TRACE(_T("CDocTemplate::CreateNewDocument returned NULL.\n"));
		else
		{
			//为每个连结创建一子框件
			CChildFrame* pFrame = (CChildFrame*)theApp.m_pDocTemplate->
				CreateNewFrame(pDoc, NULL);
			ASSERT_VALID(pFrame);
			if(pFrame == NULL)
			{
				if(pDoc != NULL)
				{
					delete pDoc;
					pDoc = NULL;
				}
			}
			else
			{
				pDoc->m_bEmbedded = TRUE;
				if(lpszFileName != NULL)
				{
					CWaitCursor wait;
					if(pDoc->OnOpenDocument(lpszFileName))
					{
						pDoc->SetPathName(lpszFileName);
						sTitle = pDoc->GetTitle();
					}
					else
					{
						TRACE(_T("CDocument::OnOpenDocument returned FALSE.\n"));
						//打开新文档失败则删除子框架及文档/视
						pFrame->DestroyWindow(); 
						pDoc = NULL;
					}
				}
				else
				{
					if(!pDoc->OnNewDocument())
					{
						TRACE(_T("CDocument::OnNewDocument returned FALSE.\n"));
						//打开新文档失败则删除子框架及文档/视
						pFrame->DestroyWindow(); 
						pDoc = NULL;
					}
				}
				
				if(pDoc != NULL)
				{
					//连结数据源
					if(!pFrame->Connect(lpszConnectString, false, lpszInitialCatalog))
					{
						pFrame->DestroyWindow(); 
						pDoc = NULL;
					}
					else
					{
						if(sTitle.IsEmpty())
							sTitle.Format(_T("%s_%d - (New Query)"), g_szUntitled, ++g_nUntitledBump);
						pDoc->SetTitle(pFrame->m_strDataSource + _T(" - ") + sTitle);
						theApp.m_pDocTemplate->InitialUpdateFrame(pFrame, pDoc);
						pDoc->m_bEmbedded = FALSE;
						pFrame->SetActiveView(pFrame->m_pQryView);
						m_listChildFrame.push_back(pFrame);
						if(m_listChildFrame.size() == 1)
							pFrame->ShowWindow(SW_SHOWMAXIMIZED);
						m_strConnections.Format(
							_T("Total # of connections: %d"), m_listChildFrame.size()
							);
						m_wndStatusBar.SetPaneText(1, m_strConnections);
						UpdateWindow();
					}
				}
			}
		}
	}

	m_wndStatusBar.SetPaneText(0, _T(""));

	return pDoc;
}
static TCHAR BASED_CODE szSection[] = _T("Settings");
static TCHAR BASED_CODE szWindowPos[] = _T("WindowPos");
static TCHAR szFormat[] = _T("%u,%u,%d,%d,%d,%d,%d,%d,%d,%d");

static BOOL PASCAL ReadWindowPlacement(LPWINDOWPLACEMENT pwp)
{
	CWinApp* pApp = AfxGetApp();
	
	CString strBuffer = pApp->GetProfileString(szSection, szWindowPos);
	
	if(strBuffer.IsEmpty())
		return FALSE;

	WINDOWPLACEMENT wp;
	int nRead = _stscanf(strBuffer, szFormat,
		&wp.flags, &wp.showCmd,
		&wp.ptMinPosition.x, &wp.ptMinPosition.y,
		&wp.ptMaxPosition.x, &wp.ptMaxPosition.y,
		&wp.rcNormalPosition.left, &wp.rcNormalPosition.top,
		&wp.rcNormalPosition.right, &wp.rcNormalPosition.bottom);

	if(nRead != 10)
		return FALSE;

	wp.length = sizeof(wp);
	*pwp = wp;

	return TRUE;
}

static void PASCAL WriteWindowPlacement(LPWINDOWPLACEMENT pwp)
{
	TCHAR szBuffer[sizeof(_T("-32767"))*8 + sizeof(_T("65535"))*2];

	wsprintf(szBuffer, szFormat,
		pwp->flags, pwp->showCmd,
		pwp->ptMinPosition.x, pwp->ptMinPosition.y,
		pwp->ptMaxPosition.x, pwp->ptMaxPosition.y,
		pwp->rcNormalPosition.left, pwp->rcNormalPosition.top,
		pwp->rcNormalPosition.right, pwp->rcNormalPosition.bottom);
	
	AfxGetApp()->WriteProfileString(szSection, szWindowPos, szBuffer);
}
void CMainFrame::InitialShowWindow(UINT nCmdShow)
{
	WINDOWPLACEMENT wp;
	if(!ReadWindowPlacement(&wp))
		CenterWindow();
	else
	{
		if(nCmdShow != SW_SHOWNORMAL)
			wp.showCmd = nCmdShow;
		else
			nCmdShow = wp.showCmd;

		SetWindowPlacement(&wp);
	}
	
	ShowWindow(nCmdShow);
	UpdateWindow();
}
//函数功能:连结数据源
void CMainFrame::OnFileConnect() 
{
	ConnectEx();
}
//函数功能:断开到数据源的当前连接
void CMainFrame::OnFileDisconnect() 
{
	CWaitCursor wait;
	
	m_wndStatusBar.SetPaneText(0, _T("Please wait..."));
	
	CChildFrame* pChild = (CChildFrame*)MDIGetActive();
	if(pChild != NULL)
		pChild->SendMessage(WM_CLOSE);
	
	m_wndStatusBar.SetPaneText(0, _T(""));	
}
//函数功能:断开所有到数据源的连接
void CMainFrame::OnFileDisconnectAll() 
{
	CWaitCursor wait;
	
	m_wndStatusBar.SetPaneText(0, _T("Please wait..."));
	
	CChildFrame* pChild = (CChildFrame*)MDIGetActive();
	CChildFrame* pOldChild = NULL;
	m_bDisconnectAllAfterCancel = TRUE;
	do
	{
		pOldChild = pChild;
		if(pChild != NULL)
			pChild->SendMessage(WM_CLOSE);

		m_strConnections.Format(_T("Total # of connections: %d"), m_listChildFrame.size());
		m_wndStatusBar.SetPaneText(1, m_strConnections);
		
		pChild = (CChildFrame*)MDIGetActive();
		if(pChild != NULL && pChild == pOldChild && pChild->m_bCanceling)
		{
			pChild->m_bCloseMainFrameAfterCancel = m_bCloseAfterCancel;
			pChild->m_bDisconnectAllAfterCancel = m_bDisconnectAllAfterCancel;
		}

		if(pChild == pOldChild)
			break;
	}
	while(pChild != NULL);
	m_bDisconnectAllAfterCancel = FALSE;
	
	m_wndStatusBar.SetPaneText(0, _T(""));	
}

⌨️ 快捷键说明

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