很喜欢用VB.NET编程序,在网上也收集了很多相关的材料,在这里我就VB.NET窗体应用总结了一个技巧,下面我们来一起看看吧。
打开 Visual Studio 2008在文件 (File) 菜单上,单击新建项目 (New Project)。 在新建项目 (New Project) 对话框的模板 (Templates) 窗格中,单击 Windows 应用程序(Windows Application)。单击确定 (OK)
VB.NET窗体应用技巧一,创建浮动窗体。
创建新工程后,选择Form1窗体,添加Timer1和Timer2控件。为窗体选择一个好看的背景,当然你也可以使用系统默认的背景。
进入代码编辑器,输入代码:
- Public Class Form1
- Inherits System.Windows.Forms.Form
- Private Sub Form1_Load(ByVal sender As System.Object,
- ByVal e As System.EventArgs)
- Handles MyBase.LoadDim pos As Point = New Point(100, 50)
- '设置窗体初始位置Me.DesktopLocation = posTimer1.Interval = 10
- '设置Timer的值Timer1.Enabled = TrueTimer2.Interval = 10Timer2.Enabled = False
- End Sub
进入Timer1_Tick事件
- Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
- Handles Timer1.TickDim pos As Point = New Point(Me.DesktopLocation.X + 2,
- Me.DesktopLocation.Y + 1)
- '窗体左上方横坐标的timer1加If pos.X < 600 Or pos.Y < 400 ThenMe.DesktopLocation = posElseTimer1.Enabled = FalseTimer2.Enabled = TrueEnd If
- End Sub
进入Timer2_Tick事件
- Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
- Handles Timer2.TickDim pos As Point = New Point(Me.DesktopLocation.X - 2,
- Me.DesktopLocation.Y - 1)
- '窗体的左上方横坐标随着timer2减一
- If pos.X > 100 Or pos.Y > 50
- ThenMe.DesktopLocation = posElseTimer1.Enabled = TrueTimer2.Enabled = False
- End If
- End Sub
创建完成后我们来运行程序测试一下,测试成功,程序在屏幕中不断地来回走动了。
VB.NET窗体应用技巧二,创建透明的窗体。
创建新工程后,选择Form1窗体,添加Label1、TrackBar1、Timer1控件。为了突出效果为窗体选择一个好看的背景。
相关的属性设置如下:
TrackBar1 Value属性:
TickFrequency: 属性:
Maximum属性: 100
10
100
Label1 Text属性: 选择窗体的透明度:
Timer1 Interval属性: 100
进入代码编辑器,输入代码:
首先进行声明:
- Public Class Form1
- Inherits System.Windows.Forms.FormDim tps
- As IntegerDim bol As Boolean
进入TrackBar1_Scroll事件
- Private Sub TrackBar1_Scroll(ByVal sender As Object, ByVal e As System.EventArgs)
- Handles TrackBar1TrackBar1.ScrollMe.Opacity = TrackBar1.Value / 100Label1.Text = "窗体透明度:" & CStr(Me.Opacity * 100) & "%"
- End Sub
进入Timer1_Tick事件
- Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.TickIf bol = False Thentps
= tps + 1Me.Opacity = tps / 100If Me.Opacity >= 1 ThenTimer1.Enabled = Falsebol = TrueEnd IfElsetps = tps - 1Me.Opacity =
tps / 100If Me.Opacity <= 0 ThenTimer1.Enabled = Falsebol = FalseEnd- If End
- If End
- Sub
进入Form1_Load事件
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadTimer1.Enabled = TrueEnd Sub
进入Form1_Closing事件
- Private Sub Form1_Closing(ByVal sender
- As Object, ByVal e As System.ComponentModel.CancelEventArgs)
- Handles MyBase.ClosingTimer1.Enabled = TrueIf MsgBox("你确实要关闭窗体吗?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok
- Thene.Cancel = FalseElseTimer1.Enabled = FalseMe.Opacity = 1tps = 100bol = Truee.Cancel = True
- End If
- End Sub
创建完成后我们来运行程序测试一下,测试成功,VB.NET窗体应用是不是变得透明了,通过调节滚动条我们甚至可以使得窗体消失达到完全隐形的目的。这是不是很神奇呢?
【编辑推荐】