1、VC+操作指南VC+6.0应用步骤及例题一、 VC+6.0 Win32应用步骤第一步:打开Microsoft Visual C+ 6.0运行软件;第二步:在主菜单中选文件下拉菜单新建出现如下界面,填写工程名称,选中Win32 Console Application后,点确定; 第三步:在主菜单中选文件下拉菜单新建出现如下界面,填写文件名,选中C+ Source Fine后,点确定; 第四步:在空白工作区编程,编译、链接、运行程序。例题1:#include using namespace std; void main() int array10; int i,j,min; cout请输入数值en
2、dl; for(i=0;iarrayi; cout arrayi ” “; min=array0; for(j=1;jarrayj) min=arrayj; coutmin=min CView中打开OnDraw添加代码void CDView:OnDraw(CDC* pDC) CDDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); / TODO: add draw code for native data here CRect rectClient, rectTitle; int nWidth = 240; int nHeight = 40; GetCl
3、ientRect(&rectClient); rectTitle.left = (rectClient.Width()-nWidth)/2; rectTitle.top = (rectClient.Height()-nHeight)/2; rectTitle.right = rectTitle.left+nWidth; rectTitle.bottom = rectTitle.top +nHeight; pDC-Rectangle(rectTitle); pDC-TextOut(rectTitle.left+10,rectTitle.top+10, wang xiaoming:Hello mi
4、ss liu);运行结果:例3.窗口显示字符串“Greetings!”第一步:Header File中打开CgreetDoc.h添加代码class CGreetDoc : public CDocumentchar *m_Message;public:char *GetMessage()return m_Message; protected: / create from serialization only第二步:Source File中打开CgreetDoc.cpp添加代码CGreetDoc:CGreetDoc() / TODO: add one-time construction code
5、herem_Message=”Greetings!”; 第三步:Source File中打开CgreetView.cpp添加代码void CGreetView:OnDraw(CDC* pDC) CGreetDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); / TODO: add draw code for native data hereRECT ClientRect; GetClientRect(&ClientRect); pDC-DrawText (pDoc-GetMessage(),/obtain the string -1, &ClientR
6、ect, DT_CENTER|DT_VCENTER|DT_SINGLELINE); 运行结果:*后面几道题同学们自己运行一下,有问题可以答疑!例4.在视图窗口绘制直线第一步:Header File中打开CLiu1View.h添加代码class CLiu1View : public CViewprotected:CString m_ClassName;int m_Dragging;HCURSOR m_HCross;CPoint m_PointOld;CPoint m_PointOrigin;protected: / create from serialization only第二步:Source
7、 File中打开CLiu1View.cpp添加代码CLiu1View:CLiu1View() / TODO: add construction code herem_Dragging=0;m_HCross=AfxGetApp()-LoadStandardCursor(IDC_CROSS);CLiu1View:CLiu1View()第三步:添加OnLButtonDown函数:(1) 选View菜单-ClassWizard出现(2) Class name选CLiu1View(3) Object IDs选CLiu1View(4) Message选WM_LBBUTTONDOWN(5) 单击Add Fu
8、nction按钮(6) 单击Edit Code在生成的框架中添加相应代码void CLiu1View:OnLButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultm_PointOrigin=point; m_PointOld=point; SetCapture(); m_Dragging=1; RECT Rect; GetClientRect(&Rect); ClientToScreen(&Rect);:ClipCursor(&Rect); CVie
9、w:OnLButtonDown(nFlags, point);第四步:添加OnMouseMove函数:void CLiu1View:OnMouseMove(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call default :SetCursor(m_HCross); if(m_Dragging) CClientDC ClientDC(this);ClientDC.SetROP2(R2_NOT);ClientDC.MoveTo(m_PointOrigin);ClientDC.LineT
10、o(m_PointOld);ClientDC.MoveTo(m_PointOrigin);ClientDC.LineTo(point);m_PointOld=point; CView:OnMouseMove(nFlags, point);第五步:添加OnLButtonUp函数void CLiu1View:OnLButtonUp(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call default if(m_Dragging) m_Dragging=0; :ReleaseCapture(
11、); :ClipCursor(NULL);CClientDC ClientDC(this);ClientDC.SetROP2(R2_NOT);ClientDC.MoveTo(m_PointOrigin);ClientDC.LineTo(m_PointOld);ClientDC.SetROP2(R2_COPYPEN);ClientDC.MoveTo(m_PointOrigin);ClientDC.LineTo(point); CView:OnLButtonUp(nFlags, point);例5.单击鼠标左键,弹出对话框显示鼠标所在点的坐标;按下键盘任意一个键,显示用户所第一步:为鼠标设定消息绑
12、定机制,编辑OnLButtonDown函数,在C View.cpp加入代码void CMyView:OnLButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call default CString strDisplay; strDisplay.Format(X=%d,Y=%d,point.x,point.y); MessageBox(strDisplay); CView:OnLButtonDown(nFlags, point);第二步:为鼠标设定消息绑定机制,编辑OnK
13、eyDown函数,在C View.cpp加入代码void CMyView:OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) / TODO: Add your message handler code here and/or call default CString strDisplay; strDisplay.Format(用户按下键%c,键值为%d, nChar,nChar); MessageBox(strDisplay); CView:OnKeyDown(nChar, nRepCnt, nFlags);例6单击鼠标左键画圆并弹出对话框,显示圆
14、在视图窗口中矩形的里面还是外面第一步:在C View.h中添加代码/AFX_VIRTUAL/ Implementation public: CRect m_rect;public: virtual C My2View();第二步:在C View.cpp中添加代码C My2View:C My2View():m_rect(0,0,200,200) / TODO: add construction code hereC My2View:C My2View()CMy2View:CMy2View()void C My2View:OnDraw(CDC* pDC) C My2Doc* pDoc = GetD
15、ocument(); ASSERT_VALID(pDoc); / TODO: add draw code for native data here pDC-Rectangle(m_rect);第三步:添加OnLButtonDown函数:void C My2View:OnLButtonDown(UINT nFlags, CPoint point) CDC*pDC=this-GetDC();if(m_rect.PtInRect(point) pDC-Ellipse(point.x-5,point.y-5,point.x+5,point.y+5); MessageBox(In the box);el
16、se pDC-Ellipse(point.x-5,point.y-5,point.x+5,point.y+5);MessageBox(Out of the box); / TODO: Add your message handler code here and/or call default CView:OnLButtonDown(nFlags, point);例7计数器:单击鼠标左键开始记数,单击鼠标右键停止记数第一步:在C View.h中添加代码class CMy3View : public CViewprotected: / create from serialization only
17、CMy3View(); DECLARE_DYNCREATE(CMy3View)/ Attributesint m_iCount;第二步:在C View.cpp中添加代码CMy3View:CMy3View() / TODO: add construction code here m_iCount=0;CMy3View:CMy3View()void CMy3View:OnDraw(CDC* pDC) CMy3Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); / TODO: add draw code for native data here CStrin
18、g strDisplay; strDisplay.Format(当前值:%d,m_iCount); pDC-TextOut(10,10,strDisplay);第三步:添加OnLButtonDown函数void CMy3View:OnLButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call default m_iCount=0; /保证计数器的重置 SetTimer(1,100,NULL); CView:OnLButtonDown(nFlags, point);第四
19、步:添加OnRButtonDown函数void CMy3View:OnRButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call default KillTimer(1); /删除定时器 CView:OnRButtonDown(nFlags, point);第五步:添加OnTimer函数void CMy3View:OnTimer(UINT nIDEvent) / TODO: Add your message handler code here and/or call
20、default m_iCount+; CRect rectReDraw(10,10,120,30); InvalidateRect(rectReDraw,TRUE); CView:OnTimer(nIDEvent);例8.显示一张位图第一步:修改视图类的定义(头文件),加入四个变量的定义class CMy1View : public CView/定义CBitmap类对象以及位图的显示参数变量CBitmap m_Bitmap;float m_fTimes;int m_nHeight;int m_nWidth;protected: / create from serialization only
21、CMy1View(); DECLARE_DYNCREATE(CMy1View)/ Attributes第二步:修改视图类的构造函数CMy1View:CMy1View() / TODO: add construction code here/载入位图资源,读位图信息BITMAP BM;m_Bitmap.LoadBitmap(IDB_BITMAP1);m_Bitmap.GetBitmap(&BM);m_nWidth=BM.bmWidth;m_nHeight=BM.bmHeight;m_fTimes=1.0;CMy1View:CMy1View()/ IDB_BITMAP1是VC+先使用缺省标识符(生
22、成的第一个位图为IDB_BITMAP1,生成的第二个位图为IDB_BITMAP2,等等)用位图编辑器设计位图步骤:选择Insert-Resource-Bitmap-New Bitmap 设计新位图时,也可以使用引用以文件扩展名为BMP或DIB存成的位图,步骤:Insert-Resource-Bitmap-Import,选位图存盘的文件名,引入的位图出现在VC+位图编辑器中,可以编辑第三步:修改视图类的OnDraw函数,显示位图void CMy1View:OnDraw(CDC* pDC) CMy1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); / T
23、ODO: add draw code for native data hereCDC MemDC;MemDC.CreateCompatibleDC(NULL);MemDC.SelectObject(&m_Bitmap);pDC-StretchBlt(0,0,(int)(m_nWidth*m_fTimes), (int)(m_nHeight*m_fTimes), &MemDC,0,0,m_nWidth,m_nHeight,SRCCOPY);第四步:MFC的类向导生成菜单消息处理函数,加入代码/原样显示void CMy1View:OnBestfit() / TODO: Add your comma
24、nd handler code herem_fTimes=1.0;Invalidate(); void CMy1View:OnUpdateBestfit(CCmdUI* pCmdUI) / TODO: Add your command update UI handler code here pCmdUI-SetCheck(m_fTimes=1.0); /缩小图像void CMy1View:OnShrink() / TODO: Add your command handler code herem_fTimes=0.5;Invalidate(); void CMy1View:OnUpdateSh
25、rink(CCmdUI* pCmdUI) / TODO: Add your command update UI handler code herepCmdUI-SetCheck(m_fTimes=0.5); /放大图像void CMy1View:OnZoomout() / TODO: Add your command handler code herem_fTimes=2.0;Invalidate(); void CMy1View:OnUpdateZoomout(CCmdUI* pCmdUI) / TODO: Add your command update UI handler code he
26、repCmdUI-SetCheck(m_fTimes=2.0); 例9.第一步:修改文档类(Doc.h)的定义(头文件),#define MAX_BUBBLE 250class CMy2Doc : public CDocumentprotected: / create from serialization only CMy2Doc(); DECLARE_DYNCREATE(CMy2Doc)/ Attributespublic:CRect m_rectBubbleMAX_BUBBLE;int m_nBubbleCount;/ Operations第二步:修改文档类的OnNewDocument函数
27、,对变量进行初始化BOOL CMy2Doc:OnNewDocument() if (!CDocument:OnNewDocument() return FALSE;m_nBubbleCount=0; / TODO: add reinitialization code here / (SDI documents will reuse this document) return TRUE;第二步:修改文档类(Doc.cpp)的:Serialize函数void CMy2Doc:Serialize(CArchive& ar) if (ar.IsStoring() / TODO: add storing code here arm_nBubbleCount; for(int i=0;im_nBubbleCount;i+) arm_nBubbleCount; for(int i=0;im_rectBubblei; 第三步:修改视图类(CView.cpp)的OnDraw函数void CMy2View:OnDraw(CDC* pDC) CMy2Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); / TODO: add draw code for native data here for(int i=0;im_nBubbleCount
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1