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

📄 main.cpp

📁 用测试OLE DB提供者的一个程序。能够测试出OEL DB提供者到底实现了哪些接口?很灵的。
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//-----------------------------------------------------------------------------
// Microsoft OLE DB RowsetViewer
// Copyright (C) 1994 - 1998 By Microsoft Corporation.
//
// @doc
//
// @module MAIN.CPP
//
//-----------------------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////////
// Includes
//												   
//////////////////////////////////////////////////////////////////////////////
#define DBINITCONSTANTS
#define INITGUID

#include "common.h"
#include "msdaguid.h"	//CLSID_OLEDB_ROWPOSITIONLIBRARY
#include <cguid.h>		//GUID_NULL

#include "CTable.h"
#include "Main.h"  
#include "Spy.h"		//IMallocSpy
#include "CDialog.h"	//CDialog
#include "version.h"	//ABOUT Box

#include <locale.h>		//setLocale
#include <richedit.h>	//RichEdit Control Header

#ifdef TXNJOIN
	#include <xolehlp.h> 
	#include <txdtc.h>
#endif
 

//////////////////////////////////////////////////////////////////////////////
// Defines
//
//////////////////////////////////////////////////////////////////////////////
#define OLEDBFRAMECLASS		"ROWSETMAIN"
#define OLEDBMDICLASS		"ROWSETMDI"

enum PROPICONS
{
	IMAGE_NORMAL	= 0,
	IMAGE_READONLY	= 1,
	IMAGE_INFO		= 2,
	IMAGE_ERROR		= 3,
};

enum STATEICONS
{
	STATE_NORMAL	= 0,
	STATE_CHECKED	= 1,
	STATE_UNCHECKED = 2,
};

enum ROWICONS
{
	IMAGE_NFP_BEFORE = IMAGE_ERROR+1,
	IMAGE_NFP_AFTER,
	IMAGE_ROW_DELETE,
	IMAGE_ROW_CHANGE,
};


