📄 textview.cpp
字号:
// TextView.cpp : implementation of the CTextView class
//
#include "stdafx.h"
#include "Text.h"
#include "TextDoc.h"
#include "TextView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTextView
IMPLEMENT_DYNCREATE(CTextView, CView)
BEGIN_MESSAGE_MAP(CTextView, CView)
//{{AFX_MSG_MAP(CTextView)
ON_COMMAND(ID_MENUBRESENHAM, OnMenubresenham)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTextView construction/destruction
CTextView::CTextView()
{
// TODO: add construction code here
}
CTextView::~CTextView()
{
}
BOOL CTextView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTextView drawing
void CTextView::OnDraw(CDC* pDC)
{
CTextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CTextView printing
BOOL CTextView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTextView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTextView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTextView diagnostics
#ifdef _DEBUG
void CTextView::AssertValid() const
{
CView::AssertValid();
}
void CTextView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTextDoc* CTextView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextDoc)));
return (CTextDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTextView message handlers
void CTextView::Bresenham() //Bresenham直线
{
CDC *pDC;
pDC=GetDC();
int c=RGB(CR,CG,CB);
int x,y,dx,dy,dk;
dx=x2-x1;
dy=y2-y1;
dk=2*dy-dx;
if(x1!=x2)
{
int k=dy/dx;
if(y1==y2)
{
if(x1<x2) //水平从左至右
{
y=y1;
for(x=x1;x<=x2;x++)
{
pDC->SetPixel(x,y,c);
}
}
else; //水平从右至左
{
y=y1;
for(x=x1;x>=x2;x--)
{
pDC->SetPixel(x,y,c);
}
}
}
else if(abs(k)<1)
{
if(x1<x2&&y1<y2) //一区域
{
y=y1;
for(x=x1;x<=x2;x++)
{
pDC->SetPixel(x,y,c);
dk=dk+2*dy;
if(dk>=0)
{
y++;
dk=dk-2*dx;
}
}
}
if(x1>x2&&y1<y2)//四区域
{
y=y1;
for(x=x1;x>=x2;x--)
{
pDC->SetPixel(x,y,c);
dk=dk+2*dy;
if(dk>=0)
{
y++;
dk=dk-2*dx;
}
}
}
if(x1>x2&&y1>y2) //五区域
{
y=y1;
for(x=x1;x>=x2;x--)
{
pDC->SetPixel(x,y,c);
dk=dk-2*dx;
if(dk>=0)
{
y--;
dk=dk-2*dy;
}
}
}
if(x1<x2&&y1>y2) //八区域
{
y=y1;
for(x=x1;x<=x2;x++)
{
pDC->SetPixel(x,y,c);
dk=dk-2*dk;
if(dk>=0)
{
y--;
dk=dk-2*dy;
}
}
}
}
else
{
x=x1;
if(x1<x2&&y1<y2) //二区域
{
for(y=y1;y<=y2;y++)
{
pDC->SetPixel(x,y,c);
dk=dk+2*dx;
if(dk>=0)
{
x++;
dk=dk-2*dy;
}
}
}
if(x1>x2&&y1<y2) //三区域
{
for(y=y1;y<=y2;y++)
{
pDC->SetPixel(x,y,c);
dk=dk-2*dx;
if(dk>=0)
{
x--;
dk=dk+2*dy;
}
}
}
if(x1>x2&&y1>y2) //六区域
{
for(y=y1;y>=y2;y--)
{
pDC->SetPixel(x,y,c);
dk=dk-2*dx;
if(dk>=0)
{
x--;
dk=dk+2*dy;
}
}
}
if(x1<x2&&y1>y2) //七区域
{
for(y=y1;y>=y2;y--)
{
pDC->SetPixel(x,y,c);
dk=dk-2*dy;
if(dk>=0)
{
x++;
dk=dk+2*dx;
}
}
}
}
}
else if(x1==x2)
{
if(y1<y2) //垂直从上到下画
{
x=x1;
for(y=y1;y<=y2;y++)
{
pDC->SetPixel(x,y,c);
}
}
else //垂直从下往上画
{
x=x1;
for(y=y1;y>=y2;y--)
{
pDC->SetPixel(x,y,c);
}
}
}
ReleaseDC(pDC);
}
void CTextView::OnMenubresenham()
{
// TODO: Add your command handler code here
InputDlg dlg;
if(dlg.DoModal()==IDOK)
{
x1=dlg.m_x1;
y1=dlg.m_y1;
x2=dlg.m_x2;
y2=dlg.m_y2;
CR=dlg.m_CR;
CG=dlg.m_CG;
CB=dlg.m_CB;
}
Bresenham();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -