adoquerydlg.h

来自「Visual C++高级编程及其项目应用开发(含源代码)」· C头文件 代码 · 共 429 行

H
429
字号
// adoQueryDlg.h : Declaration of the CadoQueryDlg

#ifndef __ADOQUERYDLG_H_
#define __ADOQUERYDLG_H_

#include "resource.h"       // main symbols
#include <atlhost.h>

#include <string>


using namespace std;
/////////////////////////////////////////////////////////////////////////////
// CadoQueryDlg
class CadoQueryDlg : 
	public CAxDialogImpl<CadoQueryDlg>
{
public:
	CadoQueryDlg()
	{
		m_QuerySql = L"";
	}
	CadoQueryDlg(_RecordsetPtr	pRs)
	{
		m_QuerySql = L"";
		m_pRecordset = pRs;
	}

	~CadoQueryDlg()
	{
	}

	enum { IDD = IDD_ADOQUERYDLG };

BEGIN_MSG_MAP(CadoQueryDlg)
	MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
	COMMAND_ID_HANDLER(IDOK, OnOK)
	COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
	COMMAND_HANDLER(IDC_COMBO_NAME, CBN_KILLFOCUS, OnKillfocusCombo_name)
	COMMAND_HANDLER(IDC_BTN_ADD, BN_CLICKED, OnClickedBtn_add)
	COMMAND_HANDLER(IDC_CHECK_MUTI, BN_CLICKED, OnClickedCheck_muti)
	COMMAND_HANDLER(IDC_BTN_OK, BN_CLICKED, OnClickedBtn_ok)
	COMMAND_HANDLER(IDC_BTN_CANCEL, BN_CLICKED, OnClickedBtn_cancel)
	COMMAND_HANDLER(IDC_BTN_CLEAR, BN_CLICKED, OnClickedBtn_clear)
	COMMAND_HANDLER(IDC_BTN_MODIFY, BN_CLICKED, OnClickedBtn_modify)
END_MSG_MAP()
// Handler prototypes:
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

public:
	// 定义成员变量
	CWindow		m_edtResult;

	CWindow		m_cmbName;
	CWindow		m_cmbOperator;
	CWindow		m_cmbValue;
	
	CWindow		m_chkMuti;
	CWindow		m_rdiAnd;
	CWindow		m_rdiOr;

	_RecordsetPtr	m_pRecordset;
	_bstr_t		m_QuerySql ;
	//IDispatch*		m_pDisp;
	

	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		HICON  hIcon = AfxGetApp()->LoadIcon(IDI_DLG);
		if(hIcon != NULL)
		{
			SetIcon(hIcon, TRUE);
		}

		// 设置控件变量
		m_edtResult.Attach(GetDlgItem(IDC_RICHEDIT_RESULT));
		m_cmbName.Attach(GetDlgItem(IDC_COMBO_NAME));
		m_cmbOperator.Attach(GetDlgItem(IDC_COMBO_OPERATOR));
		m_cmbValue.Attach(GetDlgItem(IDC_COMBO_VALUE));
		
		m_chkMuti.Attach(GetDlgItem(IDC_CHECK_MUTI));
		m_rdiAnd.Attach(GetDlgItem(IDC_RADIO_AND));
		m_rdiOr.Attach(GetDlgItem(IDC_RADIO_OR));

		// 开始设置初始值
		long nFld = 0;
		FieldsPtr pFlds;
		FieldPtr  pFld;
		//string	  strFld;
		TCHAR	 *strFld; // = new TCHAR[256];

		if(m_pRecordset->GetadoEOF() && m_pRecordset->GetBOF())
		{
		}
		else
		{
			nFld = m_pRecordset->Fields->GetCount();
			pFlds =	m_pRecordset->GetFields();
			
			for(long i =0; i < nFld; i++)
			{
				pFld = pFlds->GetItem(i);
				strFld = pFld->GetName();
				m_cmbName.SendMessage(CB_ADDSTRING, 0, (LPARAM)strFld );
			}
		}

		m_cmbOperator.SetFocus();
		CenterWindow();
		// 使得窗体总在最前面
		//BringWindowToTop();
		//SetWindowPos(::GetActiveWindow(), 0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
		//SetWindowPos(NULL, 0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
		SetWindowPos(HWND_TOPMOST, 0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);

		return 1;  // Let the system set the focus
	}

	LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		//EndDialog(wID);
		return 0;
	}

	LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		//EndDialog(wID);
		return 0;
	}

	
	LRESULT OnKillfocusCombo_name(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		// TODO : Add Code for control notification handler.		long nCount;
		nCount = m_cmbOperator.SendMessage(CB_GETCOUNT, 0, 0);
		while(nCount > 0)
		{
			nCount = m_cmbOperator.SendMessage(CB_DELETESTRING, nCount - 1, 0);
		}

		nCount = m_cmbValue.SendMessage(CB_GETCOUNT, 0, 0);
		while(nCount > 0)
		{
			nCount = m_cmbValue.SendMessage(CB_DELETESTRING, nCount - 1, 0);
		}

		// 开始设置初始值
		long nRecCount = 0;
		FieldsPtr pFlds;
		FieldPtr  pFld;
		DataTypeEnum dataType;
		
		TCHAR	strName[256];
		string		m_strName; 
		
		if(m_pRecordset->GetadoEOF() && m_pRecordset->GetBOF())
		{
		}
		else
		{
			m_cmbName.GetWindowText(strName, 256);

			m_strName = (_bstr_t)strName;
			if(m_strName.empty())
			{
				MessageBox(_T("字段名组合框不能为空"), _T("提示"), MB_OK);
				return -1;
			}
			pFlds =	m_pRecordset->GetFields();
			pFld = pFlds->GetItem(strName);
			
			dataType = pFld->GetType();
			switch(dataType)
			{
			case adInteger:
			case adDouble:
			case adCurrency:

			case adDate:
			case adDBDate: 
			case adDBTime: 
			case adDBTimeStamp:
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T("="));
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T(">"));
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T(">="));
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T("<"));
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T("<="));
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T("<>"));
				break;
			case adChar:
			case adVarChar:
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T("="));
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T("<>"));
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T("like"));
				break;

			case adBoolean:
			default:
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T("="));
				m_cmbOperator.SendMessage(CB_ADDSTRING, 0, (LPARAM)__T("<>"));
				break;
			}

			// 下面是为字段值组合框添加选项
			TCHAR	*strValue = _T("");
			string	fldValue;

			m_pRecordset->MoveFirst();
			nRecCount = m_pRecordset->RecordCount;
			for(long i = 1; i <= nRecCount; i++)
			{
				pFld = pFlds->GetItem(strName);
				dataType = pFld->GetType();
				if(dataType == adBoolean )
				{
					int nValue;
					char cValue[32];
					nValue = abs((VARIANT_BOOL)pFld->Value);
					sprintf(cValue, "%d", nValue); // 将整数转换为字符串
					fldValue = cValue;
				}
				else
				{
					fldValue = (_bstr_t)pFld->Value;
				}

				// 注意字符的转换
				USES_CONVERSION;
				strValue = A2T(fldValue.c_str());
				
				if(CB_ERR == m_cmbValue.SendMessage(CB_FINDSTRING, -1,  (LPARAM)strValue ))
				{
					m_cmbValue.SendMessage(CB_ADDSTRING, 0, (LPARAM)strValue);
				}	
				m_pRecordset->MoveNext();
			}
		}
	
		return 0;
	}

	LRESULT OnClickedBtn_add(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		// TODO : Add Code for control notification handler.		// 查询条件字符串
		TCHAR		strName[256];
		TCHAR		strOperator[256];
		TCHAR		strValue[256];
		
		string		m_strName; 
		string		m_strOperator;
		string		m_strValue;

		// 生成查询结果时,要用到的字符串
		//TCHAR		*strResult = _T("");//[1024];
		TCHAR		strResult[1024];
		string		m_strResult;
		string		m_strSql;

		string		strTemp = "'";
		string		strSpace = " ";
		string		strLike = "%";

		m_cmbName.GetWindowText(strName, 256);
		m_strName = (_bstr_t)strName;

		m_cmbOperator.GetWindowText(strOperator, 256);
		m_strOperator = (_bstr_t)strOperator;

		m_cmbValue.GetWindowText(strValue, 256);
		m_strValue = (_bstr_t)strValue;

		// 获取结果字符串
		m_edtResult.GetWindowText(strResult, 1024);
		m_strResult = (_bstr_t)strResult;

		if(m_strName.empty() || m_strOperator.empty())
		{
			MessageBox(_T("字段名和操作符不能为空"), _T("提示"), MB_OK);
			return -1;
		}

		// 根据不同的字段的数据类型,开始生成查询语句
		FieldsPtr pFlds;
		FieldPtr  pFld;
		DataTypeEnum dataType;
		
		if(m_pRecordset->GetadoEOF() && m_pRecordset->GetBOF())
		{
		}
		else
		{
			pFlds =	m_pRecordset->GetFields();
			pFld = pFlds->GetItem(strName);
			
			dataType = pFld->GetType();
			switch(dataType)
			{
			case adInteger:
			case adDouble:
			case adCurrency:
				m_strSql = m_strName + m_strOperator + m_strValue;
				break;
			case adDate:
			case adDBDate: 
			case adDBTime: 
			case adDBTimeStamp:
		
			case adChar:
			case adVarChar:
				m_strSql = m_strName + m_strOperator + strTemp + m_strValue + strTemp;
				break;
			
			case adBoolean:
			default:
				m_strSql = m_strName + m_strOperator + m_strValue;
				break;
			}

			if(m_strOperator == "like")
			{
				m_strSql = m_strName + strSpace + m_strOperator + strSpace +
					strTemp + strLike + m_strValue + strLike + strTemp;
			}
		}

		// 下面开始检查复选框以及单选钮的状态,以便进行查询字符串的组合
		int nCheck;
		string strMuti;

		nCheck = m_chkMuti.SendMessage(BM_GETCHECK, 0, 0L);
		if(BST_CHECKED == nCheck)
		{
			nCheck = m_rdiAnd.SendMessage(BM_GETCHECK, 0, 0L);
			if(BST_CHECKED == nCheck)
			{
				strMuti = "and";
				if(!m_strResult.empty())
				{
					m_strSql = strSpace + strMuti + strSpace + m_strSql;
				}
			}
			else if(BST_UNCHECKED == nCheck)
			{
				strMuti = "or";
				if(!m_strResult.empty())
				{
					m_strSql = strSpace + strMuti + strSpace + m_strSql;
				}
			}
		}
		else if(BST_UNCHECKED == nCheck)
		{
			if(!m_strResult.empty())
			{
				m_strSql = strSpace;
			}
		}

		m_strResult = m_strResult + strSpace + m_strSql;

		// 注意字符的转换
		TCHAR *strT = _T("");
		USES_CONVERSION;
		strT = A2T(m_strResult.c_str());

		m_edtResult.SetWindowText(strT);


		return 0;
	}

	LRESULT OnClickedCheck_muti(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		// TODO : Add Code for control notification handler.		int nCheck;
		nCheck = m_chkMuti.SendMessage(BM_GETCHECK, 0, 0L);
		if(BST_CHECKED == nCheck)
		{
			m_rdiAnd.EnableWindow(TRUE);
			m_rdiOr.EnableWindow(TRUE);
			m_rdiAnd.SendMessage(BM_SETCHECK, BST_CHECKED, 0L);
			m_rdiOr.SendMessage(BM_SETCHECK, BST_UNCHECKED, 0L);
		}
		else
		{
			m_rdiAnd.EnableWindow(FALSE);
			m_rdiOr.EnableWindow(FALSE);
		}
		return 0;
	}
	
	LRESULT OnClickedBtn_ok(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		// TODO : Add Code for control notification handler.		TCHAR	strResult[1024];// = _T("");
		m_edtResult.GetWindowText(strResult, 1024);
		m_QuerySql = (_bstr_t)strResult;
		
		EndDialog(wID);
		return 0;
	}
	
	LRESULT OnClickedBtn_cancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		// TODO : Add Code for control notification handler.		EndDialog(wID);
		return 0;
	}
	
	LRESULT OnClickedBtn_clear(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		// TODO : Add Code for control notification handler.		m_edtResult.SetWindowText(_T(""));
		return 0;
	}
	LRESULT OnClickedBtn_modify(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		// TODO : Add Code for control notification handler.		m_edtResult.EnableWindow(TRUE);
		return 0;
	}
};

#endif //__ADOQUERYDLG_H_

⌨️ 快捷键说明

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