纯代码绘制时钟

//
//  ViewController.m
//  time
//

//

#import "ViewController.h"


//获得当前的年月日 时分秒
#define  CURRENTSEC [[NSCalendar currentCalendar] component:NSCalendarUnitSecond fromDate:[NSDate date]]
#define  CURRENTMIN [[NSCalendar currentCalendar] component:NSCalendarUnitMinute fromDate:[NSDate date]]
#define  CURRENTHOUR [[NSCalendar currentCalendar] component:NSCalendarUnitHour fromDate:[NSDate date]]
#define  CURRENTDAY  [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:[NSDate date]]
#define  CURRENTMONTH [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:[NSDate date]]
#define  CURRENTYEAR [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]]

//角度转换成弧度
#define  ANGEL(x) x/180.0 * M_PI

#define kPerSecondA     ANGEL(6)
#define kPerMinuteA     ANGEL(6)
#define kPerHourA       ANGEL(30)
#define kPerHourMinuteA ANGEL(0.5)

@interface ViewController ()
@property (nonatomic,strong) UIImageView *imageClock;
@property (nonatomic, strong) UIView *clockView;

@property (nonatomic,strong) CALayer *layerSec;
@property (nonatomic,strong) CALayer *layerMin;
@property (nonatomic,strong) CALayer *layerHour;

@property (nonatomic,strong) CALayer *layerSec2;
@property (nonatomic,strong) CALayer *layerMin2;
@property (nonatomic,strong) CALayer *layerHour2;
@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    // Do any additional setup after loading the view, typically from a nib.

    [self.view addSubview:self.clockView];
    [self.clockView.layer addSublayer:self.layerSec];
    [self.clockView.layer addSublayer:self.layerMin];
    [self.clockView.layer addSublayer:self.layerHour];

    [self.view addSubview:self.imageClock];
    [self.imageClock.layer addSublayer:self.layerSec2];
    [self.imageClock.layer addSublayer:self.layerMin2];
    [self.imageClock.layer addSublayer:self.layerHour2];

    [self timeChange];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}

- (void)timeChange {
    self.layerSec.transform = CATransform3DMakeRotation(CURRENTSEC * kPerSecondA, 0, 0, 1);
    self.layerMin.transform = CATransform3DMakeRotation(CURRENTMIN * kPerMinuteA, 0, 0, 1);
//    self.layerHour.transform = CATransform3DMakeRotation(CURRENTHOUR * kPerHourA, 0, 0, 1);
    self.layerHour.transform = CATransform3DMakeRotation(CURRENTMIN * kPerHourMinuteA + CURRENTHOUR * kPerHourA, 0, 0, 1);

    self.layerSec2.transform = CATransform3DMakeRotation(CURRENTSEC * kPerSecondA, 0, 0, 1);
    self.layerMin2.transform = CATransform3DMakeRotation(CURRENTMIN * kPerMinuteA, 0, 0, 1);
//    self.layerHour2.transform = CATransform3DMakeRotation(CURRENTHOUR * kPerHourA, 0, 0, 1);
    self.layerHour2.transform = CATransform3DMakeRotation(CURRENTMIN * kPerHourMinuteA + CURRENTHOUR * kPerHourA, 0, 0, 1);
}

- (UIImageView *)imageClock {
    if (_imageClock == nil) {
        _imageClock = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"钟表"]];
        _imageClock.frame = CGRectMake(100, 320, 200, 200);
        _imageClock.layer.cornerRadius = 45;
        _imageClock.layer.masksToBounds = YES;
    }
    return _imageClock;
}

- (CALayer *)layerSec {
    if (_layerSec == nil) {
        _layerSec = [CALayer layer];
        _layerSec.bounds = CGRectMake(0, 0, 2, 100);
        _layerSec.backgroundColor = [UIColor redColor].CGColor;
        _layerSec.cornerRadius = 5;
        _layerSec.anchorPoint = CGPointMake(0.5, 0.9);// 固定点
        _layerSec.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);
    }
    return _layerSec;
}

- (CALayer *)layerMin {
    if (_layerMin == nil) {
        _layerMin = [CALayer layer];
        _layerMin.bounds = CGRectMake(0, 0, 4, 70);
        _layerMin.backgroundColor = [UIColor blackColor].CGColor;
        _layerMin.cornerRadius = 5;
        _layerMin.anchorPoint = CGPointMake(0.5, 0.95);
        _layerMin.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);
    }
    return _layerMin;
}

