VS2019实现C++的第一个MFC程序

 更新时间:2021年06月07日 11:53:48   作者:心跳包  
本文主要介绍了VS2019实现C++的第一个MFC程序,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、创建项目


然后点下一步,配置项目,这里我命名的是myfisrtmfc

点击创建按钮,然后弹出下面的对话框。

对上面的MFC应用程序进行配置,如下:

点击完成,生成如下界面。

第一次编译生成的默认项目,之后得到下面的界面

点击VS2019的界面,“解决方案资源管理器”

到这里,项目建成,并且编译通过。

二、添加自定义的功能(以比较通用的画图为例)

点击资源视图,这里的控件将是后面需要操作的。

双击IDR_MAINFRAME,可以在这里面添加画图功能。

也可以在Ribbon里面添加画图功能

然后点击工具箱->RIbbon编辑器:

双击Ribbon下的面板控件

修改名称为形状,并添加一个按钮控件,修改名字为矩形

修改矩形的杂项,ID改为ID_RECTANGLE

右键矩形按键,选择添加事件处理程序

得到如下弹窗

配置这个弹窗如下:

点击确定后,我们得到下面的代码

以下内容参考https://www.jb51.net/article/214347.htm

第一次使用c++,mfc很多函数都不熟悉,就直接套用了。

这里我们新建一个graph.cpp源文件

#include "framework.h"
#include "pch.h"
 
IMPLEMENT_SERIAL(graph, CObject, 1)
graph::graph(int l, int u, int r, int d)
{
    left = l;
    up = u;
    right = r;
    down = d;
    state = 0;
    fcolor = 0xffffff;
}
 
void graph::Offset(int cx, int cy)
{
    left += cx;
    right += cx;
    up += cy;
    down += cy;
}
 
void graph::onPress(int x, int y)
{
    sx = x; sy = y;
    state = 0;
    //选中图形
    if (left < x && x < right &&
        up < y && y < down) {
        state = 1;
        return;
    }
    if (left - f_width / 2 < x && x < left + f_width / 2)    state |= 2;    //    选中左边
    if (up - f_width / 2 < y && y < up + f_width / 2)    state |= 4;//选中上边
    if (right - f_width / 2 < x && x < right + f_width / 2)    state |= 8;//选中右边
    if (down - f_width / 2 < y && y < down + f_width / 2)    state |= 16;    //    选中下边
 
}
 
void graph::onRelease(int x, int y)
{
    state = 0;
}
 
 
void graph::SetBorderColor(int color)
{
    fcolor = color;
}
 
void graph::SetFillColor(int color)
{
    bcolor = color;
}
int graph::onMove(int x, int y)
{
    int cx, cy;
    cx = x - sx; cy = y - sy;
    sx = x; sy = y;
 
    if (state == 1) {
        Offset(cx, cy);        //  位移量cx,cy
    }
 
    if (2 == (state & 2)) {
        left = x;
 
    }
 
    if (4 == (state & 4)) {
        up = y;
 
    }
 
    if (8 == (state & 8)) {
        right = x;
 
    }
 
    if (16 == (state & 16)) {
        down = y;
 
    }
    return state == 0 ? 0 : 1;
}
void graph::Serialize(CArchive& ar)
{
    CObject::Serialize(ar);
    if (ar.IsLoading()) {
        ar >> left >> right >> up >> down >> f_width >> fcolor >> bcolor;
    }
    else
    {
        ar << left << right << up << down << f_width << fcolor << bcolor;
    }
}
graph::~graph()
{
}
void graph::onDraw(CDC* pDC) {
    CBrush b(fcolor);
    pDC->SelectObject(&b);
    CRect r(left, up, right, down);
    pDC->FillRect(&r, &b);
    CPen p(PS_SOLID, 1, bcolor);
    pDC->SelectObject(&p);
    pDC->Rectangle(left, up, right, down);
    pDC->MoveTo(left, up);
    pDC->DrawText(_T("空图形"), -1, new CRect(left, up, right, down), DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}

在项目中添加头文件graphz.h

在graph.h中添加下面的代码:

#pragma once
 
class graph :
	public CObject
{
protected:
	//边框
	DECLARE_SERIAL(graph)
	int left, up, right, down;
	//选中状态
	unsigned int state;
	int sx, sy;
	int  f_width = 5;
	int fcolor = 0xffffff, bcolor = 0;
 
public:
	graph() :graph(50, 50, 100, 100) {
 
	}
	graph(int l, int u, int r, int d);
	void Offset(int cx, int cy);
	void  onPress(int x, int y);	//  鼠标按下
	int  onMove(int cx, int cy);	//  鼠标移动
	void  onRelease(int x, int y);	//  鼠标释放
	virtual void onDraw(CDC* pDC);
	virtual int getGraphID() { return 0; }
	virtual void Serialize(CArchive& ar);
	void SetFillColor(int color);
	void SetBorderColor(int color);
	~graph();
 
};

在framework.h中添加graph.h

#include "graph.h"

我们要画矩形,这里添加矩形的相关代码,

跟上面的步骤一样,见一个rectangle.h和rectangle.cpp

rectangle.cpp

#include "framework.h"
#include "pch.h"
rectangle::rectangle(int l, int u, int r, int d) :graph(l, u, r, d)
{
    state = 0;
    fcolor = 0xffffff;
 
}
 
void rectangle::onDraw(CDC* pDC)
{
    CBrush b(fcolor);
    pDC->SelectObject(&b);
    CRect r(left, up, right, down);
    pDC->FillRect(&r, &b);
    CPen p(PS_SOLID, 1, bcolor);
    pDC->SelectObject(&p);
    pDC->Rectangle(left, up, right, down);
    pDC->MoveTo(left, up);
}
 
rectangle::~rectangle()
{
}

rectangle.h

#include "graph.h"
class rectangle :
    public graph
{
public:
    //DECLARE_SERIAL(graph)
    //void Serialize(CArchive& ar);
    rectangle() :graph(50, 50, 100, 100) {}
    rectangle(int l, int u, int r, int d);
    void onDraw(CDC* pDC);
    int getGraphID() { return 2; }
    ~rectangle();
};

然后myfirstmfcDoc.h中添加list

std::list<graph*> graphList;

因为调用了list,所以在framework.h中添加

#include <list>


这里要调用用OnRectangle()函数,之前生成的函数,我们现在添加下面的代码:

    CmyfisrtmfcDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
    pDoc->graphList.push_front(new rectangle(50, 50, 100, 100));
 
    Invalidate();

修改myfirstmfcView.cpp中的OnDraw函数为如下:


void CmyfisrtmfcView::OnDraw(CDC* pDC)
{
    CmyfisrtmfcDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
 
    // TODO: 在此处为本机数据添加绘制代码
    std::list<graph*>::iterator v;
    for (v = pDoc->graphList.begin(); v != pDoc->graphList.end(); ++v) {
        (*v)->onDraw(pDC);
    }
}

接下来通过类向导添加消息

添加鼠标左键按下消息,左键松开消息,鼠标移动消息

在生成的按键按下函数中

void CmyfisrtmfcView::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    CmyfisrtmfcDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
 
    // TODO: 在此处为本机数据添加绘制代码
    std::list<graph*>::iterator v;
    for (v = pDoc->graphList.begin(); v != pDoc->graphList.end(); ++v) {
        (*v)->onPress(point.x, point.y);
    }
    Invalidate();
    //CView::OnLButtonDown(nFlags, point);
}

