📄 adodbdlg.cpp
字号:
// AdoDBDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AdoDB.h"
#include "AdoDBDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CAdoDBDlg dialog
CAdoDBDlg::CAdoDBDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAdoDBDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAdoDBDlg)
// 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(IDI_ICON1);
}
void CAdoDBDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAdoDBDlg)
DDX_Control(pDX, IDC_EQUAL, m_ctlSearchEqual);
DDX_Control(pDX, IDC_SERACH_VAR, m_ctlSearchVar);
DDX_Control(pDX, IDC_SEARCH_VALUE, m_ctlSearchValue);
DDX_Control(pDX, IDC_LIST, m_ctlList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAdoDBDlg, CDialog)
//{{AFX_MSG_MAP(CAdoDBDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_OPEN, OnOpen)
ON_BN_CLICKED(IDC_COLOR, OnColor)
ON_BN_CLICKED(IDC_NEW, OnNew)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_Modify, OnModify)
ON_BN_CLICKED(IDC_ALL_RECORD, OnAllRecord)
ON_BN_CLICKED(IDC_SEARCH, OnSearch)
ON_BN_CLICKED(IDC_COLOR_TEXT, OnColorText)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAdoDBDlg message handlers
BOOL CAdoDBDlg::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
::AfxOleInit();
m_listboxFont.CreatePointFont(130,_T("黑体"));
m_ctlList.SetFont(&m_listboxFont);
m_ctlList.SetTextColor(RGB(0,0,255));
CRect rc;
m_ctlList.GetClientRect(&rc);
m_ctlList.InsertColumn(0,_T("Name"),LVCFMT_LEFT,80);
m_ctlList.InsertColumn(1,_T("Tel-Number"),LVCFMT_LEFT,120);
m_ctlList.InsertColumn(2,_T("Time"),LVCFMT_LEFT,180);
m_ctlList.InsertColumn(3,_T("Message"),LVCFMT_LEFT,rc.Width()-380);
m_ctlList.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP|LVS_EX_FULLROWSELECT);
m_pDBManager=new CDBManager(_Access);
m_ctlSearchVar.SetCurSel(0);
m_ctlSearchEqual.SetCurSel(1);
return TRUE; // return TRUE unless you set the focus to a control
}
void CAdoDBDlg::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 CAdoDBDlg::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 CAdoDBDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAdoDBDlg::OnExit()
{
// TODO: Add your control notification handler code here
OnCancel();
}
void CAdoDBDlg::OnOpen()
{
// TODO: Add your control notification handler code here
StConnPar stPar;
sprintf(stPar.szFilePath,_T("D:\\client_manager.mdb"));
if(m_pDBManager->Open(&stPar))
{
TalkInfoSet data_set;
if(m_pDBManager->GetTalkInformations(data_set))
{
ShowDataSet(data_set);
}
else
{
CString strMsg=_T("连接数据库已连接,获取数据失败,原因:\n");
strMsg+=m_pDBManager->GetLastError();
AfxMessageBox(strMsg);
}
}
else
{
CString strMsg=_T("连接数据库失败,原因:\n");
strMsg+=m_pDBManager->GetLastError();
AfxMessageBox(strMsg);
}
}
void CAdoDBDlg::ShowDataSet(TalkInfoSet& ds)
{
m_ctlList.DeleteAllItems();
int nCount=ds.GetSize();
for (int i=0;i<nCount;i++)
{
StClientTalkInfo& st=ds.GetAt(i);
int index = m_ctlList.InsertItem(0,st.szClientName);
m_ctlList.SetItemText(index,1,st.szPhoneNumber);
m_ctlList.SetItemText(index,2,st.dtTime.Format());
m_ctlList.SetItemText(index,3,st.szMessage);
m_ctlList.SetItemData(index,st.lID);
}
}
void CAdoDBDlg::OnCancel()
{
m_pDBManager->Close();
delete m_pDBManager;
m_pDBManager=0;
CDialog::OnCancel();
}
void CAdoDBDlg::OnColor()
{
CColorDialog dlg;
if(dlg.DoModal()==IDOK)
{
COLORREF color=dlg.GetColor();
m_ctlList.SetBkColor(color);
m_ctlList.SetTextBkColor(color);
m_ctlList.Invalidate();
}
}
void CAdoDBDlg::OnNew()
{
//delete &m_dlgNewModify;
if(!IsWindow(m_dlgNewModify.GetSafeHwnd()))
{
RECT rc,rc2;
m_dlgNewModify.m_pDBManager=m_pDBManager;
StClientTalkInfo st;
m_dlgNewModify.m_stRecord=st;
m_dlgNewModify.Create(CNewModifyDlg::IDD,this);
this->GetWindowRect(&rc);
m_dlgNewModify.GetWindowRect(&rc2);
rc.top=rc.bottom;
rc.bottom+=(rc2.bottom-rc2.top);
m_dlgNewModify.MoveWindow(&rc);
m_dlgNewModify.ShowWindow(SW_NORMAL);
}
else
{
AfxMessageBox(_T("当前已有[修改\\插入]数据操作未处理!"));
}
}
BOOL CAdoDBDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::PreTranslateMessage(pMsg);
}
void CAdoDBDlg::OnDelete()
{
int index=m_ctlList.GetSelectionMark();
if (index!=CB_ERR)
{
CString strFormat=_T("确定删除该记录?\n----------------------------\nID:%s\nName:%s\nTime:%s\nMessaage:%s");
CString strMsg;
strMsg.Format(strFormat,m_ctlList.GetItemText(index,0),
m_ctlList.GetItemText(index,1),
m_ctlList.GetItemText(index,2),
m_ctlList.GetItemText(index,3));
LONG lID=m_ctlList.GetItemData(index);
if(IDOK==AfxMessageBox(strMsg,MB_OKCANCEL|MB_ICONWARNING|MB_DEFBUTTON2))
{
if(m_pDBManager->DeleteRecord(lID))
{
m_ctlList.DeleteItem(index);
}
else
{
CString strMsg=_T("连接数据库失败,原因:\n");
strMsg+=m_pDBManager->GetLastError();
AfxMessageBox(strMsg);
}
}
}
}
void CAdoDBDlg::OnModify()
{
int index=m_ctlList.GetSelectionMark();
if (index!=CB_ERR)
{
LONG lID=m_ctlList.GetItemData(index);
CString strSQLCmd;
strSQLCmd.Format(_T("select * from talk_info where id = %d"),lID);
TalkInfoSet data_set;
if(m_pDBManager->GetTalkInformations(data_set,strSQLCmd))
{
if(data_set.GetSize()>0)
{
StClientTalkInfo& st=data_set.GetAt(0);
RECT rc,rc2;
if(!IsWindow(m_dlgNewModify.GetSafeHwnd()))
{
m_dlgNewModify.m_pDBManager=m_pDBManager;
m_dlgNewModify.m_stRecord=st;
m_dlgNewModify.Create(CNewModifyDlg::IDD,this);
this->GetWindowRect(&rc);
m_dlgNewModify.GetWindowRect(&rc2);
rc.top=rc.bottom;
rc.bottom+=(rc2.bottom-rc2.top);
m_dlgNewModify.MoveWindow(&rc);
m_dlgNewModify.ShowWindow(SW_NORMAL);
}
else
{
AfxMessageBox(_T("当前已有[修改\\插入]数据操作未处理!"));
}
}
}
else
{
CString strMsg=_T("操作失败,原因:\n");
strMsg+=m_pDBManager->GetLastError();
AfxMessageBox(strMsg);
}
}
}
void CAdoDBDlg::OnAllRecord()
{
TalkInfoSet data_set;
if(m_pDBManager->GetTalkInformations(data_set))
{
ShowDataSet(data_set);
}
else
{
CString strMsg=_T("获取数据失败,原因:\n");
strMsg+=m_pDBManager->GetLastError();
AfxMessageBox(strMsg);
}
}
void CAdoDBDlg::OnSearch()
{
CString strFormat=_T("select * from talk_info where %s %s'%s'");
CString strSQLCmd;
CString strVar;
CString strEqual;
CString strValue;
int index=m_ctlSearchVar.GetCurSel();
if (index==0)
{
strVar=_T("message");
}
else if (index==1)
{
strVar=_T("client_name");
}
else
{
strVar=_T("phone_number");
}
m_ctlSearchValue.GetWindowText(strValue);
index=m_ctlSearchEqual.GetCurSel();
if (index==0)
{
strSQLCmd.Format(_T("select * from talk_info where %s ='%s'"),
strVar,strValue);
}
else
{
strSQLCmd.Format(_T("select * from talk_info where %s like'%%%s%%'"),
strVar,strValue);
}
TalkInfoSet data_set;
if(m_pDBManager->GetTalkInformations(data_set,strSQLCmd))
{
ShowDataSet(data_set);
}
else
{
CString strMsg=_T("查询数据失败,原因:\n");
strMsg+=m_pDBManager->GetLastError();
AfxMessageBox(strMsg);
}
}
void CAdoDBDlg::OnColorText()
{
CColorDialog dlg;
if(dlg.DoModal()==IDOK)
{
COLORREF color=dlg.GetColor();
m_ctlList.SetTextColor(color);
m_ctlList.Invalidate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -