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

📄 chat_adodlg.cpp

📁 VC提供用Ado操作access的实例。
💻 CPP
字号:
// chat_AdoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "chat_Ado.h"
#include "chat_AdoDlg.h"
#include "LogDialog.h"
#include "_recordset1.h"
#include "Mmsystem.h"
#pragma comment(lib,"Winmm.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TIME_ELLIPSE 3000 
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CChat_AdoDlg dialog

CChat_AdoDlg::CChat_AdoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChat_AdoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChat_AdoDlg)
	m_strMsg = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CChat_AdoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChat_AdoDlg)
	DDX_Control(pDX, IDC_ADODC_CONTENT, m_ADOCtrl_Content);
	DDX_Control(pDX, IDC_ADODC_ONLINE, m_ADOCtrl_OnLine);
	DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
	DDX_Control(pDX, IDC_DATAGRID_CONTENT, m_DGridCtrl_Content);
	DDX_Control(pDX, IDC_DATAGRID_ONLINE, m_DGrid_Online);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CChat_AdoDlg, CDialog)
	//{{AFX_MSG_MAP(CChat_AdoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, OnSend)
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChat_AdoDlg message handlers

BOOL CChat_AdoDlg::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
   
	CLogDialog dlg;
	if(dlg.DoModal()!=IDOK) exit(0);
			/*设置连接字符串及数据源*/
	m_ADOCtrl_Content.SetConnectionString(dlg.m_strConnection);
	m_ADOCtrl_Content.SetRecordSource("content");
    m_ADOCtrl_Content.Refresh();
	m_ADOCtrl_OnLine.SetConnectionString(dlg.m_strConnection);
	m_ADOCtrl_OnLine.SetRecordSource("user_online");
	m_ADOCtrl_OnLine.Refresh();
			/*初始化*/
	m_strUserName=dlg.m_sUserName;
    C_Recordset1 &rst=m_ADOCtrl_Content.GetRecordset();
	m_iRecordCount=rst.GetRecordCount();
			/*打开定时器*/
    SetTimer(1,TIME_ELLIPSE,NULL);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CChat_AdoDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
    /*去除空字符*/
    m_strMsg.TrimLeft();
    m_strMsg.TrimRight();
    
	if(m_strMsg.IsEmpty()){MessageBox("不能发空消息!!");return;}
	 C_Recordset1 &rst=m_ADOCtrl_Content.GetRecordset();
	 /*添加消息*/
	try{
		variant_t var1;
		var1=long(0);
		rst.AddNew(variant_t(long(0)),variant_t(m_strUserName));
        var1=long(1);
		CTime time =CTime::GetCurrentTime();
	    CString strTime=time.Format("20%y-%m-%d");
		rst.Update(var1,variant_t(strTime));
		rst.Update(variant_t(long(2)),variant_t(m_strMsg));
		

	}
	catch(_com_error * e)
	{
		MessageBox(" ERROE!!CAN'T SEND OUT YOUR MESSAGE!!\nPLEASE CHECK THE NETWORK!!");
		return;
		
	} 
	m_strMsg.Empty();
    UpdateData(FALSE);
}

void CChat_AdoDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
   	C_Recordset1 &rst=m_ADOCtrl_OnLine.GetRecordset();
	if(rst.GetState()==1) rst.Close();
	CString m_strSource;
	m_strSource.Format("select * from user_online where name='%s'",m_strUserName);
	try{
		
		rst.Open(variant_t(m_strSource),rst.GetActiveConnection(),adOpenDynamic,adLockOptimistic,adCmdText);
		rst.Delete(1);
		rst.Requery(0);
		rst.Close();
        
		//rst.Update(variant_t(long(0)),variant_t(long(0)));
	//	rst.UpdateBatch(1);
		// rst.Close();
	}
	catch(_com_error * e)
	{
		//	MessageBox(e->Description());
		return;
		
	} 
	KillTimer(1);
	CDialog::OnCancel();
}

void CChat_AdoDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	m_ADOCtrl_OnLine.Refresh();
	m_DGrid_Online.Refresh();
	C_Recordset1 &rst=m_ADOCtrl_Content.GetRecordset();
	long count=rst.GetRecordCount();
	if(count!=m_iRecordCount) 
	{
		CString PathName;	
		PathName="\\sound\\msg";
		BOOL r=sndPlaySound(PathName,SND_ASYNC);
		if(!r)
		{
			AfxMessageBox("声卡故障!!无法播放!!");
		}
	}
	m_iRecordCount=count;
    m_ADOCtrl_Content.Refresh();
	m_DGridCtrl_Content.Refresh();	
	/*是指针末尾*/
	if(count>0)
    m_DGridCtrl_Content.SetBookmark(variant_t(long(count)));
	CDialog::OnTimer(nIDEvent);
}

BOOL CChat_AdoDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message==WM_KEYDOWN)
	{
         if(pMsg->wParam==(VK_RETURN|VK_CONTROL))
		 {
           OnSend() ;
		   return TRUE;
		 }
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CChat_AdoDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here

}

void CChat_AdoDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default

	CDialog::OnClose();
}





















⌨️ 快捷键说明

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