如何在VS中设置GDI+,How to set up GDI+ in Visual studio

如何在VS中设置GDI+,How to set up GDI+ in Visual studio

  • 原始资料地址
  • 新建工程
  • Property setting
  • include
  • build/Debug
  • Add canvas

原始资料地址

来自Youtube视频:https://www.youtube.com/watch?v=ow3nbqPikG4
由于视频翻阅起来不容易,我就用截图的方式记录下来,以及我如何跟着视频操作的过程也会附上。
视频中的环境是VS2015,我自己的是VS2017

新建工程

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第1张图片

新建工程
如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第2张图片

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第3张图片

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第4张图片

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第5张图片

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第6张图片

Property setting

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第7张图片

include

前面的截图来自视频,后面的截图来自我自己的照着视频写的程序。我的工程名字和视频不一样。
如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第8张图片

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第9张图片

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第10张图片

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第11张图片

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第12张图片

build/Debug

到此运行,可以得到一个窗口。GDI+的配置部分也是完全完成了。
接下来的操作就是在窗口中的操作了。

Add canvas

在resource view的窗口中找到运行时希望跳出来的那个框,可以把它拉到你想要的大小。
如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第13张图片

在toolbox中找到picture control。
如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第14张图片
在刚刚的窗口界面中画出想要的canvas大小,之后我们就可以在canvas中进行一些其他操作了。
如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第15张图片

选中刚刚画的canvas,在property窗口中修改名字。
如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第16张图片

增加一个新的类,名字叫canvas
如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第17张图片
然后再在Canvas中增加两个new item,一个.h文件,一个.cpp文件。
如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第18张图片

在.h中的文件是这样的:

#pragma once
#include 
#pragma comment(lib, "GdiPlus.lib")


class CCanvas : public CWnd
{
public:
	CCanvas();
	virtual ~CCanvas();

protected:
	void OnPaint();

	DECLARE_MESSAGE_MAP();
};

在.cpp中是这样的:


#include "stdafx.h"

#include "Canvas.h"


//using namespace Gdiplus;

BEGIN_MESSAGE_MAP(CCanvas, CWnd)

	ON_WM_PAINT()

END_MESSAGE_MAP()



CCanvas::CCanvas()
{

}

CCanvas::~CCanvas()
{

}



void CCanvas::OnPaint()
{

	CWnd::OnPaint();

	CRect rcClient;
	GetClientRect(&rcClient);
	//COLORREF color1;

	//static COLORREF color2 = RGB(255,0,0);
	//color1 = RGB(0, 255, 0);

	CDC* pDC = GetDC();

	Gdiplus::Graphics graphics(pDC->m_hDC);

	graphics.Clear(Gdiplus::Color(255, 0, 0, 0));

	Gdiplus::Pen pen(Gdiplus::Color(255, 0, 0, 255), 4.0F);


	graphics.DrawLine(&pen, (INT)(rcClient.Width() * 0.25), (INT)(rcClient.Height() * 0.25),
		(INT)(rcClient.Width() * 0.75), (INT)(rcClient.Height() * 0.75)); //用定义的画笔画两条线
	graphics.DrawLine(&pen, (INT)(rcClient.Width() * 0.75), (INT)(rcClient.Height() * 0.25),
		(INT)(rcClient.Width() * 0.25), (INT)(rcClient.Height() * 0.75));

	ReleaseDC(pDC);

}

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第19张图片

如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第20张图片

然后编译运行,可以得到如下结果:
如何在VS中设置GDI+,How to set up GDI+ in Visual studio_第21张图片

你可能感兴趣的:(工具调试笔记)