//////////////////////////////////////////////////////////////////////////////
// Toolbar
//
//////////////////////////////////////////////////////////////////////////////
TOOLINFO tbToolInfo;
const static TBBUTTON tbButtons[] = {
{ 0, IDMENU_CONNECT,	TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{ 1, IDMENU_DISCONNECT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},

{ 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0L, 0},

{ 2, IDMENU_CREATESESSIONWINDOW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},

{ 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0L, 0},

{ 3, IDMENU_RUN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{ 4, IDMENU_EXECUTE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},

{ 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0L, 0},

{ 5, IDMENU_RESTARTPOSITION, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{ 6, IDMENU_REFRESH, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{ 7, IDMENU_GETSCHEMAROWSET, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},

{ 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0L, 0},
/*
{ 8, IDMENU_SETDATA, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{ 9, IDMENU_INSERTROW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{10, IDMENU_DELETEROWS, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
*/
{ 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0L, 0},

{11, IDMENU_UPDATE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{12, IDMENU_UNDO, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},

{ 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0L, 0},

};


//////////////////////////////////////////////////////////////////////////////
// WinMain
//
//////////////////////////////////////////////////////////////////////////////
int PASCAL WinMain(
	HINSTANCE hInstance, 
	HINSTANCE hPrevInstance, 
	LPSTR lpCmdLine, 
	int nCmdShow
	)
{
	CMainWindow* pCMainWindow = NULL;
	CMallocSpy* pCMallocSpy = NULL; 

	//Register IMallocSpy
	XTESTC(NULL, MallocSpyRegister(&pCMallocSpy)); 
	
	//Create MainWindow
	pCMainWindow = new CMainWindow(NULL, hInstance, hPrevInstance, lpCmdLine, nCmdShow);
	TESTC_(pCMainWindow != NULL, TRUE);

	//Initialize OLE
	XTESTC(NULL, CoInitialize(NULL));

	// Ensure that common control DLL is loaded
	INITCOMMONCONTROLSEX iccex;
	iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
	iccex.dwICC = ICC_LISTVIEW_CLASSES | ICC_TREEVIEW_CLASSES | ICC_TAB_CLASSES;
	TESTC_(InitCommonControlsEx(&iccex), TRUE);
	
	//Set the Locale for all C runtime functions...
	setlocale(LC_ALL, ".ACP");

	//Display MainWindow and RunApp
	TESTC_(pCMainWindow->Run(), TRUE);

CLEANUP:
	SAFE_DELETE(pCMainWindow);
	SetErrorInfo(0, NULL);
	CoUninitialize();

	XTEST(NULL, MallocSpyDump(pCMallocSpy));
	
	//Unregister
	MallocSpyUnRegister(pCMallocSpy);
	SAFE_RELEASE(pCMallocSpy);
	return TRUE;
}


////////////////////////////////////////////////////////////////
// LONG GetColumnImage
//
/////////////////////////////////////////////////////////////////
LONG GetColumnImage(DBCOLUMNINFO* pColInfo, DBSTATUS dwStatus = DBSTATUS_S_OK)
{
	//ReadOnly Column
	if(pColInfo && !(pColInfo->dwFlags & DBCOLUMNFLAGS_WRITE || pColInfo->dwFlags & DBCOLUMNFLAGS_WRITEUNKNOWN))
		return IMAGE_READONLY;

	//Otherwise determine image from Status
	switch(dwStatus)
	{
		case DBSTATUS_S_OK:
		case DBSTATUS_S_ISNULL:
		case DBSTATUS_S_DEFAULT:
			return IMAGE_NONE;

		case DBSTATUS_S_TRUNCATED:
			return IMAGE_INFO;

		default:
			return IMAGE_ERROR;
	}

	return IMAGE_NORMAL;
}


//static members
ULONG CMainWindow::m_ulChildWindows = 0;
HWND  CMainWindow::m_hWndMDIClient = NULL;
////////////////////////////////////////////////////////////////
// CMainWindow::CMainWindow
//
/////////////////////////////////////////////////////////////////
CMainWindow::CMainWindow(HWND hWnd, HINSTANCE hInst, HINSTANCE hInstPrev, CHAR* pszCmdLine, INT iCmdShow)
	: CDialogBase(hWnd, hInst)
{
	//Objects
	m_pCConnectDlg = NULL;
	m_pCOptionsDlg = NULL;
	
	//controls
	m_hWndToolbar = NULL;
	m_hWndStatusbar = NULL;
	
	//data
	m_hInstPrev = hInstPrev;
	m_pszCmdLine = pszCmdLine;
	m_iCmdShow = iCmdShow;
	m_fWarnMaxEditBuffer = TRUE;

	//Cursors
	m_hCurSizeNS = NULL;
	m_hLibRichEdit = NULL;
}


////////////////////////////////////////////////////////////////
// CMainWindow::~CMainWindow
//
/////////////////////////////////////////////////////////////////
CMainWindow::~CMainWindow()
{
	SAFE_DELETE(m_pCConnectDlg);
	SAFE_DELETE(m_pCOptionsDlg);

//	CloseHandle(m_hWndStatusbar);
	if(m_hLibRichEdit)
		FreeLibrary(m_hLibRichEdit);
}


////////////////////////////////////////////////////////////////
// CMainWindow::Display
//
/////////////////////////////////////////////////////////////////
ULONG CMainWindow::Display()
{
	//Register
	WNDCLASSEX	wc;						//class structure

	// Register window classes for the application - Main Window Class
	wc.cbSize			= sizeof(WNDCLASSEX);
	wc.style			= 0;
	wc.lpfnWndProc		= CMainWindow::MainWndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= m_hInst;
	wc.hIcon			= LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_ROWSETVIEWER));
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)(COLOR_APPWORKSPACE+1);
	wc.lpszMenuName		= MAKEINTRESOURCE(IDMENU_MAINMENU);
	wc.lpszClassName	= OLEDBFRAMECLASS;
	wc.hIconSm			= LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_ROWSETVIEWER));

	if(!RegisterClassEx(&wc))
		return FALSE;

	// register MDI Child Window Class
	wc.hIcon 			= LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_ROWSETICON));
	wc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wc.cbWndExtra		= DLGWINDOWEXTRA;
	wc.lpszClassName	= OLEDBMDICLASS;
	wc.lpfnWndProc		= CMDIChild::MDIWinProc;
	wc.hIconSm			= LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_ROWSETICON));

	if(!RegisterClassEx(&wc))
		return FALSE;

	//Create Main window, (with "this" pointer)
	if(!(m_hWnd = CreateWindowEx(
			WS_EX_CONTROLPARENT,
			OLEDBFRAMECLASS, 
			"Microsoft OLE DB RowsetViewer", 
			WS_OVERLAPPEDWINDOW, 
			CW_USEDEFAULT, CW_USEDEFAULT, 
			CW_USEDEFAULT, CW_USEDEFAULT,
			NULL, 
			NULL, 				
			m_hInst, 
			this)))
		goto CLEANUP;

	//Objects
	m_pCConnectDlg = new CConnectDlg(m_hWnd, m_hInst, this);
	m_pCOptionsDlg = new COptionsDlg(m_hWnd, m_hInst, this);

	//Make the window visible; update its client area; and return "success" 
	ShowWindow(m_hWnd, m_iCmdShow);
	UpdateWindow(m_hWnd); 

	//See if we can load the RichEdit Controls
	m_hLibRichEdit = LoadLibrary("RICHED32.DLL");
				  
