用VB制作指针时钟。要详细的步骤和控件名称

用VB制作指针时钟。要详细的步骤和控件名称,第1张

窗体上加一个timer1,intervar设为1000(毫秒);加3个line,3个line的x1、y1相同;一个shape控件,shape属性设为3,圆心为x1,y1,把下面代码拷贝到窗体代码中,运行即可

如果觉得可以的话,加点分哈└(^o^)┘

Const pi = 314159

Dim len1, len2, len3 As Single

Private Sub Form_Load()

len1 = Sqr((Abs((Line1Y2 - Line1Y1))) ^ 2 + (Abs(Line1X2 - Line1X1)) ^ 2)

len2 = Sqr((Abs((Line2Y2 - Line2Y1))) ^ 2 + (Abs(Line2X2 - Line2X1)) ^ 2)

len3 = Sqr((Abs((Line3Y2 - Line3Y1))) ^ 2 + (Abs(Line3X2 - Line3X1)) ^ 2)

End Sub

Private Sub timer1_timer()

s = Second(Time)

m = Minute(Time)

h = Hour(Time) + m / 60

Line1X2 = Line1X1 + len1 Sin(pi s / 30)

Line1Y2 = Line1Y1 - len1 Cos(pi s / 30)

Line2X2 = Line2X1 + len2 Sin(pi m / 30)

Line2Y2 = Line2Y1 - len2 Cos(pi m / 30)

If h >= 12 Then h = h - 12

Line3X2 = Line3X1 + len3 Sin(pi h / 6)

Line3Y2 = Line3Y1 - len3 Cos(pi h / 6)

End Sub

我最近刚刚写出了能运行的代码,但是MFC的代码分布太分散,除了在CViewcpp里面,其他的源文件里也有,没办法在这里全贴出来。如果你想要全部文件的话,可以给我留个邮箱或者聊天账号之类的****。

我这里只能给你简单说一说我的思路。把客户区分成逻辑的两个区域,在左侧区域调用pDC->Ellipse()画出圆形,求出圆心坐标和半径,然后用三角函数乘以半径来定出三根表针的长度,画表针的时候定义三个不同的CPen对象,像素和RGB颜色设置不同就可以了。再调用pDC->MoveTo()和pDC->LineTo()把它们画出来。至于你说的文字和电子表上的示数,可以用pDC->TextOut()来实现。注意给它指定恰当的位置。

这里是我运行的结果。不知道是否符合你的要求。

Hand类的代码:

Public MustInherit Class Hand

Protected gp As GraphicsPath = New GraphicsPath()

Protected gpBase As GraphicsPath = Nothing

Protected midX As Integer = 150 ‘默认的窗体

Protected midY As Integer = 150 ‘中心位置

‘构造器,得到窗体中心位置

Public Sub New(ByVal theForm As Form1)

midX = (theFormClientRectangleLeft + theFormClientRectangleRight) / 2

midY = (theFormClientRectangleTop + theFormClientRectangleBottom) / 2

End Sub

MustOverride Sub Transform(ByVal d As DateTime)

‘绘制指针路径

Overridable Sub Draw(ByVal g As Graphics)

Dim aPen As Pen = New Pen(BrushesBlack, 4F)

gDrawPath(aPen, gp)

gFillPath(BrushesBlack, gp)

aPenDispose()

End Sub

‘使用矩阵实现路径(gp)的旋转

Public Sub Rotate(ByVal angle As Double)

gp = CType(gpBaseClone(), GraphicsPath)

Dim mTransform As Matrix = New Matrix()

mTransformRotateAt(CType(angle,Single),NewPointF(midX,midY))

gpTransform(mTransform)

End Sub

End Class

为了节省篇幅,上面的代码省略了引入命名空间的语句。

下面是分针(MinuteHand)类的定义:

Public Class MinuteHand

Inherits Hand

‘构造器,生成绘制分针的路径(gp)

Public Sub New(ByVal myForm As Form1)

MyBaseNew(myForm)

gpAddLine(midX, midY, midX, 45)

gpAddLine(midX, 45, midX - 3, 50)

gpAddLine(midX - 3, 50, midX + 3, 50)

gpAddLine(midX + 3, 50, midX, 45)

gpBase = CType(gpClone(), GraphicsPath)

End Sub

‘Transform方法取得系统当前时间,并旋转时钟指针。

Public Overrides Sub Transform(ByVal d As DateTime)

Dim minuteTime As Double = (CDbl(dMinute) + CDbl(dSecond / 60))

Dim angle As Double = (CDbl(minuteTime) / 60) 360

Rotate(angle)

End Sub

End Class