跟上面一样的方式

自从生成的代码在myfirstmfcView中如下:

void CmyfisrtmfcView::OnLButtonUp(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    CmyfisrtmfcDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
 
    // TODO: 在此处为本机数据添加绘制代码
    std::list<graph*>::iterator v;
    for (v = pDoc->graphList.begin(); v != pDoc->graphList.end(); ++v) {
        (*v)->onRelease(point.x, point.y);
    }
 
    //CView::OnLButtonUp(nFlags, point);
}
 
 
void CmyfisrtmfcView::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    CmyfisrtmfcDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
 
    // TODO: 在此处为本机数据添加绘制代码
    std::list<graph*>::iterator v;
    for (v = pDoc->graphList.begin(); v != pDoc->graphList.end(); ++v) {
        (*v)->onMove(point.x, point.y);
    }
    Invalidate();
//    CView::OnMouseMove(nFlags, point);
}

到这里就完成了全部工作,可以进行编译了。

生成下面的图形,矩形可以移动,可拉伸

点击项目中的属性,在配置属性中选择高级,MFC使用 静态库,在编译一次,生成.exe可以其他电脑上不依赖动态库也能打开了。

总结:

1.学会了如何添加项目工程

2.学会了添加用户自己的源文件和头文件,并且与项目关联

3.学会了类向导

4.学会了按键控件的生成,和通过消息ID跟函数关联起来

参考文献:

(1)vs2019 MFC实现office界面的画图小项目(超超级详细)

(2)在vs2019中使用MFC快速构建简单windows窗口程序

到此这篇关于VS2019实现C++的第一个MFC程序的文章就介绍到这了,更多相关C++第一个MFC程序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++插入排序算法实例详解

    C++插入排序算法实例详解

    这篇文章主要为大家详细介绍了C++插入排序算法实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • C语言命令行参数的使用详解

    C语言命令行参数的使用详解

    本文主要介绍了C语言命令行参数的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • C++中String类的常用接口函数总结

    C++中String类的常用接口函数总结

    这篇文章主要介绍了C++中Stirng类的常用接口函数,文中有详细的代码示例供大家参考,对我们学习C++有一定的帮助,感兴趣的同学可以跟着小编一起来学习
    2023-06-06
  • Qt图片绘图类之QPixmap/QImage/QPicture详解

    Qt图片绘图类之QPixmap/QImage/QPicture详解

    这篇文章主要为大家详细介绍了Qt图片绘图类中QPixmap、QImage和QPicture的使用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-03-03
  • C++中move的使用及说明

    C++中move的使用及说明

    这篇文章主要介绍了C++中move的使用及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • 使用Qt开发实现字幕滚动效果

    使用Qt开发实现字幕滚动效果

    我们经常能够在外面看到那种滚动字幕,那么就拿qt来做一个吧,文章通过代码示例给大家介绍的非常详细,对大家的学习或工作有有一定的参考价值,需要的朋友可以参考下
    2023-11-11
  • C语言安全编码数组记法的一致性

    C语言安全编码数组记法的一致性

    这篇文章主要介绍了C语言安全编码数组记法的一致性,需要的朋友可以参考下
    2014-07-07
  • C语言简明讲解变量的属性

    C语言简明讲解变量的属性

    我们知道以在 C 语言中的变量有自己的属性,只要在定义变量的时候加上“属性”关键字即可。“属性”关键字指明变量的特有意义,但是 register 关键字只是请求寄存器变量,所以不一定会成功
    2022-04-04
  • ubuntu 下编译C++代码出现的问题解决

    ubuntu 下编译C++代码出现的问题解决

    这篇文章主要介绍了ubuntu 下编译C++代码出现的问题解决的相关资料,需要的朋友可以参考下
    2015-03-03
  • C++11 condition_variable条件变量的用法说明

    C++11 condition_variable条件变量的用法说明

    这篇文章主要介绍了C++11 condition_variable条件变量的用法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07

最新评论