- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
TestDlg.cpp
资源名称:querydef.zip [点击查看]
上传用户:yunnanyeer
上传日期:2007-01-03
资源大小:86k
文件大小:5k
源码类别:
数据库编程
开发平台:
Visual C++
- // TestDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "resource.h"
- #include "Test.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #include <QueryDef.h>
- USE_QUERYDEF
- #include "TestDlg.h"
- /////////////////////////////////////////////////////////////////////////////
- // CListCtrlDB data set bound ListControl class
- BEGIN_MESSAGE_MAP(CListCtrlDB, CListCtrl)
- END_MESSAGE_MAP()
- CListCtrlDB::CListCtrlDB()
- {
- m_pQset = NULL;
- m_nCols = -1; // all columns from querydef visible
- m_qDefEvt = NULL;
- }
- CListCtrlDB::~CListCtrlDB()
- {
- }
- int CListCtrlDB::CalcColumnWidth(const CQueryCol& col)
- {
- CString outstr;
- int cx;
- int nChar;
- CClientDC dc(this);
- TEXTMETRIC tm;
- nChar = col.Precision();
- dc.GetTextMetrics(&tm);
- cx = static_cast<int>(static_cast<double>(tm.tmAveCharWidth * nChar) * (1. + 2./nChar));
- return cx;
- }
- BOOL CListCtrlDB::Bind(CQueryDef& qDef,int nCol)
- {
- if (m_pQset)
- m_pQset->Unadvise(m_qDefEvt);
- m_pQset = &qDef;
- m_qDefEvt = qDef.Advise(static_cast<IQueryDefEventSink*>(this));
- m_nCols = nCol;
- return m_qDefEvt != NULL;
- }
- void CListCtrlDB::SetAspect(int nCol)
- {
- LVCOLUMN lvCol;
- int maxCol = (nCol == -1)? m_pQset->GetODBCFieldCount() : nCol;
- lvCol.mask = LVCF_FMT|LVCF_TEXT|LVCF_WIDTH;
- for (int i = 0; i < maxCol; ++i)
- {
- switch (m_pQset->Column(i).SQLType())
- {
- case SQL_NUMERIC:
- case SQL_DECIMAL:
- case SQL_BIGINT:
- case SQL_TINYINT:
- case SQL_SMALLINT:
- case SQL_INTEGER:
- case SQL_REAL:
- case SQL_FLOAT:
- case SQL_DOUBLE:
- lvCol.fmt = LVCFMT_RIGHT;
- break;
- default:
- lvCol.fmt = LVCFMT_LEFT;
- }
- lvCol.cx = CalcColumnWidth(m_pQset->Column(i));
- lvCol.pszText = const_cast<char*>(m_pQset->Column(i).Name());
- InsertColumn(i,&lvCol);
- }
- m_nCols = maxCol;
- }
- LPARAM CListCtrlDB::RSNotifyOpen(EVNHANDLE)
- {
- SetAspect();
- return 0;
- }
- LPARAM CListCtrlDB::RSNotifyClose(EVNHANDLE)
- {
- if (GetSafeHwnd())
- {
- for (int i = 0; i < m_nCols; ++i)
- DeleteColumn(0);
- DeleteAllItems();
- }
- m_pQset = NULL;
- m_qDefEvt = NULL;
- m_nCols = -1;
- return 0;
- }
- LPARAM CListCtrlDB::RSNotifyFormatChanged(EVNHANDLE,BYTE nFormat)
- {
- Beep(600,1000);
- return 0;
- }
- LPARAM CListCtrlDB::RSNotifyMove(EVNHANDLE)
- {
- return 0;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTestDlg dialog
- extern CTestApp theApp;
- CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTestDlg::IDD, pParent), m_qDef(&theApp.db)
- {
- //{{AFX_DATA_INIT(CTestDlg)
- m_sql = "select * from authors";
- //}}AFX_DATA_INIT
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CTestDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTestDlg)
- DDX_Control(pDX, IDC_RESULTS, m_result);
- DDX_Text(pDX, IDC_SQL, m_sql);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
- //{{AFX_MSG_MAP(CTestDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTestDlg message handlers
- BOOL CTestDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- m_result.SetExtendedStyle(m_result.GetExtendedStyle() | LVS_EX_FULLROWSELECT|LVS_EX_INFOTIP|LVS_EX_HEADERDRAGDROP);
- m_result.Bind(m_qDef);
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- return TRUE; // return TRUE unless you set the focus to a control
- }
- // 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 CTestDlg::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();
- }
- }
- HCURSOR CTestDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CTestDlg::OnOK()
- {
- UpdateData();
- if (m_qDef.IsOpen())
- m_qDef.Close();
- m_result.Bind(m_qDef);
- try
- {
- int nRow = 0;
- m_qDef.Open(CRecordset::forwardOnly,m_sql,CRecordset::readOnly);
- while (!m_qDef.IsEOF())
- {
- LVITEM lv;
- CString sqlValue;
- lv.mask = LVIF_TEXT;
- lv.iItem = nRow++;
- for (int i = 0; i < m_qDef.GetODBCFieldCount(); ++i)
- {
- if (m_qDef[i] != SQL_NULL)
- sqlValue = m_qDef[i];
- else
- sqlValue = "<NULL>";
- lv.iSubItem = i;
- lv.pszText = sqlValue.GetBuffer(sqlValue.GetLength());
- if (i == 0)
- m_result.InsertItem(&lv);
- else
- m_result.SetItem(&lv);
- }
- m_qDef.MoveNext();
- }
- }
- catch (CDBException* e)
- {
- AfxMessageBox(e->m_strError);
- e->Delete();
- }
- }