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

📄 12321dlg.cpp

📁 小巧阅读器: MFC+ACCESS 简单的查询,以滚动形式显示文字.需要自己向数据库中加入数据,以关键词查询内容.
💻 CPP
字号:
// 12321Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "12321.h"
#include "12321Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy12321Dlg dialog
static int i=0;
#define WM_SHOWTASK WM_USER+123

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

//	this->m_font.CreatePointFont(100,"宋体");
}

void CMy12321Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy12321Dlg)
	DDX_Control(pDX, IDC_COMBO2, m_cob);
	DDX_Control(pDX, IDC_BUTTON1, m_stop);
	DDX_Control(pDX, IDOK, m_start);
	DDX_Control(pDX, IDC_EDIT1, m_edit);
	DDX_Text(pDX, IDC_EDIT1, m_str);
	DDX_CBString(pDX, IDC_COMBO2, m_keyword);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy12321Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy12321Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDOK, OnSelect)
	ON_BN_CLICKED(IDC_BUTTON1, OnStop)
	ON_EN_HSCROLL(IDC_EDIT1, OnHscrollEdit1)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON5, OnExit)
	ON_BN_CLICKED(IDC_BUTTON3, OnMin)
	ON_MESSAGE(WM_SHOWTASK,OnShowTask)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy12321Dlg message handlers

BOOL CMy12321Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	::SetWindowPos(this->m_hWnd,HWND_TOPMOST,0,0,0,0,//最前端显示
		SWP_NOMOVE|SWP_NOREDRAW|SWP_NOSIZE);

	m_start.SetIcon(AfxGetApp()->LoadIcon(IDI_START));
	m_stop.SetIcon(AfxGetApp()->LoadIcon(IDI_STOP));

	AfxOleInit();///初始化COM库
	////////////连接数据库//////////////
	HRESULT hr;
	try
	{
	hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
	if(SUCCEEDED(hr))
	{
	hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",adModeUnknown);///连接数据库
	///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;  }
	}
	}
	catch(_com_error e)///捕捉异常
	{
		CString errormessage;
		errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
		AfxMessageBox(errormessage);///显示错误信息
		return FALSE;
	} 
	//取得所有关键字
	_variant_t vTest;
    CString a;
	try
	{
		m_pRecordset.CreateInstance("ADODB.Recordset");
		m_pRecordset->Open((_variant_t)("SELECT 关键词 FROM test"),_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);

		while(!m_pRecordset->adoEOF)
		{
			vTest=m_pRecordset->GetCollect("关键词");
			a=(char*)(_bstr_t)vTest;
			this->m_cob.AddString(a);
			//this->UpdateData(false);
			m_pRecordset->MoveNext();
		}
		m_pRecordset->Close();
		
	}
	catch(_com_error e)///捕捉异常
	{
		AfxMessageBox("读取数据库失败!");///显示错误信息
		m_pRecordset->Close();
	}

	SetTimer(11001,timelength,NULL);
	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 CMy12321Dlg::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 CMy12321Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMy12321Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	m_edit.SendMessage(WM_HSCROLL,MAKELONG(SB_THUMBPOSITION,i+=3),0);
	CDialog::OnTimer(nIDEvent);
}


void CMy12321Dlg::OnSelect() //响应查询事件
{
	// TODO: Add your control notification handler code here
	this->SetTimer(11001,timelength,NULL);
	this->UpdateData();
	_variant_t vTest;
	try
	{
		m_pRecordset.CreateInstance("ADODB.Recordset");
		m_pRecordset->Open((_variant_t)("SELECT * FROM test where 关键词 like '%"+m_keyword+"%'"),_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);

		while(!m_pRecordset->adoEOF)
		{
			vTest=m_pRecordset->GetCollect("内容");
			m_str=(char*)(_bstr_t)vTest;
			m_str.Replace("\r\n"," ");
			if(m_str.GetLength()>=1024)
			{
				m_temp=m_str.Right(m_str.GetLength()-1024);
				m_str=m_str.Left(1024);
			}
			this->UpdateData(false);			
			m_pRecordset->MoveNext();
		}
		m_pRecordset->Close();
		
	}
	catch(_com_error e)///捕捉异常
	{
		AfxMessageBox("读取数据库失败!");///显示错误信息
		m_pRecordset->Close();
	}

   

	this->UpdateData(false);
	m_edit.SendMessage(WM_HSCROLL,MAKELONG(SB_THUMBPOSITION,i=0),0);
}