CLEANUP:
	return m_hWnd ? TRUE : FALSE;      
}


////////////////////////////////////////////////////////////////
// CMainWindow::Destroy
//
/////////////////////////////////////////////////////////////////
ULONG CMainWindow::Destroy()
{
	RemoveAllChildren();
	PostQuitMessage(0);
	m_hWnd = NULL;
	return TRUE;
}
	
////////////////////////////////////////////////////////////////
// CMainWindow::Run
//
/////////////////////////////////////////////////////////////////
ULONG CMainWindow::Run()
{
	MSG		msg;

	if(!Display())
		return FALSE;

	//load accelerators
	HACCEL	hAccel = LoadAccelerators(m_hInst, MAKEINTRESOURCE(IDA_ACCELERATOR));
	
	// acquire and dispatch messages until a WM_QUIT message is received
	while(GetMessage(&msg, NULL, 0, 0))
	{           
		// check for MDI accelerators
		if(m_hWndMDIClient && TranslateMDISysAccel(m_hWndMDIClient, &msg))
			continue;
		
		// check for App accelerators
		if(m_hWnd && TranslateAccelerator(m_hWnd, hAccel, &msg))
			continue;
			
        // if the message does not need special processing, dispatch it
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return TRUE;      
}


////////////////////////////////////////////////////////////////
// CMainWindow::InitControls
//
/////////////////////////////////////////////////////////////////
BOOL CMainWindow::InitControls(HWND hWnd)
{
	m_hWnd = hWnd;

	//Create ToolBar
	m_hWndToolbar = CreateToolbarEx( 
		hWnd,					// parent
		WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_WRAPABLE | TBSTYLE_ALTDRAG | CCS_ADJUSTABLE,   // window style
		ID_TOOLBAR,             // toolbar id
		8,                      // number of bitmaps
		m_hInst,	            // mod instance
		IDB_TOOLBAR,            // resource ID for bitmap
		(LPCTBBUTTON)&tbButtons,// address of buttons
		NUMELE(tbButtons),      // number of buttons
		16,16,                  // width & height of buttons
		16,16,                  // width & height of bitmaps
		sizeof(TBBUTTON));      // structure size

    //Create ToolTips
    HWND hWndTT = (HWND)SendMessage(m_hWndToolbar, TB_GETTOOLTIPS, 0, 0);
	if(hWndTT)
    {
	    TOOLINFO lpToolInfo;
        // Fill out the TOOLINFO structure.
        lpToolInfo.cbSize = sizeof(lpToolInfo);
		// The uID is the handle of the tool (the combo box).
        lpToolInfo.uFlags = TTF_CENTERTIP;
		// the string ID in the resource
        lpToolInfo.lpszText = (LPSTR)IDB_TOOLBAR;
		// the window that gets the ToolTip messages
        lpToolInfo.hwnd = m_hWndToolbar;
		// the tool
        lpToolInfo.uId = (UINT)m_hWndToolbar;
		// the instance that owns the string resource
        lpToolInfo.hinst = m_hInst;

        // Set up the ToolTips for the combo box.
        SendMessage(hWndTT, TTM_ADDTOOL, 0, (LPARAM)&lpToolInfo);
    }
	
	//Create StatusBar
	m_hWndStatusbar = CreateStatusWindow(WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 
		"", hWnd, ID_STATUSBAR);


	//Create MDI Client Window
	CLIENTCREATESTRUCT	ccs;		//MDIclient window structure
	ccs.hWindowMenu = GetSubMenu(GetMenu(hWnd), 10/*IDMENU_WINDOW*/);
	ccs.idFirstChild = IDC_MDICHILD;

	m_hWndMDIClient = CreateWindowEx(NULL, "MDICLIENT", NULL, WS_CHILD | WS_CLIPSIBLINGS |
		WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE | CS_DBLCLKS, 
		CW_USEDEFAULT, CW_USEDEFAULT, 
		CW_USEDEFAULT, CW_USEDEFAULT,
		hWnd, (HMENU)ID_MDICLIENT, m_hInst, (LPSTR)&ccs);

	// check to see if any of the above window creation failed

⌨️ 快捷键说明

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