VS2019實現(xiàn)C++的第一個MFC程序
一、創(chuàng)建項目

然后點下一步,配置項目,這里我命名的是myfisrtmfc

點擊創(chuàng)建按鈕,然后彈出下面的對話框。

對上面的MFC應(yīng)用程序進行配置,如下:

點擊完成,生成如下界面。

第一次編譯生成的默認項目,之后得到下面的界面

點擊VS2019的界面,“解決方案資源管理器”

到這里,項目建成,并且編譯通過。
二、添加自定義的功能(以比較通用的畫圖為例)
點擊資源視圖,這里的控件將是后面需要操作的。

雙擊IDR_MAINFRAME,可以在這里面添加畫圖功能。

也可以在Ribbon里面添加畫圖功能

然后點擊工具箱->RIbbon編輯器:

雙擊Ribbon下的面板控件

修改名稱為形狀,并添加一個按鈕控件,修改名字為矩形

修改矩形的雜項,ID改為ID_RECTANGLE

右鍵矩形按鍵,選擇添加事件處理程序

得到如下彈窗

配置這個彈窗如下:

點擊確定后,我們得到下面的代碼
以下內(nèi)容參考http://www.dbjr.com.cn/article/214347.htm
第一次使用c++,mfc很多函數(shù)都不熟悉,就直接套用了。
這里我們新建一個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;
//選中狀態(tài)
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"
我們要畫矩形,這里添加矩形的相關(guān)代碼,
跟上面的步驟一樣,見一個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;
因為調(diào)用了list,所以在framework.h中添加
#include <list>

這里要調(diào)用用OnRectangle()函數(shù),之前生成的函數(shù),我們現(xiàn)在添加下面的代碼:

CmyfisrtmfcDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; pDoc->graphList.push_front(new rectangle(50, 50, 100, 100)); Invalidate();
修改myfirstmfcView.cpp中的OnDraw函數(shù)為如下:

void CmyfisrtmfcView::OnDraw(CDC* pDC)
{
CmyfisrtmfcDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此處為本機數(shù)據(jù)添加繪制代碼
std::list<graph*>::iterator v;
for (v = pDoc->graphList.begin(); v != pDoc->graphList.end(); ++v) {
(*v)->onDraw(pDC);
}
}
接下來通過類向?qū)砑酉?/p>


添加鼠標左鍵按下消息,左鍵松開消息,鼠標移動消息

在生成的按鍵按下函數(shù)中

void CmyfisrtmfcView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調(diào)用默認值
CmyfisrtmfcDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此處為本機數(shù)據(jù)添加繪制代碼
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: 在此添加消息處理程序代碼和/或調(diào)用默認值
CmyfisrtmfcDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此處為本機數(shù)據(jù)添加繪制代碼
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: 在此添加消息處理程序代碼和/或調(diào)用默認值
CmyfisrtmfcDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此處為本機數(shù)據(jù)添加繪制代碼
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使用 靜態(tài)庫,在編譯一次,生成.exe可以其他電腦上不依賴動態(tài)庫也能打開了。


總結(jié):
1.學(xué)會了如何添加項目工程
2.學(xué)會了添加用戶自己的源文件和頭文件,并且與項目關(guān)聯(lián)
3.學(xué)會了類向?qū)?/strong>
4.學(xué)會了按鍵控件的生成,和通過消息ID跟函數(shù)關(guān)聯(lián)起來
參考文獻:
(1)vs2019 MFC實現(xiàn)office界面的畫圖小項目(超超級詳細)
(2)在vs2019中使用MFC快速構(gòu)建簡單windows窗口程序
到此這篇關(guān)于VS2019實現(xiàn)C++的第一個MFC程序的文章就介紹到這了,更多相關(guān)C++第一個MFC程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VSCode 使用 Code Runner 插件無法編譯運行文件名帶空格的文件問題
這篇文章主要介紹了VSCode 使用 Code Runner 插件無法編譯運行文件名帶空格的文件問題,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下2021-07-07
基于C++詳解數(shù)據(jù)結(jié)構(gòu)(附帶例題)
數(shù)據(jù)結(jié)構(gòu)作為每一個IT人不可回避的問題,本文基于C++編寫,下面這篇文章主要給大家介紹了關(guān)于數(shù)據(jù)結(jié)構(gòu)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-06-06
C語言深入講解動態(tài)內(nèi)存分配函數(shù)的使用
這篇文章主要介紹了C語言動態(tài)內(nèi)存分配,C語言內(nèi)存管理相關(guān)的函數(shù)主要有realloc、calloc、malloc、free、柔性數(shù)組等,下面這篇文章帶大家了解一下2022-05-05
C++中sln,vcxproj,vcxproj.filters,lib,dll,exe的含義說明
這篇文章主要介紹了C++中sln,vcxproj,vcxproj.filters,lib,dll,exe的含義說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
VisualStudio2022制作多項目模板及Vsix插件的實現(xiàn)
本文主要介紹了VisualStudio2022制作多項目模板及Vsix插件的實現(xiàn),文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
C++實現(xiàn)圖片轉(zhuǎn)base64的示例代碼
Base64就是一種 基于64個可打印字符來表示二進制數(shù)據(jù)的表示方法,本文主要為大家詳細介紹了如何使用C++實現(xiàn)圖片轉(zhuǎn)base64,需要的可以參考下2024-04-04
matlab鳥群算法求解車間調(diào)度問題詳解及實現(xiàn)源碼
這篇文章主要為大家介紹了matlab鳥群算法求解車間調(diào)度的問題分析及實現(xiàn)源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02

