《mfc编程小结.doc》由会员分享,可在线阅读,更多相关《mfc编程小结.doc(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1,mfc显示位图if(!m_Dlgbar.Create(this, IDD_DIALOGBAR, CBRS_LEFT|CBRS_TOOLTIPS|CBRS_FLYBY, IDD_DIALOGBAR) TRACE0(Failed to create DlgBarn); return -1; / fail to create void CTestView:OnDraw(CDC* pDC)CTestDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data here/ TODO: add dr
2、aw code for native data here/在视图中显示一副图像/*HBITMAP hBitmap = (HBITMAP) LoadImage(NULL, F:/testui/testui.bmp,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);/定义一个位图对象CBitmap m_bitmap;/将位图句柄与位图对象相连接m_bitmap.Attach(hBitmap);/位图信息结构BITMAP bm;/将位图数据存入位图结构m_bitmap.GetBitmap(&bm);/源CDC d
3、cImage;dcImage.CreateCompatibleDC(pDC); dcImage.SelectObject(&m_bitmap);/画图pDC-BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dcImage,0,0,SRCCOPY);*/2,坐标显示 CDC *pDC=GetDC();CBrush backbrush(RGB(205,205,180);CBrush* pOldBrush = pDC-SelectObject(&backbrush);CRect rect;GetClientRect(rect);pDC-PatBlt(rect.left, rec
4、t.top, rect.Width(), rect.Height(), PATCOPY);pDC-SelectObject(pOldBrush);/划XY坐标CPen pen1(PS_SOLID,3,RGB(0,0,205);CPen pen2(PS_SOLID,1,RGB(209,209,209);CClientDC dc(this);CPen *poldpen =dc.SelectObject(&pen1);dc.MoveTo(40,20);dc.LineTo(40,320);/o点dc.MoveTo(40,320);dc.LineTo(1040,320);dc.MoveTo(40,360
5、);dc.LineTo(40,660);/o点dc.MoveTo(40,660);dc.LineTo(1040,660);dc.SetBkColor(RGB(205,205,180);dc.SetTextColor(RGB(0,0,0);dc.SetTextAlign(TA_RIGHT);dc.TextOut(30,310,0);dc.TextOut(30,650,0);/dc.TextOut(750,100,压力显示(PA));/划分隔线 和 y分度CString str;poldpen=dc.SelectObject(&pen2);for (int i=1;iEnable( TRUE );
6、 4,导出execl文件在stdAfx.h中添加#include afxdb.h CDatabase database;CString sDriver = MICROSOFT EXCEL DRIVER (*.XLS); / Excel安装驱动CString sExcelFile,sPath; CString sSql,sql;/获取主程序所在路径,存在sPath中GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);sPath.ReleaseBuffer ();int nPos;nPos=sPath.Rev
7、erseFind ();sPath=sPath.Left (nPos);/ 要建立的Excel文件sExcelFile = sPath + exceltest.xls; /sExcelFile = F:exceltest.xls; CFileFind finder;BOOL bWorking = finder.FindFile(sExcelFile);/寻找文件if (bWorking)/如果已经存在文件,则删除CFile:Remove(LPCTSTR)sExcelFile);/ 创建进行存取的字符串sql.Format(DRIVER=%s;DSN=;FIRSTROWHASNAMES=1;RE
8、ADONLY=FALSE;CREATE_DB=%s;DBQ=%s,sDriver, sExcelFile, sExcelFile);if( database.OpenEx(sql,CDatabase:noOdbcDialog) )sSql = CREATE TABLE Exceldemo (压力 NUMBER,温度 NUMBER,时间 TEXT,仪表号 TEXT); database.ExecuteSQL(sSql); sSql=insert into Exceldemo values(1,20,2012-12-21,4);database.ExecuteSQL(sSql);/ 关闭数据库da
9、tabase.Close();AfxMessageBox(Excel文件写入成功!);5,ado操作access在stdAfx.h中#include / MFC support for Windows Common Controls#import C:Program FilesCommon FilesSystemadomsado15.dll no_namespace rename(EOF,rsEOF)#endif / _AFX_NO_AFXCMN_SUPPORTCoInitialize(NULL);_ConnectionPtr pConn(_uuidof(Connection);_Record
10、setPtr pRst(_uuidof(Recordset);_CommandPtr pCmd(_uuidof(Command);pConn-ConnectionString=Provider=MIcrosoft.Jet.OLEDB.4.0;Data source=student.mdb; /pCmd-put_ActiveConnection(_variant_t(IDispatch*)pConn);/ pCmd-CommandText=select * from student;/ pRst=pCmd-Execute(NULL,NULL,adCmdText); pConn-Open(,adC
11、onnectUnspecified); /pCmd-CommandText=INSERT INTO student (num,name,age) VALUES (11,cxk1,16);/pRst=pCmd-Execute(NULL,NULL,adCmdText); CString sql,cname; /cname=name; /int d=220;/ sql.Format(_T(insert into student(num,name,age) values(%d,%s,16),d,cname);/sql=delete * from student; pConn-Execute(_bstr
12、_t)sql,NULL,adCmdText); pRst=pConn-Execute(select * from student,NULL,adCmdText); while(!pRst-rsEOF) (CListBox*)GetDlgItem(IDC_LIST1)-AddString(_bstr_t)pRst-GetCollect(name); pRst-MoveNext(); pRst-Close(); pConn-Close(); pCmd.Release(); pRst.Release(); pConn.Release(); CoUninitialize();6 。mfc 网络编程WO
13、RD wVersionRequested;WSADATA wsaData;int err;wVersionRequested=MAKEWORD(1,1);err=WSAStartup(wVersionRequested,&wsaData);if(err!=0)return;if(LOBYTE(wsaData.wVersion)!=1|HIBYTE(wsaData.wVersion)!=1)WSACleanup();return ;SOCKET sockSrc=socket(AF_INET,SOCK_STREAM,0);SOCKADDR_IN addrSrc;addrSrc.sin_addr.S
14、_un.S_addr=htonl(INADDR_ANY);addrSrc.sin_family=AF_INET;addrSrc.sin_port=htons(7000);bind(sockSrc,(SOCKADDR*)&addrSrc,sizeof(SOCKADDR);listen(sockSrc,5);SOCKADDR_IN addrclient;int len=sizeof(SOCKADDR);SOCKET sockconn=accept(sockSrc,(SOCKADDR*)&addrclient,&len);char data10;recv(sockconn,data,10,0);closesocket(sockconn);SetDlgItemText(IDC_INPUT,data);closesocket(sockSrc);WSACleanup();