MFC入门教程VS.docx
《MFC入门教程VS.docx》由会员分享,可在线阅读,更多相关《MFC入门教程VS.docx(19页珍藏版)》请在冰豆网上搜索。
MFC入门教程VS
点击C++里的MFC再点击MFCApplication,到下面改名字和路径,然后OK
然后点击Next,
选择singledocument,MFCstandard,简体中文,然后Finish
这时候可以先直接运行,看看工程的样子,操作如下
编译完成,
稍后有个空白菜单框弹出;
关闭菜单框,开始添加代码,
我们先创建资源视图,点击解决方案,再资源文件里找到以rc开头的文件双击,如图
双击后就会有四个视图
先点击Classview,再点击CMFCApplication1View(这里名字不一样,但都是以View结尾的),
鼠标右键****View那个类,如图
点击ClassWizard
对清一下信息,从Message里找到WM_LBUTTONDOWN和WM_LBUTTONUP和WM_MOUSEMOVE分别双击他们(他们按照字母顺序排列的)
关了这个窗口,点击是
进入以下界面
点击右边的****view.h,双击它
双击后找到protected,在里面添加***view类的全局变量
CPointm_point;
BOOLm_signal;
CPointm_point2;
添加后
再点击***view.cpp
找到
选中这三个函数,删掉这段代码
然后复制粘贴上这段代码
/*
//画直线
voidCMFCApplication1View:
:
OnLButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
m_point=point;
CView:
:
OnLButtonDown(nFlags,point);
}
voidCMFCApplication1View:
:
OnLButtonUp(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CClientDCdc(this);
dc.MoveTo(m_point);
dc.LineTo(point);
CView:
:
OnLButtonUp(nFlags,point);
}
voidCMFCApplication1View:
:
OnMouseMove(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CView:
:
OnMouseMove(nFlags,point);
}
//画直线
*/
//画曲线
voidCMFCApplication1View:
:
OnLButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
m_point=point;
m_signal=true;
CView:
:
OnLButtonDown(nFlags,point);
}
voidCMFCApplication1View:
:
OnLButtonUp(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
m_signal=false;
m_point2=point;
CView:
:
OnLButtonUp(nFlags,point);
}
voidCMFCApplication1View:
:
OnMouseMove(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CClientDCdc(this);
if(m_signal==true)
{
dc.MoveTo(m_point);
dc.LineTo(point);
m_point=point;
}
CView:
:
OnMouseMove(nFlags,point);
}
//画曲线
/*
//涂鸦
voidCMFCApplication1View:
:
OnLButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
m_point=point;
m_signal=true;
CView:
:
OnLButtonDown(nFlags,point);
}
voidCMFCApplication1View:
:
OnLButtonUp(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CBitmapm_bitmap2;
m_bitmap2.LoadBitmapW(IDB_BITMAP1);
CBrushm_brush(&m_bitmap2);
CClientDCdc(this);
dc.FillRect(CRect(m_point,point),&m_brush);
CView:
:
OnLButtonUp(nFlags,point);
}
voidCMFCApplication1View:
:
OnMouseMove(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CView:
:
OnMouseMove(nFlags,point);
}
//涂鸦
*/
/*
//扇形
voidCMFCApplication1View:
:
OnLButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
m_point=point;
m_signal=true;
CView:
:
OnLButtonDown(nFlags,point);
}
voidCMFCApplication1View:
:
OnLButtonUp(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
m_signal=false;
m_point2=point;
CView:
:
OnLButtonUp(nFlags,point);
}
voidCMFCApplication1View:
:
OnMouseMove(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CClientDCdc(this);
if(m_signal==true)
{
//CPen*blackpen=dc.SelectObject(&pen);
dc.MoveTo(m_point);
dc.LineTo(point);
//m_point=point;
dc.LineTo(m_point);
}
CView:
:
OnMouseMove(nFlags,point);
}
//扇形
*/
点击DEBUG
就会出现画曲线的程序,这是因为我画曲线的代码没注释掉(/**/和//)
你们可以把画曲线的代码注释掉,把画直线的注释去掉,再运行,就是画直线的了
其他都可以运行,只有涂鸦的不行,因为涂鸦还要操作:
1.去掉涂鸦代码的注释,把其他代码(三个函数)注释,
2.点击资源视图,找到以.rc结尾的文件,按鼠标右键,选择添加资源,如图
双击Bitmap
点击画笔如图,在格子里画要涂鸦的内容
我写了个李,然后再关闭这个窗口,点击如图,然后是保存
然后运行,就是涂鸦的程序了
四个程序都能运行了