学习笔记二(MFC实验)
MFC 二维变化实验**
注:本人使用为2013版本VS
版本不同会导致有些内在文件不同,可根据自己版本进行更改
#今日收获
1、了解了添加类的方法
2、学习了使用类向导添加命令或者实验
3、学会了添加图标相应
第一步:添加类
1、首先我们要进行二维变化的前提是要进行图形的绘制,所以我们要添加如下三个类。
CPoint2、CRGB、CALine
具体步骤:
类向导->添加类-类名称
在13版本中会直接将.h和.cpp 文件前的C去掉,但是在更高的版本如19版本中则需要手动删除.h和.cpp前的C
代码如下:
RGB.h
#pragma once
class CRGB
{
public:
CRGB();
~CRGB();
CRGB(double red, double green, double blue, double alpha = 0.0);
void Normalize();
friend CRGB operator*(double scalar, const CRGB &c);
friend CRGB operator+(const CRGB &c0, const CRGB &c1);
friend CRGB operator-(const CRGB &c0, const CRGB &c1);
double red;
double green;
double blue;
double alpha;
};
RGB.cpp
#include "RGB.h"
CRGB::~CRGB()
{
}
CRGB::CRGB()
{
red = 1.0;
green = 1.0;
blue = 1.0;
alpha = 1.0;
}
CRGB::CRGB(double red, double green, double blue, double alpha)
{
this->red = red;
this->green = green;
this->blue = blue;
this->alpha = alpha;
}
void CRGB::Normalize()
{
red = (red < 0.0) ? 0.0 : ((red > 1.0) ? 1.0 : red);
green = (green < 0.0) ? 0.0 : ((green > 1.0) ? 1.0 : green);
blue = (blue < 0.0) ? 0.0 : ((blue > 1.0) ? 1.0 : blue);
}
CRGB operator *(double scalar, const CRGB &c)
{
CRGB color;
color.red = scalar * c.red;
color.green = scalar * c.green;
color.blue = scalar * c.blue;
return color;
}
CRGB operator+(const CRGB &c0, const CRGB &c1)
{
CRGB color;
color.red = c0.red + c1.red;
color.green = c0.green + c1.green;
color.blue = c0.blue + c1.blue;
return color;
}
CRGB operator-(const CRGB &c0, const CRGB &c1)
{
CRGB color;
color.red = c0.red - c1.red;
color.green = c0.green - c1.green;
color.blue = c0.blue - c1.blue;
return color;
}
Point2.h
#include "RGB.h"
class CPoint2
{
public:
CPoint2();
~CPoint2();
CPoint2(int x, int y);
CPoint2(int x, int y, CRGB c);
int x, y;
CRGB c;
};
Point2.cpp
#include "Point2.h"
CPoint2::~CPoint2()
{
}
CPoint2::CPoint2()
{
x = 0;
y = 0;
c = CRGB(0.0, 0.0, 0.0);
}
CPoint2::CPoint2(int x, int y)
{
this->x = x;
this->y = y;
}
CPoint2::CPoint2(int x, int y, CRGB c)
{
this->x = x;
this->y = y;
this->c = c;
}
ALine.h
#pragma once
#include "stdafx.h"
#include "Point2.h"
class CALine
{
public:
CALine();
~CALine();
void MoveTo(CDC* pDC, CPoint2 p0);
void MoveTo(CDC* pDC, int x, int y, CRGB c0);
void LineTo(CDC* pDC, CPoint2 p1);
void LineTo(CDC* pDC, int x, int y, CRGB c1);
CRGB LinearInterp(double t, double tStart, double tEnd, CRGB cStart, CRGB cEnd);
private:
CPoint2 P0;
CPoint2 P1;
};
ALine.cpp
#include "ALine.h"
#include"RGB.h"
#define COLOR(c) int(RGB(c.red*255,c.green*255,c.blue*255))
CALine::CALine()
{
}
CALine::~CALine()
{
this