📄 test9view.cpp
字号:
// test9View.cpp : implementation of the CTest9View class
//
#include "stdafx.h"
#include "test9.h"
#include "math.h"
#include "test9Doc.h"
#include "test9View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTest9View
IMPLEMENT_DYNCREATE(CTest9View, CView)
BEGIN_MESSAGE_MAP(CTest9View, CView)
//{{AFX_MSG_MAP(CTest9View)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CTest9View construction/destruction
CTest9View::CTest9View()
{
// TODO: add construction code here
}
CTest9View::~CTest9View()
{
}
BOOL CTest9View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTest9View drawing
void CTest9View::OnDraw(CDC* pDC)
{
CTest9Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect Rect; //定义矩形对象
GetClientRect(&Rect); //获得当前窗口的客户区大小
int CenterX1=Rect.right/4;
int CenterY1=Rect.bottom/2;
int CenterX2=3*Rect.right/4;
int CenterY2=Rect.bottom/2;
int x0,y0;
int x1,y1;
//取当前时间
CTime Time=CTime::GetCurrentTime();
CString str;
int x,y;
CSize size;
CPen pen(PS_SOLID,4,RGB(55,55,0));
//设置当前画笔,并记下以前的笔
CPen *OldPen=pDC->SelectObject(&pen);
// 绘制钟面椭圆
pDC->Ellipse(5,5,(Rect.right-5)/2,Rect.bottom-5);
pDC->Ellipse((Rect.right-5)/2,4,Rect.right-5,Rect.bottom-5);
double Radians;
//设置字体颜色为红色
pDC->SetTextColor(RGB(245,160,0));
for(int i=1;i<=12;i++)
{
//格式化钟点值
str.Format("%d",i);
//GetTextExtent method computes the width and height of a line of text using the current font to determine the dimensions
size=pDC->GetTextExtent(str,str.GetLength());
Radians=(double)i*6.28/12.0;
// 计算钟点放置的位置
x=CenterX1-(size.cx/2)+(int)((double)(CenterX1-20)*sin(Radians));
y=CenterY1-(size.cy/2)-(int)((double)(CenterY1-20)*cos(Radians));
//绘制钟点
pDC->TextOut(x,y,str);
//格式化钟点值
str.Format("%d",i);
//GetTextExtent method computes the width and height of a line of text using the current font to determine the dimensions
size=pDC->GetTextExtent(str,str.GetLength());
Radians=(double)i*6.28/12.0;
// 计算钟点放置的位置
x=CenterX2-(size.cx/2)+(int)((double)(CenterX1-20)*sin(Radians));
y=CenterY2-(size.cy/2)-(int)((double)(CenterY2-20)*cos(Radians));
//绘制钟点
pDC->TextOut(x,y,str);
}
//计算时钟指示的夹角
Radians=(double)Time.GetHour()+(double)Time.GetMinute()/60.0+(double)Time.GetSecond()/3600.0;
Radians*=6.28/12.0;
//创建时钟指针画笔
CPen HourPen(PS_SOLID,1,RGB(0,255,0));
pDC->SelectObject(&HourPen);
//绘制时钟指针
pDC->MoveTo(CenterX1,CenterY1);
pDC->LineTo(CenterX1+(int)((double)(CenterX1/3)*sin(Radians)),CenterY1-(int)((double)(CenterY1/3)*cos(Radians)));
Radians=(double)Time.GetMinute()+(double)Time.GetSecond()/60.0;
Radians*=6.28/60.0;
//创建分钟指针画笔
CPen MinutePen(PS_SOLID,1,RGB(0,0,255));
pDC->SelectObject(&MinutePen);
//绘制分钟指针
pDC->MoveTo(CenterX1,CenterY1);
pDC->LineTo(CenterX1+(int)((double)(CenterX1*1/2)*sin(Radians)),CenterY1-(int)((double)(CenterY1*1/2)*cos(Radians)));
Radians=(double)Time.GetSecond();
Radians*=6.28/60.0;
//创建秒表指针画笔
CPen SecondPen(PS_SOLID,1,RGB(0,255,255));
pDC->SelectObject(&SecondPen);
//绘制秒表指针
pDC->MoveTo(CenterX1,CenterY1);
pDC->LineTo(CenterX1+(int)((double)(CenterX1*4/5)*sin(Radians)),CenterY1-(int)((double)(CenterY1*4/5)*cos(Radians)));
pDC->SelectObject(OldPen);
//反走样
//计算时钟指示的夹角
CPoint pt1, pt2;
COLORREF color1, color2, color3;
color1 = RGB(0,255,0);
color2 = RGB(0,0,255);
color3 = RGB(0,255,255);
Radians=(double)Time.GetHour()+(double)Time.GetMinute()/60.0+(double)Time.GetSecond()/3600.0;
Radians*=6.28/12.0;
pt1.x=CenterX2;
pt1.y=CenterY2;
pt2.x=CenterX2+(int)((double)(CenterX1/3)*sin(Radians));
pt2.y=CenterY2-(int)((double)(CenterY2/3)*cos(Radians));
WuLine(pDC,pt1,pt2,color1);
Radians=(double)Time.GetMinute()+(double)Time.GetSecond()/60.0;
Radians*=6.28/60.0;
pt2.x=CenterX2+(int)((double)(CenterX1*1/2)*sin(Radians));
pt2.y=CenterY2-(int)((double)(CenterY2*1/2)*cos(Radians));
WuLine(pDC,pt1,pt2,color2);
Radians=(double)Time.GetSecond();
Radians*=6.28/60.0;
pt2.x=CenterX2+(int)((double)(CenterX1*4/5)*sin(Radians));
pt2.y=CenterY2-(int)((double)(CenterY2*4/5)*cos(Radians));
WuLine(pDC,pt1,pt2,color3);
}
void CTest9View::WuLine(CDC * pDC,CPoint & pt1,CPoint & pt2,int color)
{
int deltax,deltay,start,finish;
double dx,dy,dydx; // fractional parts
BYTE LR,LG,LB;
int x1,x2,y1,y2;
double d1,d2;
x1=pt1.x;
y1=pt1.y;
x2=pt2.x;
y2=pt2.y;
deltax=abs(x2-x1); // Calculate deltax and deltay for initialisation
deltay=abs(y2-y1);
if((deltax==0)||(deltay==0))
{
pDC->MoveTo(x1,y1);
pDC->LineTo(x2,y2);
return;
}
LR=color&0x0000FF;
LG=(color&0x00FF00)>>8;
LB=(color&0xFF0000)>>16;
if(deltax>deltay) // then begin // horizontal or vertical
{
if(y2>y1) // determine rise and run
dydx=-((double)deltay/deltax);
else
dydx=(double)deltay/deltax;
if(x2<x1)
{
start=x2; // right to left
finish=x1;
dy=y2;
}
else
{
start=x1; // left to right
finish=x2;
dy=y1;
dydx=-dydx; // inverse slope
}
for(int x=start;x<=finish;x++)
{
d2=modf(dy,&d1);
AlphaBlendPixel(pDC,x,(int)d1,LR,LG,LB,1-d2);
AlphaBlendPixel(pDC,x,(int)d1+1,LR,LG,LB,d2);
dy=dy+dydx; // next point
}
}
else
{
if(x2>x1) // determine rise and run
dydx=-((double)deltax/deltay);
else
dydx=(double)deltax/deltay;
if(y2<y1)
{
start=y2; // right to left
finish=y1;
dx=x2;
}
else
{
start=y1; // left to right
finish=y2;
dx=x1;
dydx=-dydx; // inverse slope
}
for(int y=start;y<=finish;y++)
{
d2=modf(dx,&d1);
AlphaBlendPixel(pDC,(int)d1,y,LR,LG,LB,1-d2);
AlphaBlendPixel(pDC,(int)d1+1,y,LR,LG,LB,d2);
dx=dx+dydx;
}
}
}
void CTest9View::AlphaBlendPixel(CDC * pDC,int x,int y,BYTE R,BYTE G,BYTE B,double ratio)
{
double _ratio;
int color_old;
BYTE R1,G1,B1;
BYTE R2,G2,B2;
color_old=pDC->GetPixel(x,y);
R1=color_old&0x0000FF;
G1=(color_old&0x00FF00)>>8;
B1=(color_old&0xFF0000)>>16;
_ratio=1-ratio;
B2=(int)(B*ratio+B1*_ratio);
G2=(int)(G*ratio+G1*_ratio);
R2=(int)(R*ratio+R1*_ratio);
pDC->SetPixel(x,y,RGB(R2,G2,B2));
}
/////////////////////////////////////////////////////////////////////////////
// CTest9View printing
BOOL CTest9View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTest9View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTest9View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTest9View diagnostics
#ifdef _DEBUG
void CTest9View::AssertValid() const
{
CView::AssertValid();
}
void CTest9View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTest9Doc* CTest9View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTest9Doc)));
return (CTest9Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTest9View message handlers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -