版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
本篇博文主要讲述如何用VB.Net实现桌面人偶程序。
一、准备工作
1.1、人偶图片的准备
本文中将使用到两张人物行走的png图片,均来源于网络。
图1、男性图片
图2、女性图片
图片大小为280*992,实际包含32张人物行走动作的图片,从上到下分别为:
向下行走、向左行走、向右行走、向上行走、左下行走、右下行走、左上行走、右上行走
共8个方向的行走动作图。
根据图片内容可以均分为4*8区域,每个区域对应大小为70*124。
1.2、窗体和控件设置
打开VB.Net,新建工程,工程名称为Actor,窗体名称为FormMain。
根据1.1节分析的内容,设置FormMain的高宽属性为70*124;
为使人偶显示在所有窗体最上面,设置TopMost属性为True;
为使人偶窗体不显示系统多拽栏、系统菜单(包含关闭按钮),设置FormBorderStyle 为None;
窗体出现位置为屏幕中央,设置StartPosition为CenterScreen。
在窗体上添加快捷菜单,名称为ContextMenuStrip1。
菜单项如下图:
图3、快捷菜单菜单项
作用分别是调节人偶走动速度、设置人偶性别、退出程序。
在窗体上放置一个图片框,名称为picActor。
picActor的高宽属性为70*124,和窗体边框重合;
picActor的ContextMenuStrip 属性设置为ContextMenuStrip1,确保在图片上点击鼠标右键出来快捷菜单
在窗体上放置一个Timer组件,名称为tmActor。
tmActor时间间隔Interval 属性为150。
tmActor的作用是每隔一段时间控制人偶移动一定距离。
1.3、使用资源文件
为了方便调用人偶图片,向工程添加资源文件Resource1.resx,将1.1节中的两张人物行走的图片添加到资源文件中,分别命名为f1和m1。
图4、资源文件中包含的图片
二、动起来吧,小可爱
在代码中已经添加了详细注释,这里不再作说明。
1、人偶类
'人偶类
Public Class clsActor'方向Public Enum Directiondown = 0leftrightupdownleftdownrightupleftuprightEnd Enum'人偶的宽度和高度Const Width As Integer = 70Const Height As Integer = 124'当前方向Property currentdirection As Direction'当前动作Property currentAct As Integer'数组用以保存拆分的图像,包含每张人偶行走动作的图像'8个方向,每个方向4张图像,一共32张图像Private bmpPerson(31) As Bitmap'参数为资源中的人偶图片Public Sub New(ByVal bmpActor As Bitmap)'当前动作为第一张图片currentAct = 0Call loadActor(bmpActor)End Sub'载入人偶图像,将图像拆分到数组中Public Sub loadActor(ByVal bmpActor As Bitmap)'bmp是资源中的图像,它是一个固定宽高的图像,宽280、高992Dim bmp As Bitmap'按照4*8区域,将资源图片中的每一个行走动作图保存到图像数组中For i As Integer = 0 To 7For j As Integer = 0 To 3bmp = New Bitmap(Width, Height)Dim g As Graphics = Graphics.FromImage(bmp)g.DrawImage(bmpActor, 0, 0, New Rectangle(70 * j, 124 * i, Width, Height), GraphicsUnit.Pixel)bmpPerson(i * 4 + j) = bmpg.Dispose()NextNextEnd Sub'人偶移动Public Sub moveActor(ByVal g As Graphics)'使用RGB(255, 255, 254)清除之前的graphics'注意:使用的颜色应和窗体透明索引(TransparencyKey)一致,确保背景透明g.Clear(Color.FromArgb(255, 255, 254))'绘制行走的图像g.DrawImage(bmpPerson(currentdirection * 4 + currentAct), New Point(0, 0))'设置为下一个动作currentAct += 1If currentAct = 4 Then currentAct = 0End Sub
End Class
2、窗体上的代码
Public Class FormMainDim actor As clsActor'图片数组,用于保存资源文件中的图片Dim actorImg(1) As Bitmap'上下左右直线运动时的位移量Dim moveStraightLine As Integer = 10'斜向运动时的x、y方向上的位移量Dim moveObliqueLine As Integer = 7Dim screenWidth As IntegerDim screenHeight As Integer'用于产生随机数Dim rnd As Random'按下鼠标左键时鼠标位置Dim mousePoint As PointPrivate Sub FormMain_Load(sender As Object, e As EventArgs) Handles Me.LoadMe.Width = 70Me.Height = 124'设置窗体需要透明的颜色Me.TransparencyKey = Color.FromArgb(255, 255, 254)'设置背景颜色Me.BackColor = Color.FromArgb(255, 255, 254)'获得屏幕宽度和高度screenWidth = Screen.PrimaryScreen.WorkingArea.WidthscreenHeight = Screen.PrimaryScreen.WorkingArea.Height'将资源文件中的图片保存到数组actorImg(0) = My.Resources.Resource1.f1actorImg(1) = My.Resources.Resource1.m1'创建新人偶actor = New clsActor(actorImg(0))'设置初始随机行走方向rnd = New Random()actor.currentdirection = rnd.Next(8)'启动定时器tmActor.Start()End SubPrivate Sub tmActor_Tick(sender As Object, e As EventArgs) Handles tmActor.Tick'根据方向移动窗体Select Case actor.currentdirectionCase clsActor.Direction.downMe.Top += moveStraightLineCase clsActor.Direction.leftMe.Left -= moveStraightLineCase clsActor.Direction.rightMe.Left += moveStraightLineCase clsActor.Direction.upMe.Top -= moveStraightLineCase clsActor.Direction.downleftMe.Top += moveObliqueLineMe.Left -= moveObliqueLineCase clsActor.Direction.downrightMe.Top += moveObliqueLineMe.Left += moveObliqueLineCase clsActor.Direction.upleftMe.Top -= moveObliqueLineMe.Left -= moveObliqueLineCase clsActor.Direction.uprightMe.Top -= moveObliqueLineMe.Left += moveObliqueLineEnd Select'方向数组,保存与现有方向相反的方向Dim chooseDirection() As Integer'是否需要改变方向Dim changeDirection As Boolean = False'判断人偶行走是否到达屏幕边缘Select Case TrueCase Me.Left <= 0 '屏幕左边缘'如果到达左边缘,则方向数组中保存 向右、右下、右上 这三个方向,下同。chooseDirection = New Integer() {clsActor.Direction.right, clsActor.Direction.downright, clsActor.Direction.upright}changeDirection = TrueCase Me.Top <= 0 '屏幕上边缘chooseDirection = New Integer() {clsActor.Direction.down, clsActor.Direction.downleft, clsActor.Direction.downright}changeDirection = TrueCase (Me.Left + Me.Width) >= screenWidth '屏幕右边缘chooseDirection = New Integer() {clsActor.Direction.left, clsActor.Direction.downleft, clsActor.Direction.upleft}changeDirection = TrueCase (Me.Top + Me.Height) >= screenHeight '屏幕下边缘chooseDirection = New Integer() {clsActor.Direction.up, clsActor.Direction.upleft, clsActor.Direction.upright}changeDirection = TrueCase ElsechangeDirection = FalseEnd Selectrnd = New Random()'如果人偶到达边缘If changeDirection = True Then'设置人偶方向为方向数组中的随机一个方向actor.currentdirection = chooseDirection(rnd.Next(3))End If'从图片框创建GraphicsDim g As Graphicsg = picActor.CreateGraphics'移动人偶actor.moveActor(g)'释放当前的Graphicsg.Dispose()End Sub'设置定时器间隔Private Sub 速度100ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 速度100ToolStripMenuItem.ClicktmActor.Interval = 100End SubPrivate Sub 速度150ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 速度150ToolStripMenuItem.ClicktmActor.Interval = 150End SubPrivate Sub 速度200ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 速度200ToolStripMenuItem.ClicktmActor.Interval = 200End Sub'设置人偶性别Private Sub 男性角色ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 男性角色ToolStripMenuItem.Clickactor.loadActor(actorImg(0))End SubPrivate Sub 女性角色ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 女性角色ToolStripMenuItem.Clickactor.loadActor(actorImg(1))End Sub'退出程序Private Sub 退出ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 退出ToolStripMenuItem.ClickApplication.Exit()End Sub'当在图片框上按下鼠标时Private Sub picActor_MouseDown(sender As Object, e As MouseEventArgs) Handles picActor.MouseDown'暂停人偶移动tmActor.Stop()'在图片框上按下时,记录当前鼠标位置If e.Button = MouseButtons.Left ThenmousePoint = New Point(e.X, e.Y)End IfEnd Sub'当在图片框上移动鼠标时Private Sub picActor_MouseMove(sender As Object, e As MouseEventArgs) Handles picActor.MouseMove'实现人偶随鼠标移动If e.Button = MouseButtons.Left ThentmActor.Stop()Me.Left -= mousePoint.X - e.XMe.Top -= mousePoint.Y - e.YEnd IfEnd Sub'当在图片框上释放鼠标时Private Sub picActor_MouseUp(sender As Object, e As MouseEventArgs) Handles picActor.MouseUp'启动定时器tmActor.Start()End SubEnd Class
三、最终效果
图5、行走的人偶
图6、更换人偶性别