在绘图中添加网格选项。
建立类向导
使用二重循环 在屏幕上绘制40X30个小矩形(模拟屏幕纵横比为4 : 3),每个网格为边长为10个像 素的正方形。设定虚拟网格坐标系的原点为左上角第一个网格中点,虚拟网格坐标系的* 轴水平向右点轴垂直向下。
矩形的中心点为参考点,然后根 据直线中Bresenham等算法的计算结果,填充每个小矩形。
在项目中新建p2.h,NewPixel.h以及他们的cpp文件。
// P2.h: interface for the CP2 class.
//
//
#if !defind(AFX_P2_H__E525809F_8CB8_41E2_ABF7_3909432BB73D__INCLUDED_)
#define AFX_P2_H__E525809F_8CB8_41E2_ABF7_3909432BB73D__INCLUDED_
class Cp2 //二维点类
{
public:
Cp2(){
};
virtual ~Cp2(){
};
public:
int x;
int y;
}
#endif // !defined(AFX_P2_H__E525809F_8CB8_41E2_ABF7_3909432BB73D__INCLUDED_)
// P2.cpp: implementation of the CP2 class.
//
#include "stdafx.h"
#include "text1.h"
#include "p2.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
Cp2::Cp2(){
}
Cp2::~Cp2(){
}
// NewPixel.h: interface for the CNewPixel class.
//
#if !defined(AFX_NEWPIXEL_H__CC9BA4DE_B83D_4B9E_8DF1_74CF0496B622__INCLUDED_)
#define AFX_NEWPIXEL_H__CC9BA4DE_B83D_4B9E_8DF1_74CF0496B622__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CNewPixel // 新像素类
{
public:
CNewPixel();
virtual ~CNewPixel();
int x;//像素中点x坐标
int y;//像素中点y坐标
}
#endif //!defined(AFX_NEWPIXEL_H__CC9BA4DE_B83D_4B9E_8DF1_74CF0496B622__INCLUDED_)
// NewPixel.cpp: implementation of the CNewPixel class.
//
#include "stdafx.h"
#include "Test.h"
#include "NewPixel.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CNewPixel::CNewPixel(){
}
CNewPixel::~CNewPixel(){
}
在MainFrm类中添加如下代码
protected: // control bar embedded members
CNewPixel pix[40][30]; //像素数组
Cp2 P0,P1,P; //直线坐标
int a; //像素长
添加网格绘制代码如下:
// 网格绘制
// 使用二重循环在屏幕上绘制80*60个小矩形
void CMainFrame::Onwangge()
{
CDC *pDC = GetDC();
int xstart=0,ystart=0;
a=10; //每个小矩形中有20个像素点
pix[0][0].x=xstart+a/2;pix[0][0].y=ystart+a/2; //取小矩形的中点
CPen NewPen,*OldPen;
//知识点连接:https://wenku.baidu.com/view/c6bd2317f18583d049645994.html
NewPen.CreatePen(PS_SOLID,1,RGB(0,0,250)); //2代表笔的线宽,就是两个像素宽
OldPen=pDC->SelectObject(&NewPen);
for(int i=0;i<80;i++