- (CALayer *)layerHour {
    if (_layerHour == nil) {
        _layerHour = [CALayer layer];
        _layerHour.bounds = CGRectMake(0, 0, 6, 50);
        _layerHour.backgroundColor = [UIColor blackColor].CGColor;
        _layerHour.cornerRadius = 5;
        _layerHour.anchorPoint = CGPointMake(0.5, 0.95);
        _layerHour.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);
    }
    return _layerHour;
}

- (CALayer *)layerSec2 {
    if (_layerSec2 == nil) {
        _layerSec2 = [CALayer layer];
        _layerSec2.bounds = CGRectMake(0, 0, 2, 100);
        _layerSec2.backgroundColor = [UIColor redColor].CGColor;
        _layerSec2.cornerRadius = 5;
        _layerSec2.anchorPoint = CGPointMake(0.5, 0.9);// 固定点
        _layerSec2.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);
    }
    return _layerSec2;
}

- (CALayer *)layerMin2 {
    if (_layerMin2 == nil) {
        _layerMin2 = [CALayer layer];
        _layerMin2.bounds = CGRectMake(0, 0, 4, 70);
        _layerMin2.backgroundColor = [UIColor blackColor].CGColor;
        _layerMin2.cornerRadius = 5;
        _layerMin2.anchorPoint = CGPointMake(0.5, 0.95);
        _layerMin2.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);
    }
    return _layerMin2;
}

- (CALayer *)layerHour2 {
    if (_layerHour2 == nil) {
        _layerHour2 = [CALayer layer];
        _layerHour2.bounds = CGRectMake(0, 0, 6, 50);
        _layerHour2.backgroundColor = [UIColor blackColor].CGColor;
        _layerHour2.cornerRadius = 5;
        _layerHour2.anchorPoint = CGPointMake(0.5, 0.95);
        _layerHour2.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);
    }
    return _layerHour2;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)clockView {
    if (_clockView == nil) {
        _clockView = [[UIView alloc] initWithFrame:CGRectMake(100, 80, 200, 200)];
        _clockView.backgroundColor = [UIColor whiteColor];
        _clockView.layer.cornerRadius = 100;
        _clockView.layer.masksToBounds = YES;
        [self create];

    }
    return _clockView;
}

- (void)create {
    for (int i = 1; i <= 60; i++) {
        CALayer *layer = [CALayer layer];
        CGFloat height;

        if (i % 5 == 0) {

            int j = (int)(i / 5);
            height = 10;
            layer.anchorPoint = CGPointMake(0.5, 10);// 固定点

            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 15, 10)];
            label.textColor = [UIColor blackColor];
            label.text = [NSString stringWithFormat:@"%d", j];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:12];
            label.layer.position = CGPointMake(_clockView.bounds.size.width/2, _clockView.bounds.size.height/2);
            label.layer.transform = CATransform3DMakeRotation(i * kPerSecondA, 0, 0, 1);
            label.layer.anchorPoint = CGPointMake(0.5, 8.5);
            [_clockView addSubview:label];
        } else {
            height = 5;
            layer.anchorPoint = CGPointMake(0.5, 20);// 固定点
        }
        layer.bounds = CGRectMake(0, 0, 4, height);
        layer.backgroundColor = [UIColor blackColor].CGColor;

        layer.position = CGPointMake(_clockView.bounds.size.width/2, _clockView.bounds.size.height/2);
        layer.transform = CATransform3DMakeRotation(i * kPerSecondA, 0, 0, 1);
        [_clockView.layer addSublayer:layer];
    }
}

@end

 

转载于:https://my.oschina.net/zsyzone/blog/704231

