TestDlg.cpp
上传用户:yunnanyeer
上传日期:2007-01-03
资源大小:86k
文件大小:5k
源码类别:

数据库编程

开发平台:

Visual C++

  1. // TestDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "Test.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #include <QueryDef.h>
  12. USE_QUERYDEF
  13. #include "TestDlg.h"
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CListCtrlDB data set bound ListControl class
  16. BEGIN_MESSAGE_MAP(CListCtrlDB, CListCtrl)
  17. END_MESSAGE_MAP()
  18. CListCtrlDB::CListCtrlDB()
  19. {
  20. m_pQset   = NULL;
  21. m_nCols = -1; // all columns from querydef visible
  22. m_qDefEvt = NULL;
  23. }
  24. CListCtrlDB::~CListCtrlDB()
  25. {
  26. }
  27. int CListCtrlDB::CalcColumnWidth(const CQueryCol& col)
  28. {
  29. CString outstr;
  30. int cx;
  31. int nChar;
  32. CClientDC dc(this);
  33. TEXTMETRIC tm;
  34. nChar = col.Precision();
  35. dc.GetTextMetrics(&tm);
  36. cx = static_cast<int>(static_cast<double>(tm.tmAveCharWidth * nChar) * (1. + 2./nChar));
  37. return cx;
  38. }
  39. BOOL CListCtrlDB::Bind(CQueryDef& qDef,int nCol)
  40. {
  41. if (m_pQset)
  42. m_pQset->Unadvise(m_qDefEvt);
  43. m_pQset = &qDef;
  44. m_qDefEvt = qDef.Advise(static_cast<IQueryDefEventSink*>(this));
  45. m_nCols = nCol;
  46. return m_qDefEvt != NULL;
  47. }
  48. void CListCtrlDB::SetAspect(int nCol)
  49. {
  50. LVCOLUMN lvCol;
  51. int maxCol = (nCol == -1)? m_pQset->GetODBCFieldCount() : nCol;
  52. lvCol.mask = LVCF_FMT|LVCF_TEXT|LVCF_WIDTH;
  53. for (int i = 0; i < maxCol; ++i)
  54. {
  55. switch (m_pQset->Column(i).SQLType())
  56. {
  57. case SQL_NUMERIC:
  58. case SQL_DECIMAL:
  59. case SQL_BIGINT:
  60. case SQL_TINYINT:
  61. case SQL_SMALLINT:
  62. case SQL_INTEGER:
  63. case SQL_REAL:
  64. case SQL_FLOAT:
  65. case SQL_DOUBLE:
  66. lvCol.fmt  = LVCFMT_RIGHT;
  67. break;
  68. default:
  69. lvCol.fmt  = LVCFMT_LEFT;
  70. }
  71. lvCol.cx = CalcColumnWidth(m_pQset->Column(i));
  72. lvCol.pszText = const_cast<char*>(m_pQset->Column(i).Name());
  73. InsertColumn(i,&lvCol);
  74. }
  75. m_nCols = maxCol;
  76. }
  77. LPARAM CListCtrlDB::RSNotifyOpen(EVNHANDLE)
  78. {
  79. SetAspect();
  80. return 0;
  81. }
  82. LPARAM CListCtrlDB::RSNotifyClose(EVNHANDLE)
  83. {
  84. if (GetSafeHwnd())
  85. {
  86. for (int i = 0; i < m_nCols; ++i)
  87. DeleteColumn(0);
  88. DeleteAllItems();
  89. }
  90. m_pQset   = NULL;
  91. m_qDefEvt = NULL;
  92. m_nCols   = -1;
  93. return 0;
  94. }
  95. LPARAM CListCtrlDB::RSNotifyFormatChanged(EVNHANDLE,BYTE nFormat)
  96. {
  97. Beep(600,1000);
  98. return 0;
  99. }
  100. LPARAM CListCtrlDB::RSNotifyMove(EVNHANDLE)
  101. {
  102. return 0;
  103. }
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CTestDlg dialog
  106. extern CTestApp theApp;
  107. CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
  108. : CDialog(CTestDlg::IDD, pParent), m_qDef(&theApp.db)
  109. {
  110. //{{AFX_DATA_INIT(CTestDlg)
  111. m_sql = "select * from authors";
  112. //}}AFX_DATA_INIT
  113. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  114. }
  115. void CTestDlg::DoDataExchange(CDataExchange* pDX)
  116. {
  117. CDialog::DoDataExchange(pDX);
  118. //{{AFX_DATA_MAP(CTestDlg)
  119. DDX_Control(pDX, IDC_RESULTS, m_result);
  120. DDX_Text(pDX, IDC_SQL, m_sql);
  121. //}}AFX_DATA_MAP
  122. }
  123. BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
  124. //{{AFX_MSG_MAP(CTestDlg)
  125. ON_WM_PAINT()
  126. ON_WM_QUERYDRAGICON()
  127. //}}AFX_MSG_MAP
  128. END_MESSAGE_MAP()
  129. /////////////////////////////////////////////////////////////////////////////
  130. // CTestDlg message handlers
  131. BOOL CTestDlg::OnInitDialog()
  132. {
  133. CDialog::OnInitDialog();
  134. m_result.SetExtendedStyle(m_result.GetExtendedStyle() | LVS_EX_FULLROWSELECT|LVS_EX_INFOTIP|LVS_EX_HEADERDRAGDROP);
  135. m_result.Bind(m_qDef);
  136. SetIcon(m_hIcon, TRUE); // Set big icon
  137. SetIcon(m_hIcon, FALSE); // Set small icon
  138. return TRUE;  // return TRUE  unless you set the focus to a control
  139. }
  140. // If you add a minimize button to your dialog, you will need the code below
  141. //  to draw the icon.  For MFC applications using the document/view model,
  142. //  this is automatically done for you by the framework.
  143. void CTestDlg::OnPaint() 
  144. {
  145. if (IsIconic())
  146. {
  147. CPaintDC dc(this); // device context for painting
  148. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  149. // Center icon in client rectangle
  150. int cxIcon = GetSystemMetrics(SM_CXICON);
  151. int cyIcon = GetSystemMetrics(SM_CYICON);
  152. CRect rect;
  153. GetClientRect(&rect);
  154. int x = (rect.Width() - cxIcon + 1) / 2;
  155. int y = (rect.Height() - cyIcon + 1) / 2;
  156. // Draw the icon
  157. dc.DrawIcon(x, y, m_hIcon);
  158. }
  159. else
  160. {
  161. CDialog::OnPaint();
  162. }
  163. }
  164. HCURSOR CTestDlg::OnQueryDragIcon()
  165. {
  166. return (HCURSOR) m_hIcon;
  167. }
  168. void CTestDlg::OnOK() 
  169. {
  170. UpdateData();
  171. if (m_qDef.IsOpen())
  172. m_qDef.Close();
  173. m_result.Bind(m_qDef);
  174. try
  175. {
  176. int nRow = 0;
  177. m_qDef.Open(CRecordset::forwardOnly,m_sql,CRecordset::readOnly);
  178. while (!m_qDef.IsEOF())
  179. {
  180. LVITEM lv;
  181. CString sqlValue;
  182. lv.mask = LVIF_TEXT;
  183. lv.iItem = nRow++;
  184. for (int i = 0; i < m_qDef.GetODBCFieldCount(); ++i)
  185. {
  186. if (m_qDef[i] != SQL_NULL)
  187. sqlValue = m_qDef[i];
  188. else
  189. sqlValue = "<NULL>";
  190. lv.iSubItem = i;
  191. lv.pszText = sqlValue.GetBuffer(sqlValue.GetLength());
  192. if (i == 0)
  193. m_result.InsertItem(&lv);
  194. else
  195. m_result.SetItem(&lv);
  196. }
  197. m_qDef.MoveNext();
  198. }
  199. }
  200. catch (CDBException* e)
  201. {
  202. AfxMessageBox(e->m_strError);
  203. e->Delete();
  204. }
  205. }