桌面时钟

it2025-03-12  24

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

在本篇博文中将向读者展现如何实现一个如下图所示的背景透明的桌面时钟 。

1、窗体和控件

窗体高度和宽度都为200 ,TopMost=True;FormBorderStyle=None 放置的控件: picClock:Picturebox控件,Dock=Fill。 ContextMenuStrip1:ContextMenuStrip组件,菜单项分别为:图片一、图片二、图片三、退出。 tmClock:Timer组件,Interval =100。

2、设置窗体透明: 通过TransparencyKey 属性设置窗体需要透明的颜色 通过BackColor属性设置背景颜色

3、时钟刻度和指针位置: 这里需要一些简单的三角函数知识,同时需要注意VB.Net的坐标系,x轴向右,y轴向下。

4、资源文件的使用 时钟中心图片可以替换,用于替换的图片保存在资源文件中,名称为png1、png2、png3。

5、Timer组件的间隔 这里是设置为了100毫秒,为什么不设置为1秒,因为经过测试,可能会出现Timer的1秒刚刚开始,恰好真实事件的1秒刚好结束的情况,也就是看起来时钟比真实时间差1秒。所以选择了100毫秒。

6、时钟位置 由于窗体设置了无边框,所以必须加入相应代码实现窗体的拖动,当鼠标左键按下时,记录当前鼠标位置;当鼠标移动并且鼠标左键按下时,记录当前位置,并与之前的位置做对比,从而得到窗体的位置。

7、详细代码

Dim g As Graphics '时钟圆形边缘半径 Dim r As Integer '时钟中央图片编号 Dim centerImgId As Integer '按下鼠标左键时鼠标位置 Dim mousePoint As Point Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load '设置窗体需要透明的颜色 Me.TransparencyKey = Color.White '设置背景颜色 Me.BackColor = Color.White '绘图表面 g = picClock.CreateGraphics '时钟圆形边缘半径 r = picClock.Height / 2 - 20 '初始显示图片为1号图片 centerImgId = 1 '启动计时器 tmClock.Start() End Sub '绘制中心图片 Private Sub drawClockImg() Dim bmp As Bitmap Select Case centerImgId Case 1 bmp = My.Resources.Resource1.png1 Case 2 bmp = My.Resources.Resource1.png2 Case 3 bmp = My.Resources.Resource1.png3 End Select '圆心坐标 Dim center As Point = New Point(picClock.Width / 2, picClock.Height / 2) g.DrawImage(bmp, New Point(center.X - bmp.Width / 2, center.Y - bmp.Height / 2)) End Sub '绘制时钟圆盘 Private Sub drawClockCircle() Dim myPen As New Pen(New SolidBrush(Color.Red), 3) g.DrawEllipse(myPen, New Rectangle(10, 10, picClock.Width - 20, picClock.Height - 20)) End Sub '绘制时钟刻度 Private Sub drawClockGrid() Dim pos As Point Dim angle As Integer Dim rGrid As Integer rGrid = r - 5 For i As Integer = 0 To 59 Step 5 angle = 180 - Math.Truncate(i * 360 / 60) pos.X = CType(rGrid * Math.Sin(angle * Math.PI / 180), Integer) pos.Y = CType(rGrid * Math.Cos(angle * Math.PI / 180), Integer) g.FillEllipse(New SolidBrush(Color.Blue), New Rectangle(picClock.Width / 2 + pos.X - 2, picClock.Height / 2 + pos.Y - 2, 4, 4)) Next End Sub '绘制时钟指针 Private Sub drawClockHand(ByVal nowTime As DateTime) Dim H As Integer = (nowTime.Hour - 12) * 5 Dim M As Integer = nowTime.Minute Dim S As Integer = nowTime.Second Dim Hr As Integer = r - 40 Dim Mr As Integer = r - 20 Dim Sr As Integer = r Dim Hpen As New Pen(New SolidBrush(Color.Red), 2) Dim Mpen As New Pen(New SolidBrush(Color.Blue), 2) Dim Spen As New Pen(New SolidBrush(Color.Red), 1) '圆心坐标 Dim center As Point = New Point(picClock.Width / 2, picClock.Height / 2) Dim Hpos As PointF = getPos(Hr, H) g.DrawLine(Hpen, center, New Point(center.X + Hpos.X, center.Y + Hpos.Y)) Dim Mpos As PointF = getPos(Mr, M) g.DrawLine(Mpen, center, New Point(center.X + Mpos.X, center.Y + Mpos.Y)) Dim Spos As PointF = getPos(Sr, S) g.DrawLine(Spen, center, New Point(center.X + Spos.X, center.Y + Spos.Y)) End Sub ''' <summary> ''' 求出指针末端坐标 ''' </summary> ''' <param name="radius">指针半径</param> ''' <param name="HandValue">指针所对值</param> ''' <returns></returns> Private Function getPos(ByVal radius As Integer, HandValue As Integer) As PointF Dim angle As Integer angle = 180 - Math.Truncate(HandValue * 360 / 60) Dim pos As PointF pos.X = radius * Math.Sin(angle * Math.PI / 180) pos.Y = radius * Math.Cos(angle * Math.PI / 180) Return pos End Function Private Sub tmClock_Tick(sender As Object, e As EventArgs) Handles tmClock.Tick g.Clear(Color.White) '绘制中心图片 Call drawClockImg() '绘制时钟圆盘 Call drawClockCircle() '绘制时钟刻度 Call drawClockGrid() '绘制时钟指针 Call drawClockHand(Now) End Sub Private Sub 图片一ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 图片一ToolStripMenuItem.Click centerImgId = 1 End Sub Private Sub 图片二ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 图片二ToolStripMenuItem.Click centerImgId = 2 End Sub Private Sub 图片三ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 图片三ToolStripMenuItem.Click centerImgId = 3 End Sub Private Sub 退出ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 退出ToolStripMenuItem.Click Application.Exit() End Sub Private Sub picClock_MouseDown(sender As Object, e As MouseEventArgs) Handles picClock.MouseDown If e.Button = MouseButtons.Left Then mousePoint = New Point(e.X, e.Y) End If End Sub Private Sub picClock_MouseMove(sender As Object, e As MouseEventArgs) Handles picClock.MouseMove If e.Button = MouseButtons.Left Then Me.Left -= mousePoint.X - e.X Me.Top -= mousePoint.Y - e.Y End If End Sub

 

最新回复(0)