void CMy12321Dlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	static bool one=true;
	if(one)
	{
		this->KillTimer(11001);
	 one=false;
	}
	else 
	{
		one =true;
		this->SetTimer(11001,timelength,NULL);
	}
	
}



void CMy12321Dlg::OnHscrollEdit1() //发送与时间的终止
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_SCROLL flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	int pos,max;
	pos=this->m_edit.GetScrollPos(SB_HORZ);
	max=m_edit.GetScrollLimit(SB_HORZ);
	i=pos;
	if(pos==max)
	{
		int n=m_temp.GetLength();
		if(n<=1)
		{
			this->KillTimer(11001);
			return;
		}
		m_str="     以下是换行         :";
		m_edit.SetSel(0,1024);
		m_edit.Clear();
		if(m_temp.GetLength()>=1000)
		{
				m_str+=m_temp.Left(1000);
				m_temp=m_temp.Right(m_temp.GetLength()-1000);
		}
		else
		{
			m_str+=m_temp;
			m_temp="";
		}
		pos=0;
		i=0;
		this->UpdateData(false);
	}
	m_edit.SendMessage(WM_HSCROLL,MAKELONG(SB_THUMBPOSITION,pos),0);
	this->m_edit.UpdateData(false);

}

HBRUSH CMy12321Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC,pWnd, nCtlColor);
    // TODO: Change any attributes of theDC here

	if(pWnd->GetDlgCtrlID()== IDC_EDIT1) 
		{
            pDC->SetTextColor(RGB(0,0,250));
			pDC->SelectObject(&m_font);
			pDC->SetBkColor(RGB(200,200,200));
            HBRUSH B = CreateSolidBrush(RGB(200,200,200));
            return (HBRUSH) B;
		}
		return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

}

void CMy12321Dlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	this->PostMessage(WM_CLOSE);
}

void CMy12321Dlg::OnMin() 
{
	// TODO: Add your control notification handler code here
	this->KillTimer(11001);
	NOTIFYICONDATA nid; 
	nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA); 
	nid.hWnd=this->m_hWnd; 
	nid.uID=IDR_MAINFRAME; 
	nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ; 
	nid.uCallbackMessage=WM_SHOWTASK;//自定义的消息名称 
	nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME)); 
	strcpy(nid.szTip,"自制微型阅读器");//信息提示条
	Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标 
	ShowWindow(SW_HIDE);//隐藏主窗口 
}


void CMy12321Dlg::OnShowTask(WPARAM wParam, LPARAM lParam)
{
//wParam接收的是图标的ID,而lParam接收的是鼠标的行为 
	if(wParam!=IDR_MAINFRAME) 
	return  ; 
	switch(lParam) 
	{ 
	case WM_RBUTTONUP://右键起来时弹出快捷菜单,这里只有一个“关闭” 
		{ 
			
			LPPOINT lpoint=new tagPOINT; 
			::GetCursorPos(lpoint);//得到鼠标位置 
			CMenu menu; 
			menu.CreatePopupMenu();//声明一个弹出式菜单 
			//增加菜单项“关闭”,点击则发送消息WM_DESTROY给主窗口(已 
			//隐藏),将程序结束。 
			menu.AppendMenu(MF_STRING,WM_DESTROY,"close!"); 
			//确定弹出式菜单的位置 
			menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this); 
			//资源回收 
			HMENU hmenu=menu.Detach(); 
			menu.DestroyMenu(); 
			delete lpoint; 
		} 
		break; 
	case WM_LBUTTONDOWN://单击左键的处理 
		{ 
			this->ShowWindow(SW_SHOW);//简单的显示主窗口完事儿 
		} 
		break; 
	} 
	return  ;   	
	
}

void CMy12321Dlg::OnLButtonDown(UINT nFlags, CPoint point) //发送HTCLIENT移动消息
{
	// TODO: Add your message handler code here and/or call default
	SendMessage(WM_SYSCOMMAND,SC_MOVE|HTCLIENT,0);	
	CDialog::OnLButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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