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

📄 daosqldlg.cpp

📁 VC++高级编程技巧与示例
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DaoSQLDlg.cpp : implementation file
//

//●

#include "stdafx.h"
#include "DaoSQL.h"
#include "DaoSQLDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CDaoSQLDlg dialog

CDaoSQLDlg::CDaoSQLDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDaoSQLDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDaoSQLDlg)
		// 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 CDaoSQLDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDaoSQLDlg)
	DDX_Control(pDX, IDC_TREE1, m_Tree1);
	DDX_Control(pDX, IDC_LIST1, m_List1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDaoSQLDlg, CDialog)
	//{{AFX_MSG_MAP(CDaoSQLDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_WM_DESTROY()
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
	ON_BN_CLICKED(IDC_EXECUTE, OnExecute)
	ON_NOTIFY(NM_SETFOCUS, IDC_TREE1, OnSetfocusTree1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDaoSQLDlg message handlers

BOOL CDaoSQLDlg::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
	
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CDaoSQLDlg::OnBrowse() 
{
	// TODO: Add your control notification handler code here
	int i;
	HTREEITEM hi;
	CFileDialog fd(TRUE,NULL,NULL,
		           OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
				   "Microsoft Access Database(*.mdb)|"
				   "*.mdb|All Files(*.*)|*.*||"
				   );

	if(fd.DoModal()==IDOK)
	{
		CString fn;
		fn=fd.GetPathName();
		if(m_Db.IsOpen())
		{
			m_Db.Close();
			m_Tree1.DeleteAllItems();
			m_hTr=0;
			(CButton *)GetDlgItem(IDC_EXECUTE)->EnableWindow(FALSE);
			(CEdit *)GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
		}
		try
		{
	 		m_Db.Open(fn);
			CString nDb;
			nDb=m_Db.GetName();
			m_hTr=m_Tree1.InsertItem(nDb);
			m_Nt=m_Db.GetTableDefCount();
			if(m_Nt!=0)
			{
				CDaoTableDefInfo tinfo;
				CDaoTableDef td(&m_Db);
				CDaoFieldInfo fi;
				for(i=0;i<m_Nt;i++)
				{
					m_Db.GetTableDefInfo(i,tinfo);
					if (tinfo.m_lAttributes&dbSystemObject)
					{
						continue;
					}
					hi=m_Tree1.InsertItem(tinfo.m_strName,m_hTr);
					//读取每一个表的字段信息:
					td.Open(tinfo.m_strName);
					short nFields = td.GetFieldCount();
					for(short i=0;i<nFields;i++)
					{
						td.GetFieldInfo(i,fi);
						m_Tree1.InsertItem(fi.m_strName,hi);
					}
					td.Close();
				}
			}
			ShowDatabaseInfo();
			(CButton *)GetDlgItem(IDC_EXECUTE)->EnableWindow(TRUE);
			(CEdit *)GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
		}
		catch(CDaoException* e)
		{
			AfxMessageBox(e->m_pErrorInfo->m_strDescription,
				          MB_ICONEXCLAMATION);
			e->Delete();
			if(m_Db.IsOpen())
			{
				m_Db.Close();
			}
			m_List1.DeleteAllItems();
			while(m_List1.DeleteColumn(0));
			(CButton *)GetDlgItem(IDC_EXECUTE)->EnableWindow(FALSE);
			(CEdit *)GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
			return;
		}
	}
}

void CDaoSQLDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	// TODO: Add your message handler code here
	if(m_Db.IsOpen())
	{
		m_Db.Close();
	}
}

CString CDaoSQLDlg::GetVariantString(COleVariant var)
{
	CString str;
	switch(var.vt)
	{
	case VT_BSTR:
		str=(LPCTSTR)var.bstrVal;
		break;
	case VT_I2:
		str.Format("%d",(int)var.iVal);
		break;
	case VT_I4:
		str.Format("%d",var.lVal);
		break;
	case VT_R4:
		str.Format("%10.5f",(double)var.fltVal);
		break;
	case VT_R8:
		str.Format("%10.5f",var.dblVal);
		break;
	case VT_CY:
		str=COleCurrency(var).Format();
		break;
	case VT_DATE:
		str=COleDateTime(var).Format();
		break;
	case VT_BOOL:
		str=(var.boolVal==0)?"FALSE":"TRUE";
		break;
	case VT_NULL:
		str="----";
		break;
	default:
		str.Format("Unknown type %d",var.vt);
		TRACE("Unknown type %d",var.vt);
	}
	return str;
}

void CDaoSQLDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	if(!m_Db.IsOpen())
	{
		return;
	}

	HTREEITEM hi=m_Tree1.GetSelectedItem();
	CString tn;
	//如果选择的是表名:
	if(m_Tree1.GetParentItem(hi)==m_hTr)
	{
		ShowTable(hi);
	}
	//如果选择的是数据库名:
    else if(hi==m_hTr)
	{
		ShowDatabaseInfo();
		return;
	}
	//如果选择的是字段名:
	else
	{
		ShowFieldInfo(hi);
		return;
	}
	*pResult = 0;
}

void CDaoSQLDlg::ShowDatabaseInfo()
{
		m_List1.SetRedraw(FALSE);
		m_List1.DeleteAllItems ();
		while(m_List1.DeleteColumn(0));

		CDaoDatabaseInfo di;
		m_Db.m_pWorkspace->GetDatabaseInfo(0,di);
	//1. 添加列:
		LV_COLUMN lc;
		lc.mask = LVCF_FMT | LVCF_WIDTH |
				  LVCF_TEXT | LVCF_SUBITEM;
		lc.fmt = LVCFMT_CENTER;
		lc.cx = 75;
		lc.iSubItem = 0;
		lc.pszText = "项目名称";
		m_List1.InsertColumn(0, &lc);
		lc.pszText = "属性";
		m_List1.InsertColumn(1, &lc);
	//2. 添加各个项目(各行):
		LVITEM Item;
		CString Info;
		Item.mask=LVIF_TEXT;
		Item.iSubItem=0;

		Item.iItem=0;
		Item.pszText=_T("数据库名称:");
		m_List1.InsertItem(&Item);

		Item.iItem=1;
		Item.pszText=_T("是否可写:");
		m_List1.InsertItem(&Item);
		Item.iItem=2;
		Item.pszText=_T("是否允许事务处理:");
		m_List1.InsertItem(&Item);

	//3. 设置各个子项的字符串:
		Info=di.m_strName;
		m_List1.SetItemText(0,1,Info);

		if(di.m_bUpdatable)
		{
			Info="可写";
		}

⌨️ 快捷键说明

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