对所有的指针旋转的方法都是相同的,因此在基类中实现。由于时针和秒针的实现与分针相似,所不同者,只在于构造器中绘制的指针路径不同和Transform方法中转动的角度不同,在这里就不在赘述了。

另外还需要提一下的是画时钟表面的代码,时钟表面用ClockFace类来实现。这个类首先画一个圆代表时钟,然后画上米老鼠的图案,最后在相应的位置画上数字1~12代表12个小时。

Public Sub Draw(ByVal g As Graphics)

DrawClockFace(g)

DrawImage(g)

DrawNumbers(g)

DrawPin(g)

End Sub

下面是ClockFace类的属性:

Private ClockRectangle As Rectangle

Private ClockFont As Font = New Font("Arial", 12)

Private midPoint As Point

Private ClockImage As Bitmap

Private Const IMAGEX As Integer = 50

Private Const IMAGEY As Integer = 50

DrawClockFace方法用来画时钟表面:

Private Sub DrawClockFace(ByVal g As Graphics)

gFillEllipse(BrushesWhite, ClockRectangleLeft + 10, ClockRectangleTop + 10, ClockRectangleWidth - 20, ClockRectangleHeight - 20)

gDrawEllipse(PensBlack, ClockRectangleLeft + 10, ClockRectangleTop + 10, ClockRectangleWidth - 20, ClockRectangleHeight - 20)

End Sub

然后用Graphics对象的DrawImage方法画出米老鼠的:

Private Sub DrawImage(ByVal g As Graphics)

Dim nWidth As Integer = ClockImageWidth

Dim nHeight As Integer = ClockImageHeight

Dim destRect As Rectangle = New Rectangle(midPointX - IMAGEX / 2, midPointY - IMAGEY / 2, IMAGEX, IMAGEY)

gDrawImage(ClockImage, destRect)

End Sub

数字在时钟上的位置是用sin和cos函数计算的:

Private Sub DrawNumbers(ByVal g As Graphics)

Dim count As Integer = 1

Dim a As Double

For a = 0 To 2 MathPI Step 2 MathPI / 12

Dim x As Double = (ClockRectangleWidth - 70) / 2 MathCos(a - MathPI / 3) + (ClockRectangleWidth - 70) / 2 + 25

Dim y As Double = (ClockRectangleWidth - 70) / 2 MathSin(a - MathPI / 3) + (ClockRectangleWidth - 70) / 2 + 20

gDrawString(ConvertToString(count), ClockFont, BrushesBlack, CType(x, Single), CType(y, Single), New StringFormat())

count += 1

Next

End Sub

最后是窗体文件(Form1vb):

Public Class Form1

Inherits SystemWindowsFormsForm

Private MyMinuteHand As MinuteHand

Private MyHourHand As HourHand

Private MySecondHand As SecondHand

Private TheClockFace As ClockFace

Private FirstTick As Boolean = False

‘在窗体的OnPaint事件中取得Graphics对象

Protected Overrides Sub OnPaint(ByVal e As SystemWindowsFormsPaintEventArgs)

If (FirstTick = False) Then Exit Sub

Dim g As Graphics = eGraphics

TheClockFaceDraw(g)

MyHourHandDraw(g)

MyMinuteHandDraw(g)

MySecondHandDraw(g)

TheClockFaceDrawPin(g)

End Sub

‘计时器事件

Private Sub Timer1_Tick(ByVal sender As SystemObject, ByVal e As SystemEventArgs) Handles Timer1Tick

MySecondHandTransform(DateTimeNow)

MyHourHandTransform(DateTimeNow)

MyMinuteHandTransform(DateTimeNow)

FirstTick = True

Invalidate()

这里分享下matlab时钟指针怎么转的方法。

设备:华硕笔记本

系统:win10

软件:matlab2019

1、首先在电脑中打开matlab2脚本编辑器,如下图所示。

2、然后利用magic函数生成魔方三维矩阵。

3、然后分别利用duration将数字转化为时间以及利用char函数生成数字时钟样式。

4、最后点击运行程序,即可显示3行的数字时钟样式的字符。

电脑调整时间的方法是:

1、用鼠标右键点击电脑屏幕右下角的时间区域。

2、打开时间窗口后,再点击下方的更改时间区域和时间几个字。

3、此时会出现调整时间的窗口,把鼠标光标分别移动到时、分、秒上,然后用升降键调整具体时间。

4、点击应用、确定退出。

以上就是关于用VB制作指针时钟。要详细的步骤和控件名称全部的内容,包括:用VB制作指针时钟。要详细的步骤和控件名称、如何用vc++编写一个模拟时钟的程序,要求指针大小宽度颜色不一,旁边附加一个电子表、vb.net开发简单的时钟程序高手救救我!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9855319.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存