ImageVerifierCode 换一换
格式:DOCX , 页数:63 ,大小:35.46KB ,
资源ID:7377647      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7377647.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++编程常用模块.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++编程常用模块.docx

1、C+编程常用模块1(一):连接SQL数据库 一、连接数据库(SQL):(一)直接写在程序中1、导入动态连接库在StdAfx.h中:#import C:Program FilesCommon FilesSystemadomsado15.dll no_namespace rename(EOF,adoEOF) rename(BOF,adoBOF)2、定义Provider宏在StdAfx.h中:#define Provider Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog

2、=HRMS3、初始化a) .h文件中private: HDC m_hDC; DWORD m_CurrentRecord; _RecordsetPtr m_pRecordset; _ConnectionPtr m_pConnection;b) .cpp文件中CADOOperation:CADOOperation() try m_pConnection.CreateInstance(_uuidof(Connection); m_pConnection-Open(_bstr_t(Provider), , , adModeUnknown); catch(_com_error e) AfxMessage

3、Box(e.Description(), MB_OK); exit(0); m_pRecordset.CreateInstance(_uuidof(Recordset); m_CurrentRecord = -1;4、执行5、关闭CADOOperation:CADOOperation() m_pConnection-Close(); (二)通过login.ini文件连接void CBbbApp:IniAdo() try CString temp; char filepathMAX_PATH; GetModuleFileName(NULL,filepath,MAX_PATH); temp = t

4、heApp.ExtractFilePath(filepath);/获取可执行文件的路径,也就是程序运行的.exe文件所在路径 /运行后,temp的值为程序文件.exe 所在路径 CString strAdoConn; char temp1100; /* 在同目录下创建login.ini文件,并在其中设置数据库连接字符串,如:DatabaseConfigServer=. / . 表示本机Database=BookManageUser=saPWD=sa*/ GetPrivateProfileString(DatabaseConfig,Server,temp1,100,temp+login.ini)

5、; strserver = (TCHAR *)temp1; GetPrivateProfileString(DatabaseConfig,Database,temp1,100,temp+login.ini); strdbName = temp1; GetPrivateProfileString(DatabaseConfig,User,temp1,100,temp+login.ini); strUser = temp1; GetPrivateProfileString(DatabaseConfig,PWD,temp1,100,temp+login.ini); strPassword = temp

6、1; strAdoConn.Format(driver=SQL Server;SERVER=%s;UID=%s;PWD=%s;DATABASE=%s, strserver, strUser, strPassword, strdbName); m_pCon.CreateInstance(_uuidof(Connection); m_pCon-ConnectionString = (_bstr_t)strAdoConn; m_pCon-Open(,NULL); m_pCom.CreateInstance(ADODB.Command); m_pRs.CreateInstance(_uuidof(Re

7、cordset); m_pRs1.CreateInstance(_uuidof(Recordset); / ADOFLAG = TRUE; catch(_com_error) /ADOFLAG = FALSE; Flag = FALSE; MessageBox(0,请检查系统配置信息, 数据库连接失败,MB_OK); return; catch(.) AfxMessageBox(SYS Error); return ; C+编程常用模块(二):连接Access数据库 二、连接数据库 (Access 2000)1、在.h文件中导入动态链接库#import C:Program FilesCommo

8、n FilesSystemadomsado15.dllno_namespace rename(EOF,adoEOF)rename(BOF,adoBOF) 类定义:class ADOConn public: BOOL ExecuteSQL(_bstr_t bstrSQL); ADOConn(); virtual ADOConn(); /添加一个指向Connection对象的指针 _ConnectionPtr m_pConnection; /添加一个指向Recordset对象的指针 _RecordsetPtr m_pRecordset; _RecordsetPtr& GetRecordSet(_b

9、str_t bstrSQL); void ExitConnect(); void OnInitADOConn(); 2、建立连接void ADOConn:OnInitADOConn() /初始化OLE/COM库环境 :CoInitialize(NULL); try char filepathMAX_PATH; GetModuleFileName(NULL,filepath,MAX_PATH); CString strdb; strdb = ExtractFilePath(filepath) + sm.mdb; strdb = DBQ= + strdb; /创建connection对象 m_pC

10、onnection.CreateInstance(ADODB.Connection); m_pConnection-ConnectionTimeout=3; /设置连接字符串,两种都可以 /_bstr_t strConnect=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sm.mdb; _bstr_t strConnect=DRIVER=Microsoft Access Driver (*.mdb);uid=;pwd=sm; strConnect = strConnect + strdb; /SERVER和UID,PWD的设置根据实际情况来设置 m

11、_pConnection-Open(strConnect,adModeUnknown); /捕捉异常 catch(_com_error e) /显示错误信息 AfxMessageBox(e.Description(); 3、执行SQL语句/仅执行,无返回BOOL ADOConn:ExecuteSQL(_bstr_t bstrSQL) try /是否已连接数据库 if(m_pConnection=NULL) OnInitADOConn(); m_pConnection-Execute(bstrSQL,NULL,adCmdText); return true; catch(_com_error e

12、) e.Description(); return false; /返回记录集_RecordsetPtr& ADOConn:GetRecordSet(_bstr_t bstrSQL) try if(m_pConnection=NULL) OnInitADOConn(); m_pRecordset.CreateInstance(_uuidof(Recordset); m_pRecordset-Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); catch(_com_erro

13、r e) e.Description(); return m_pRecordset; 4、关闭连接void ADOConn:ExitConnect() /关闭记录集和连接 if(m_pRecordset!=NULL) m_pRecordset-Close(); m_pRecordset=NULL; if(m_pConnection!=NULL) m_pConnection-Close(); m_pConnection=NULL; /释放环境 :CoUninitialize();C+编程常用模块(三):窗体操作 三、窗体操作1、显示对话框窗体CDlgLogin login ; login.DoM

14、odal(); 若要处理“确定”或“取消”: int nResponse = login.DoModal();if (nResponse = IDOK) / TODO: Place code here to handle when the dialog is / dismissed with OK else if (nResponse = IDCANCEL) / TODO: Place code here to handle when the dialog is / dismissed with Cancel 2、关闭对话框窗体 EndDialog(0); 或 CDialog:OnCancel

15、(); 或CDialog:OnOK();3、退出应用程序 exit(0); 4、登录:P308void CDlgLogin:OnButton1() CString c_user,c_password; m_user.GetWindowText(c_user); m_password.GetWindowText(c_password); if (c_user.IsEmpty() | c_password.IsEmpty() MessageBox(用户名称或密码不能为空, 用户登录信息); return; CString sql; sql.Format(select * from tb_opera

16、tor where name = %s and password = %s,c_user,c_password); m_pRs-raw_Close(); m_pRs-Open(_variant_t)sql,m_pCon.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText); /if (m_pRs-RecordCount0) 或者 if (!myConn.m_pRecordset -adoEOF) Flag = true; /Flag为全局变量 user = m_pRs-GetCollect(name).bstrVal; passw

17、ord = m_pRs-GetCollect(password).bstrVal; EndDialog(0); else user = ; password = ; MessageBox(用户名或密码不正确.,提示,64); return; 在程序启动初始化部分BOOL CBbbApp:InitInstance()函数里完成窗口跳转:if (Flag =true) CBbbDlg dlg; /启动主窗体 m_pMainWnd = &dlg; dlg.DoModal(); 5、动态创建工具栏:P306方法(一):CToolBar m_toolbar;CImageList m_imagelist;

18、 m_imagelist.Create(24,24,ILC_COLOR24|ILC_MASK,1,1); m_imagelist.Add(AfxGetApp()-LoadIcon(IDI_ICON4); m_imagelist.Add(AfxGetApp()-LoadIcon(IDI_ICON11); m_imagelist.Add(AfxGetApp()-LoadIcon(IDI_ICON13); m_imagelist.Add(AfxGetApp()-LoadIcon(IDI_ICON5); m_imagelist.Add(AfxGetApp()-LoadIcon(IDI_ICON19);

19、 UINT array5; for(int i=0;iLoadIcon(IDI_ICON4); m_ImageList.Add(:AfxGetApp()-LoadIcon(IDI_ICON6); m_ImageList.Add(:AfxGetApp()-LoadIcon(IDI_ICON5); m_ImageList.Add(:AfxGetApp()-LoadIcon(IDI_ICON2); m_ImageList.Add(:AfxGetApp()-LoadIcon(IDI_ICON3); m_ToolBar.Create(WS_CHILD | WS_VISIBLE, CRect(0, 0,

20、0, 0), this, 0); m_ToolBar.SetImageList(&m_ImageList); for(int j=0; j ICONSUM; j+) tbButtonj.dwData = NULL; tbButtonj.fsState = TBSTATE_ENABLED; tbButtonj.fsStyle = (BYTE)TBSTYLE_FLAT; tbButtonj.iBitmap = j; tbButton0.idCommand = ID_Info; tbButton1.idCommand = ID_MR; tbButton2.idCommand = ID_QS; tbB

21、utton3.idCommand = ID_XS; tbButton4.idCommand = ID_HELP; m_ToolBar.AddButtons(ICONSUM, tbButton); m_ToolBar.AutoSize(); m_ToolBar.SetStyle(TBSTYLE_FLAT | CCS_TOP);6、设置菜单有效和失效有效:m_Menu.EnableMenuItem(ID_USER_REGISTER, MF_BYCOMMAND | MF_ENABLED); 失效:m_Menu.EnableMenuItem(ID_USER_REGISTER, MF_BYCOMMAND

22、 | MF_DISABLED | MF_GRAYED);7、设置状态栏CTime Time; Time = CTime:GetCurrentTime(); CString Str = Time.Format(%Y-%m-%d); UINT Array3; for(int i=0;iGetActiveView(); if (! temp-ispreview) theApp.m_pMainWnd = theApp.m_main; CFrameWnd:OnClose();11、窗体标题栏加上图标先在头文件中声明:HICON m_hIcon;在构造函数中:m_hIcon = AfxGetApp()-LoadIcon(IDI_ICON1); 在OnInitDialog()中: SetIcon(m_hIcon, TRUE); / Set big icon SetIcon(m_hIcon, FALSE); / Set small icon 在OnPaint()中: void CSmDlg:OnPaint() if (IsIconic() CPaintDC dc(this); / device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetS

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1