C++编程常用模块.docx
- 文档编号:6246472
- 上传时间:2023-01-04
- 格式:DOCX
- 页数:31
- 大小:23.94KB
C++编程常用模块.docx
《C++编程常用模块.docx》由会员分享,可在线阅读,更多相关《C++编程常用模块.docx(31页珍藏版)》请在冰豆网上搜索。
C++编程常用模块
1
(一):
连接SQL数据库
一、连接数据库(SQL):
(一)直接写在程序中
1、导入动态连接库
在中:
#import"C:
\ProgramFiles\CommonFiles\System\ado\"no_namespace\
rename("EOF","adoEOF")rename("BOF","adoBOF")
2、定义Provider宏
在中:
#defineProvider"Provider=;IntegratedSecurity=SSPI;\
PersistSecurityInfo=False;InitialCatalog=HRMS"
3、初始化
a).h文件中
private:
HDCm_hDC;
DWORDm_CurrentRecord;
_RecordsetPtrm_pRecordset;
_ConnectionPtrm_pConnection;
b).cpp文件中
CADOOperation:
:
CADOOperation()
{
try{
(_uuidof(Connection));
m_pConnection->Open(_bstr_t(Provider),"","",adModeUnknown);
}
catch(_com_errore)
{
AfxMessageBox(),MB_OK);
exit(0);
}
(_uuidof(Recordset));
m_CurrentRecord=-1;
}
4、执行
5、关闭
CADOOperation:
:
~CADOOperation()
{
m_pConnection->Close();
}
(二)通过文件连接
voidCBbbApp:
:
IniAdo()
{
try
{
CStringtemp;
charfilepath[MAX_PATH];
GetModuleFileName(NULL,filepath,MAX_PATH);
temp=(filepath);xe文件所在路径
xe所在路径
CStringstrAdoConn;
chartemp1[100];
/*********************************************
在同目录下创建文件,并在其中设置数据库连接字符串,如:
[DatabaseConfig]
Server=.表示本机
Database=BookManage
User=sa
PWD=sa
*********************************************/
GetPrivateProfileString("DatabaseConfig","Server","",temp1,100,temp+"");
strserver=(TCHAR*)temp1;
GetPrivateProfileString("DatabaseConfig","Database","",temp1,100,temp+"");
strdbName=temp1;
GetPrivateProfileString("DatabaseConfig","User","",temp1,100,temp+"");
strUser=temp1;
GetPrivateProfileString("DatabaseConfig","PWD","",temp1,100,temp+"");
strPassword=temp1;
("driver={SQLServer};SERVER=%s;UID=%s;PWD=%s;DATABASE=%s",
strserver,strUser,strPassword,strdbName);
(_uuidof(Connection));
m_pCon->ConnectionString=(_bstr_t)strAdoConn;
m_pCon->Open("","","",NULL);
("");
(_uuidof(Recordset));
(_uuidof(Recordset));
.)
{
AfxMessageBox("SYSError");
return;
}
}
C++编程常用模块
(二):
连接Access数据库
二、连接数据库(Access2000)
1、在.h文件中导入动态链接库
#import"C:
\ProgramFiles\CommonFiles\System\ado\"no_namespace\
rename("EOF","adoEOF")rename("BOF","adoBOF")
类定义:
classADOConn
{
public:
BOOLExecuteSQL(_bstr_tbstrSQL);
ADOConn();
virtual~ADOConn();
db)};uid=;pwd=sm;";
strConnect=strConnect+strdb;
strVal;
password=m_pRs->GetCollect("password").bstrVal;
EndDialog(0);
}
else
{
user="";
password="";
MessageBox("用户名或密码不正确.","提示",64);
return;
}
}
在程序启动初始化部分BOOLCBbbApp:
:
InitInstance()函数里完成窗口跳转:
if(Flag==true)
{
CBbbDlgdlg;etButtonWidth(60,120);
().SetImageList(&m_imagelist);
(CSize(70,60),CSize(28,40));
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
/**************************原理************************
先创建索引,再在中定义控件代表的索引,如
#defineIDB_bookinfo5000
#defineIDB_booksell5001
#defineIDB_sellinfo5002
#defineIDB_set5003
#defineIDB_cancel5004
最后在对话框的.cpp文件中将控件与响应函数关联起来
BEGIN_MESSAGE_MAP(CBbbApp,CWinApp)
ON_COMMAND(IDB_bookinfo,OnBookInfo)
ON_COMMAND(IDB_booksell,OnBookInput)
ON_COMMAND(IDB_sellinfo,OnSaleQuery)
ON_COMMAND(IDB_set,OnButton4)
ON_COMMAND(IDB_cancel,OnCancel)
END_MESSAGE_MAP()
******************************************************/
方法
(二):
CImageListm_ImageList;
CToolBarCtrlm_ToolBar;
TBBUTTONtbButton[ICONSUM]={0};
(32,32,ILC_COLOR32|ILC_MASK,0,0);
(:
:
AfxGetApp()->LoadIcon(IDI_ICON4));
(:
:
AfxGetApp()->LoadIcon(IDI_ICON6));
(:
:
AfxGetApp()->LoadIcon(IDI_ICON5));
(:
:
AfxGetApp()->LoadIcon(IDI_ICON2));
(:
:
AfxGetApp()->LoadIcon(IDI_ICON3));
(WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),this,0);
(&m_ImageList);
for(intj=0;j { tbButton[j].dwData=NULL; tbButton[j].fsState=TBSTATE_ENABLED; tbButton[j].fsStyle=(BYTE)TBSTYLE_FLAT; tbButton[j].iBitmap=j; } tbButton[0].idCommand=ID_Info; tbButton[1].idCommand=ID_MR; tbButton[2].idCommand=ID_QS; tbButton[3].idCommand=ID_XS; tbButton[4].idCommand=ID_HELP; (ICONSUM,tbButton); (); (TBSTYLE_FLAT|CCS_TOP); 6、设置菜单有效和失效 有效: (ID_USER_REGISTER,MF_BYCOMMAND|MF_ENABLED); 失效: (ID_USER_REGISTER,MF_BYCOMMAND|MF_DISABLED|MF_GRAYED); 7、设置状态栏 CTimeTime; Time=CTime: : GetCurrentTime(); CStringStr=("%Y-%m-%d"); UINTArray[3]; for(inti=0;i<3;i++) { Array[i]=10000+i; } (this); (Array,3); (0,Array[0],0,150); (1,Array[1],0,450); (2,Array[2],0,150); (0,"用户: "+; (1,"CopyRight"); (2,"当前时间: "+Str,true); RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0); 8、设置菜单栏 (IDR_MainMENU); SetMenu(&m_Menu); 9、获得窗口WindowDCP464 CWindowDC*dc=(CWindowDC*)GetWindowDC(); 10、文档框架关闭时,恢复程序主窗口 voidCPrintFram: : OnClose() { =NULL; CPrintView*temp=(CPrintView*)this->GetActiveView(); if(! temp->ispreview) =; CFrameWnd: : OnClose(); } 11、窗体标题栏加上图标 先在头文件中声明: HICONm_hIcon; 在构造函数中: m_hIcon=AfxGetApp()->LoadIcon(IDI_ICON1); 在OnInitDialog()中: SetIcon(m_hIcon,TRUE);wData=NULL; tbButton[j].fsState=TBSTATE_ENABLED; tbButton[j].fsStyle=(BYTE)TBSTYLE_FLAT; tbButton[j].iBitmap=j; } tbButton[0].idCommand=ID_Sw; tbButton[1].idCommand=ID_BI; tbButton[2].idCommand=ID_MR; tbButton[3].idCommand=ID_QR; tbButton[4].idCommand=ID_XR; tbButton[5].idCommand=ID_HELP; tbButton[6].idCommand=ID_Exit; (ICONSUM,tbButton); (); (TBSTYLE_FLAT|CCS_TOP); 13、对话框的淡入淡出效果 #defineAW_CENTER0x00000010d.%d.%d",nField0,nField1,nField2,nField3); AfxMessageBox(str_IP); 10、对编辑框输入内容进行控制 (1)限制输入框的内容为数字 创建一继承自CEdit类的新类,并在以后使用文本框时用该类定义,在此类中写: voidCOnlyNumEdit: : OnChar(UINTnChar,UINTnRepCnt,UINTnFlags) { xe"; CStringtemp; intpos; temp=filename; pos=(appname,0); temp=(pos); returntemp; } 6、获取程序所在的根目录: P388 GetCurrentDirectory(MAX_PATH,path); 7、文件选择对话框使用 CFileDialogfileDlg(TRUE,"bmp",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"位图文件(*.bmp)|*.bmp||",this); if()! =IDOK) return; strPictureFile=(); C++编程常用模块(七): 图片操作 七、图片操作 1、加载图片并显示出来 charfilepath[MAX_PATH]; GetModuleFileName(NULL,filepath,MAX_PATH); CStringstrFile; strFile=ExtractFilePath(filepath)+"\\Image\\"+filename; CFileFindcf; if(strFile)) { m_Photo= (HBITMAP): : LoadImage(: : AfxGetInstanceHandle(),strFile,IMAGE_BITMAP,150,200, LR_LOADFROMFILE|LR_DEFAULTCOLOR|LR_DEFAULTSIZE); (m_Photo);db"; charfilepath[MAX_PATH]; GetModuleFileName(NULL,filepath,MAX_PATH); strDBSource=ExtractFilePath(filepath)+""; CopyFile(strDBSource,strpath,false);db)|*.mdb",NULL); if()==IDOK) { CStringstr; str=(); (str); } } BOOLCDBReturn: : OnInitDialog()文件中)定义按钮: CBaseButtonm_btnAdd; CBaseButtonm_btnModify; CBaseButtonm_btnDelete; l在.cpp文件中将上述定义关联到控件: voidCAddInfo: : DoDataExchange(CDataExchange*pDX) { DDX_Control(pDX,IDC_btnAdd,m_btnAdd); DDX_Control(pDX,IDC_btnModify,m_btnModify); DDX_Control(pDX,IDC_btnDelete,m_btnDelete); } l具体实现: (1)、文件: ///////////////////////////////////////////////////////////////////////////// //CBaseButtonwindow //定义按钮状态 enumButtonState{bsNormal,bsHot,bsDown}; classCBaseButton: publicCButton { //Construction public: CBaseButton(); intm_State; //Attributes public: //Operations public: //Overrides //ClassWizardgeneratedvirtualfunctionoverrides //{{AFX_VIRTUAL(CBaseButton) public: virtualvoidDrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct); //}}AFX_VIRTUAL //Implementation public: virtual~CBaseButton(); //Generatedmessagemapfunctions protected: //{{AFX_MSG(CBaseButton) afx_msgvoidOnLButtonDown(UINTnFlags,CPointpoint); afx_msgvoidOnLButtonUp(UINTnFlags,CPointpoint); afx_msgvoidOnKillFocus(CWnd*pNewWnd); afx_msgvoidOnMouseMove(UINTnFlags,CPointpoint); afx_msgvoidOnPaint(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// (2)、文件: ///////////////////////////////////////////////////////////////////////////// //CBaseButton CBaseButton: : CBaseButton() { m_State=bsNormal; } CBaseButton: : ~CBaseButton() { } BEGIN_MESSAGE_MAP(CBaseButton,CButton) //{{AFX_MSG_MAP(CBaseButton) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_KILLFOCUS() ON_WM_MOUSEMOVE() ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// //CBaseButtonmessagehandlers voidCBaseButton: : DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct) { //TODO: Addyourcodetodrawthespecifieditem OnPaint(); CDC*pDC; pDC=CDC: : FromHandle(lpDrawItemStruct->hDC); UINTstate=lpDrawItemStruct->itemState; CRectfocRC,RC=lpDrawItemStruct->rcItem; =+2; =-2; =+2; =-2; if(state&ODS_FOCUS) { pDC->DrawFocusRect(&focRC); lpDrawItemStruct->itemAction=ODA_FOCUS; } } voidCBaseButton: : OnPaint() { CPaintDCdc(this);//devicecontextforpainting //TODO: Addyourmessagehandlercodehere CWnd*pWnd=this; CRectforRC,RC; CFontFont; CFont*pOldFont; CBrushBrush; CBrush*pOldBrush; CPointPT(2,2); CStringText; (TRANSPARENT); (12,0,0,0,FW_HEAVY,0,0,0,ANSI_CHARSET,\ OUT_TT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,\ VARIABLE_PITCH|FF_SWISS,"MSSansSerif"); pOldFont=(&Font); if(m_State==bsNormal) { CBitmapbmp; (IDB_BITMAP4); (&bmp); (RGB(80,80,80)); } elseif(m_State==bsDown) { CBitmapbmp; (IDB_BITMAP6); (&bmp); (RGB(50,50,250)); } elseif(m_State==bsHot) { CBitmapbmp; (IDB_BITMAP5); (&bmp); (RGB(250,250,0)); } pOldBrush=(&Brush); pWnd->GetClientRect(&RC); (&RC,PT); HRGNhRgn=CreateRectRgn,,,; pWnd->SetWindowRgn(hRgn,TRUE); DeleteObject(hRgn); pWnd->GetWindowText(Text); (Text,&RC,DT_CENTER|DT_VCENTER|DT_SINGLELINE); (); (); (pOldFont); (pOldBrush); //DonotcallCButton: : OnPaint()forpaintingmessages } voidCBaseButton: : OnLButtonDown(UINTnFlags,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 编程 常用 模块