MFC入门教程VS2013
点击C++里的MFC再点击MFCApplication,到下面改名字和路径,然后OK
然后点击Next,
选择single document,MFCstandard,简体中文,然后Finish
这时候可以先直接运行,看看工程的样子,操作如下
编译完成,
稍后有个空白菜单框弹出;
我们先创建资源视图,点击解决方案,再资源文件里找到以rc开头的文件双击,如图
双击后就会有四个视图
先点击Class view,再点击CMFCApplication1View(这里名字不一样,但都是以View结尾的),
鼠标右键****View那个类,如图
点击Class Wizard
对清一下信息,从Message里找到WM_LBUTTONDOWN和WM_LBUTTONUP和WM_MOUSEMOVE分别双击他们(他们按照字母顺序排列的)
关了这个窗口,点击是
进入以下界面
点击右边的****view.h,双击它
双击后找到protected,在里面添加***view类的全局变量CPoint m_point;
BOOL m_signal;
CPoint m_point2;
添加后
再点击***view.cpp
找到
选中这三个函数,删掉这段代码
然后复制粘贴上这段代码
/*
//画直线
void CMFCApplication1View::OnLButtonDown(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
m_point = point;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication1View::OnLButtonUp(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.MoveTo(m_point);
dc.LineTo(point);
CView::OnLButtonUp(nFlags, point);
}
void CMFCApplication1View::OnMouseMove(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
CView::OnMouseMove(nFlags, point);
}
//画直线
*/
//画曲线
void CMFCApplication1View::OnLButtonDown(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
m_point = point;
m_signal = true;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication1View::OnLButtonUp(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
m_signal = false;
m_point2 = point;
CView::OnLButtonUp(nFlags, point);
}
void CMFCApplication1View::OnMouseMove(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
if (m_signal == true)
{
dc.MoveTo(m_point);
dc.LineTo(point);
m_point = point;
}
CView::OnMouseMove(nFlags, point);
}
//画曲线
/*
//涂鸦
void CMFCApplication1View::OnLButtonDown(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
m_point = point;
m_signal = true;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication1View::OnLButtonUp(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
CBitmap m_bitmap2;
m_bitmap2.LoadBitmapW(IDB_BITMAP1);
CBrush m_brush(&m_bitmap2);
CClientDC dc(this);
dc.FillRect(CRect(m_point, point), &m_brush);
CView::OnLButtonUp(nFlags, point);
}
void CMFCApplication1View::OnMouseMove(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
CView::OnMouseMove(nFlags, point);
}
//涂鸦
*/
/*
//扇形
void CMFCApplication1View::OnLButtonDown(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
m_point = point;
m_signal = true;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication1View::OnLButtonUp(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
m_signal = false;
m_point2 = point;
CView::OnLButtonUp(nFlags, point);
}
void CMFCApplication1View::OnMouseMove(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
CClientDC dc(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
点击画笔如图,在格子里画要涂鸦的内容
我写了个李,然后再关闭这个窗口,点击如图,然后是保存
然后运行,就是涂鸦的程序了
四个程序都能运行了