VS2019實(shí)現(xiàn)C++的第一個(gè)MFC程序
一、創(chuàng)建項(xiàng)目
然后點(diǎn)下一步,配置項(xiàng)目,這里我命名的是myfisrtmfc
點(diǎn)擊創(chuàng)建按鈕,然后彈出下面的對(duì)話(huà)框。
對(duì)上面的MFC應(yīng)用程序進(jìn)行配置,如下:
點(diǎn)擊完成,生成如下界面。
第一次編譯生成的默認(rèn)項(xiàng)目,之后得到下面的界面
點(diǎn)擊VS2019的界面,“解決方案資源管理器”
到這里,項(xiàng)目建成,并且編譯通過(guò)。
二、添加自定義的功能(以比較通用的畫(huà)圖為例)
點(diǎn)擊資源視圖,這里的控件將是后面需要操作的。
雙擊IDR_MAINFRAME,可以在這里面添加畫(huà)圖功能。
也可以在Ribbon里面添加畫(huà)圖功能
然后點(diǎn)擊工具箱->RIbbon編輯器:
雙擊Ribbon下的面板控件
修改名稱(chēng)為形狀,并添加一個(gè)按鈕控件,修改名字為矩形
修改矩形的雜項(xiàng),ID改為ID_RECTANGLE
右鍵矩形按鍵,選擇添加事件處理程序
得到如下彈窗
配置這個(gè)彈窗如下:
點(diǎn)擊確定后,我們得到下面的代碼
以下內(nèi)容參考http://www.dbjr.com.cn/article/214347.htm
第一次使用c++,mfc很多函數(shù)都不熟悉,就直接套用了。
這里我們新建一個(gè)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("空?qǐng)D形"), -1, new CRect(left, up, right, down), DT_CENTER | DT_VCENTER | DT_SINGLELINE); }
在項(xiàng)目中添加頭文件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); // 鼠標(biāo)按下 int onMove(int cx, int cy); // 鼠標(biāo)移動(dòng) void onRelease(int x, int y); // 鼠標(biāo)釋放 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"
我們要畫(huà)矩形,這里添加矩形的相關(guān)代碼,
跟上面的步驟一樣,見(jiàn)一個(gè)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;
因?yàn)檎{(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: 在此處為本機(jī)數(shù)據(jù)添加繪制代碼 std::list<graph*>::iterator v; for (v = pDoc->graphList.begin(); v != pDoc->graphList.end(); ++v) { (*v)->onDraw(pDC); } }
接下來(lái)通過(guò)類(lèi)向?qū)砑酉?/p>
添加鼠標(biāo)左鍵按下消息,左鍵松開(kāi)消息,鼠標(biāo)移動(dòng)消息
在生成的按鍵按下函數(shù)中
void CmyfisrtmfcView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: 在此添加消息處理程序代碼和/或調(diào)用默認(rèn)值 CmyfisrtmfcDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 在此處為本機(jī)數(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)用默認(rèn)值 CmyfisrtmfcDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 在此處為本機(jī)數(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)用默認(rèn)值 CmyfisrtmfcDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 在此處為本機(jī)數(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); }
到這里就完成了全部工作,可以進(jìn)行編譯了。
生成下面的圖形,矩形可以移動(dòng),可拉伸
點(diǎn)擊項(xiàng)目中的屬性,在配置屬性中選擇高級(jí),MFC使用 靜態(tài)庫(kù),在編譯一次,生成.exe可以其他電腦上不依賴(lài)動(dòng)態(tài)庫(kù)也能打開(kāi)了。
總結(jié):
1.學(xué)會(huì)了如何添加項(xiàng)目工程
2.學(xué)會(huì)了添加用戶(hù)自己的源文件和頭文件,并且與項(xiàng)目關(guān)聯(lián)
3.學(xué)會(huì)了類(lèi)向?qū)?/strong>
4.學(xué)會(huì)了按鍵控件的生成,和通過(guò)消息ID跟函數(shù)關(guān)聯(lián)起來(lái)
參考文獻(xiàn):
(1)vs2019 MFC實(shí)現(xiàn)office界面的畫(huà)圖小項(xiàng)目(超超級(jí)詳細(xì))
(2)在vs2019中使用MFC快速構(gòu)建簡(jiǎn)單windows窗口程序
到此這篇關(guān)于VS2019實(shí)現(xiàn)C++的第一個(gè)MFC程序的文章就介紹到這了,更多相關(guān)C++第一個(gè)MFC程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VSCode 使用 Code Runner 插件無(wú)法編譯運(yùn)行文件名帶空格的文件問(wèn)題
這篇文章主要介紹了VSCode 使用 Code Runner 插件無(wú)法編譯運(yùn)行文件名帶空格的文件問(wèn)題,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07基于C++詳解數(shù)據(jù)結(jié)構(gòu)(附帶例題)
數(shù)據(jù)結(jié)構(gòu)作為每一個(gè)IT人不可回避的問(wèn)題,本文基于C++編寫(xiě),下面這篇文章主要給大家介紹了關(guān)于數(shù)據(jù)結(jié)構(gòu)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06C語(yǔ)言深入講解動(dòng)態(tài)內(nèi)存分配函數(shù)的使用
這篇文章主要介紹了C語(yǔ)言動(dòng)態(tài)內(nèi)存分配,C語(yǔ)言?xún)?nèi)存管理相關(guān)的函數(shù)主要有realloc、calloc、malloc、free、柔性數(shù)組等,下面這篇文章帶大家了解一下2022-05-05C語(yǔ)言函數(shù)棧幀的創(chuàng)建和銷(xiāo)毀介紹
大家好,本篇文章主要講的是C語(yǔ)言函數(shù)棧幀的創(chuàng)建和銷(xiāo)毀介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下2021-12-12C++中sln,vcxproj,vcxproj.filters,lib,dll,exe的含義說(shuō)明
這篇文章主要介紹了C++中sln,vcxproj,vcxproj.filters,lib,dll,exe的含義說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05C++實(shí)現(xiàn)簡(jiǎn)單FTP客戶(hù)端軟件開(kāi)發(fā)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單FTP客戶(hù)端軟件開(kāi)發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08VisualStudio2022制作多項(xiàng)目模板及Vsix插件的實(shí)現(xiàn)
本文主要介紹了VisualStudio2022制作多項(xiàng)目模板及Vsix插件的實(shí)現(xiàn),文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06C++實(shí)現(xiàn)圖片轉(zhuǎn)base64的示例代碼
Base64就是一種 基于64個(gè)可打印字符來(lái)表示二進(jìn)制數(shù)據(jù)的表示方法,本文主要為大家詳細(xì)介紹了如何使用C++實(shí)現(xiàn)圖片轉(zhuǎn)base64,需要的可以參考下2024-04-04matlab鳥(niǎo)群算法求解車(chē)間調(diào)度問(wèn)題詳解及實(shí)現(xiàn)源碼
這篇文章主要為大家介紹了matlab鳥(niǎo)群算法求解車(chē)間調(diào)度的問(wèn)題分析及實(shí)現(xiàn)源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02