Imports System.ComponentModel _ Public Class DSButton Private _ButtonColor As Color = Color.White Private SF As New System.Drawing.StringFormat Private _Text As String Public Property ButtonColor As Color Get Return _ButtonColor End Get Set(ByVal value As Color) _ButtonColor = value MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Set End Property Public Property RoundRectValue As Integer = 10 Private nIndex As Integer = 0 Private IsMouseEnter As Boolean = False Public Property IsShowAnimate As Boolean = False Public Property ButtonText As String Get Return _Text End Get Set(ByVal value As String) _Text = value MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Set End Property Private _TextColor As Color = Color.White Public Property TextColor As Color Get Return _TextColor End Get Set(ByVal value As Color) _TextColor = value MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Set End Property Private Sub DSButton_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SetStyle(ControlStyles.UserPaint, True) SetStyle(ControlStyles.AllPaintingInWmPaint, True) SetStyle(ControlStyles.ResizeRedraw, True) SetStyle(ControlStyles.Selectable, True) SF.LineAlignment = StringAlignment.Center SF.Alignment = StringAlignment.Center MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Sub Private Sub MakeRoundedRect(ByVal Rounded As Integer, ByVal Ct As Control, ByVal ButtonColor As Color) If Ct.BackgroundImage IsNot Nothing Then Ct.BackgroundImage.Dispose() Ct.BackgroundImage = New Bitmap(Ct.Width, Ct.Height) Dim WW, HH As Integer WW = Ct.Width - 1 HH = Ct.Height - 1 Using G As Graphics = Graphics.FromImage(Ct.BackgroundImage) G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit Using Gp As New Drawing2D.GraphicsPath Gp.AddArc(New Rectangle(0, 0, Rounded, Rounded), 180, 90) Gp.AddArc(New Rectangle(WW - Rounded, 0, Rounded, Rounded), -90, 90) Gp.AddArc(New Rectangle(WW - Rounded, HH - Rounded, Rounded, Rounded), 0, 90) Gp.AddArc(New Rectangle(0, HH - Rounded, Rounded, Rounded), 90, 90) Gp.AddLine(New Point(0, HH - Rounded), New Point(0, Rounded / 2)) Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, HH), ControlPaint.Dark(ButtonColor, 0.5), ButtonColor) G.FillPath(Lg, Gp) G.DrawPath(Pens.Black, Gp) End Using End Using WW = WW - 3 HH = HH - 3 Using Gp As New Drawing2D.GraphicsPath Gp.AddArc(New Rectangle(3, 3, Rounded, Rounded), 180, 90) Gp.AddArc(New Rectangle(WW - Rounded, 3, Rounded, Rounded), -90, 90) Gp.AddArc(New Rectangle(WW - Rounded, HH / 2 - Rounded - 1, Rounded, Rounded), 0, 90) Gp.AddArc(New Rectangle(3, HH / 2 - Rounded - 1, Rounded, Rounded), 90, 90) Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, HH / 2), Color.FromArgb(220, 255, 255, 255), Color.FromArgb(50, 255, 255, 255)) G.FillPath(Lg, Gp) End Using End Using Using Gp As New Drawing2D.GraphicsPath Gp.AddEllipse(New Rectangle(3, HH / 2 + 10, WW, HH / 2)) Using Lg As New Drawing2D.PathGradientBrush(Gp) Lg.CenterColor = Color.FromArgb(150, 255, 255, 255) Lg.SurroundColors = New Color() {Color.Transparent} Gp.FillMode = Drawing2D.FillMode.Winding G.FillPath(Lg, Gp) End Using End Using Try If _Text.Length 0 Then G.DrawString(_Text, Me.Font, New SolidBrush(TextColor), New Rectangle(0, 0, Me.Width, Me.Height), SF) Catch End Try End Using End Sub Private Sub DSButton_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick End Sub Private Sub DSButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown If e.Button = MouseButtons.Left Then MakeRoundedRect(RoundRectValue, Me, Color.Black) End If End Sub Private Sub DSButton_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter If DesignMode = False Then IsMouseEnter = True Timer1.Enabled = True End If End Sub Private Sub DSButton_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave If DesignMode = False Then IsMouseEnter = False Timer1.Enabled = True End If End Sub Private Sub DSButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp MakeRoundedRect(RoundRectValue, Me, _ButtonColor) End Sub Private Sub DSButton_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged If Me.IsHandleCreated Then MakeRoundedRect(RoundRectValue, Me, ButtonColor) End If End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Select Case IsMouseEnter Case True If IsShowAnimate = True Then nIndex = IIf(nIndex + 30 >= 225, 255, nIndex + 30) If nIndex >= 255 Then Timer1.Enabled = False Else nIndex = 255 Timer1.Enabled = False End If Case False nIndex = IIf(nIndex - 20 <= 50, 50, nIndex - 20) If nIndex <= 50 Then Timer1.Enabled = False End Select Try MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * nIndex, ButtonColor.G / 255 * nIndex, ButtonColor.B / 255 * nIndex)) Catch End Try End Sub End Class
用MFC VC++实现的时钟代码 // MFCFrame1View.cpp : implementation of the CMFCFrame1View class // #include "stdafx.h" #include "MFCFrame1.h" #include "MFCFrame1Doc.h" #include "MFCFrame1View.h" #include "PointDialog.h" #include "math.h" GLUquadricObj *objCylinder = gluNewQuadric(); #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View IMPLEMENT_DYNCREATE(CMFCFrame1View, CView) BEGIN_MESSAGE_MAP(CMFCFrame1View, CView) //{{AFX_MSG_MAP(CMFCFrame1View) ON_WM_CREATE() ON_WM_DESTROY() ON_WM_SIZE() ON_COMMAND(IDM_ZIXUAN, OnZixuan) ON_WM_TIMER() ON_COMMAND(IDM_ChangDirect, OnChangDirect) //}}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() ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View construction/destruction CMFCFrame1View::CMFCFrame1View() { // TODO: add construction code here this->m_GLPixelIndex = 0; this->m_hGLContext = NULL; Angle1=0.0; Angle2=30.0; Timer=0; x=0.0; z=0.0; juli=40.0; } CMFCFrame1View::~CMFCFrame1View() { } BOOL CMFCFrame1View::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS); return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View drawing ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View printing BOOL CMFCFrame1View::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMFCFrame1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMFCFrame1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View diagnostics #ifdef _DEBUG void CMFCFrame1View::AssertValid() const { CView::AssertValid(); } void CMFCFrame1View::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMFCFrame1Doc* CMFCFrame1View::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCFrame1Doc))); return (CMFCFrame1Doc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View message handlers BOOL CMFCFrame1View::SetWindowPixelFormat(HDC hDC) { PIXELFORMATDESCRIPTOR pixelDesc= { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL| PFD_DOUBLEBUFFER|PFD_SUPPORT_GDI, PFD_TYPE_RGBA, 24, 0,0,0,0,0,0, 0, 0, 0, 0,0,0,0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 }; this->m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc); if(this->m_GLPixelIndex==0) { this->m_GLPixelIndex = 1; if(DescribePixelFormat(hDC,this->m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0) { return FALSE; } } if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE) { return FALSE; } return TRUE; } int CMFCFrame1View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here HWND hWnd = this->GetSafeHwnd(); HDC hDC = ::GetDC(hWnd); if(this->SetWindowPixelFormat(hDC)==FALSE) { return 0; } if(this->CreateViewGLContext(hDC)==FALSE) { return 0; } return 0; } BOOL CMFCFrame1View::CreateViewGLContext(HDC hDC) { this->m_hGLContext = wglCreateContext(hDC); if(this->m_hGLContext==NULL) {//创建失败 return FALSE; } if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE) {//选为当前RC失败 return FALSE; } return TRUE; } void CMFCFrame1View::OnDestroy() { CView::OnDestroy(); // TODO: Add your message handler code here if(wglGetCurrentContext()!=NULL) { wglMakeCurrent(NULL,NULL); } if(this->m_hGLContext!=NULL) { wglDeleteContext(this->m_hGLContext); this->m_hGLContext = NULL; } } void CMFCFrame1View::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); // TODO: Add your message handler code here GLsizei width,height; GLdouble aspect; width = cx; height = cy; if(cy==0) { aspect = (GLdouble)width; } else { aspect = (GLdouble)width/(GLdouble)height; } glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40.0,aspect,5.0,1000.0); } void CMFCFrame1View::OnDraw(CDC* pDC) { CMFCFrame1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here CPaintDC dc(this); glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,10.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotatef(-90.0,1.0,0.0,0.0);/*返回原坐标*/ glTranslatef(-3.0,0.0,0.0); SwapBuffers(dc.m_ps.hdc); glDrawBuffer (GL_BACK); glFlush(); } void CMFCFrame1View::OnZixuan() { // TODO: Add your command handler code here Timer=1; SetTimer(1,100,NULL); } void CMFCFrame1View::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default CPaintDC dc(this); if (Timer==1) { Angle1=Angle1-1; Angle2=Angle2-1; glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW);/**/ glLoadIdentity(); gluLookAt(0.0,20.0,0.000000001,0.0,0.0,0.0,0.0,1.0,0.0); glPushMatrix(); glColor3f(0.7,0.7,0.7); glTranslatef(0.0,0.0,1.1); glRotatef(-90.0,1.0,0.0,0.0); glutSolidCone(5.0,0.0,60.0,60.0); /*底盘*/ glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(3.9,0.99,1.0); /*刻度*/ glRotatef(90.0,0.0,1.0,0.0); gluCylinder(objCylinder, 0.05, 0.05, 0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(-4.7,0.99,1.0); /*刻度*/ glRotatef(90.0,0.0,1.0,0.0); gluCylinder(objCylinder,0.05, 0.05, 0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(0.0,0.99,-2.9); /*刻度*/ glRotatef(180.0,0.0,1.0,0.0); gluCylinder(objCylinder,0.05, 0.05, 0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(0.0,0.99,5.8); /*刻度*/ glRotatef(180.0,0.0,1.0,0.0); gluCylinder(objCylinder,0.05, 0.05,0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,1.0,0.0); glRotatef(45.0,0.0,1.0,0.0); glTranslatef(-0.67,0.99,0.7); /*时针*/ glRotatef(Angle1/129600,0.0,1.0,0.0); gluCylinder(objCylinder, 0.07, 0.02, 2.5, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,0.0); glTranslatef(0.0,0.99,1.0); /*分针*/ glRotatef(Angle2/360,0.0,1.0,0.0); gluCylinder(objCylinder, 0.05, 0.02, 3.5, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.5); glTranslatef(0.0,0.99,1.0); /*秒针*/ glRotatef(Angle1,0.0,1.0,0.0); gluCylinder(objCylinder, 0.07, 0.02, 4.5, 9999, 9); glPopMatrix(); SwapBuffers(dc.m_ps.hdc); glDrawBuffer (GL_BACK); glFlush(); } else if(Timer==2) { glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); if (juli>12.0) { glMatrixMode(GL_MODELVIEW);/*建立了从世界坐标系到观察坐标系的转换矩阵*/ glLoadIdentity(); gluLookAt(0.0,8.0,juli,0.0,0.0,0.0,0.0,1.0,0.0); juli=juli-0.1; glPushMatrix(); glColor3f(0.0,0.0,0.0); glRotatef(-90.0,1.0,0.0,0.0); glutWireCone(40.0,0.0,30.0,30.0); /*画高度为0的圆锥*/ glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glLineWidth(4.0); glTranslatef(4.0,1.0,0.0); glutWireOctahedron(); /*画八面体*/ glLineWidth(1.0); glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glTranslatef(0.0,1.1,0.0); glRotatef(Angle2,0.0,1.0,0.0); gluCylinder(objCylinder, 1.0, 1.0, 10.0, 9999, 9); /*画壶*/ glPopMatrix(); } else if(juli<=12.0) { Angle2=Angle2+0.01; if (Angle2==360.0) Angle2=0.0; glMatrixMode(GL_MODELVIEW);/*建立了从世界坐标系到观察坐标系的转换矩阵*/ glLoadIdentity(); x=12.0*sin(Angle2); z=12.0*cos(Angle2); gluLookAt(x,5.0,z,0.0,0.0,0.0,0.0,1.0,0.0); glPushMatrix(); glColor3f(0.0,0.0,0.0); glRotatef(-90.0,1.0,0.0,0.0); glutWireCone(40.0,0.0,30.0,30.0); /*画高度为0的圆锥*/ glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glLineWidth(4.0); glTranslatef(4.0,1.0,0.0); glutWireOctahedron(); /*画八面体*/ glLineWidth(1.0); glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glTranslatef(0.0,1.1,0.0); gluCylinder(objCylinder, 1.0, 1.0, 10.0, 9999, 9); /*画壶*/ glPopMatrix(); } SwapBuffers(dc.m_ps.hdc); glDrawBuffer (GL_BACK); glFlush(); } CView::OnTimer(nIDEvent); } void CMFCFrame1View::OnChangDirect() { // TODO: Add your command handler code here Timer=2; SetTimer(1,100 ,NULL); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值