欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VS2019實(shí)現(xiàn)C++的第一個(gè)MFC程序

 更新時(shí)間:2021年06月07日 11:53:48   作者:心跳包  
本文主要介紹了VS2019實(shí)現(xiàn)C++的第一個(gè)MFC程序,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、創(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)文章

最新評(píng)論