(摘)用VB.NET轻松制作特效窗体

段键
2003-1-2 14:31:13


窗体是Windows应用程序的基础。新一代的开发工具Visual Basic.Net为设计制作窗体提供了更多简单而丰富的方法,无需再求助于复杂而易错的API函数,我们就可以轻松制作多种特效窗体:

1 、轻松制作透明窗体

VB.NET可以轻松制作出任一透明度的窗体:我们只要在窗体的“属性”窗口中,将 Opacity 属性设置为一个介于 0.0(完全透明)与 1.0(完全不透明)之间的值就可以了:

Dim frm As FrmTrans = New FrmTrans()

frm.Opacity = 0.5

frm.ShowDialog()

2、轻松制作始终位于最上层的窗体

在VB6中,要制作一个始终位于最上层的窗体我们只能求助于令人头痛的API函数。然而在 .NET 中,我们只要简单设置窗体的 TopMost 属性就可实现同样效果了!例如:

Dim frm As frmTopMost = New frmTopMost()

frm.TopMost = True

frm.Show()

3、轻松制作不可见的窗体

如果要编写一个不让别人发现的隐藏程序,制作不可见的窗体就是必须实现的第一步。窗体的可见性通常由 Visible 属性控制。但是,如果希望 Windows 应用程序的主窗体在应用程序启动时不可见,您将会发现将它的 Visible 属性设置为 False 的方法无效,窗体总会自己显示出来(这是因为,启动窗体的生存期决定了应用程序的生存期)。虽然如此,我们还是可以通过简单将应用程序的启动设置为一个模块,从而从窗体的生存期分出应用程序的生存期,轻松实现不可见的窗体。在下面这个例子中,窗体在特定的时间内自动隐藏:

(1)在 Visual Basic 中,右击项目并选择“添加模块”以将模块添加到 Windows 应用程序。

(2)在已添加的模块(或类)内,创建可作为项目启动对象的 Main 函数:

Sub main()

Dim f1 As New Form1()

f1.Visible = False

While Hour(Date.Now) < 15 ‘如果当前时间早于15点,窗体自动隐藏

Application.DoEvents()

End While

f1.ShowDialog()

End Sub

4、轻松编写托盘程序

托盘程序作为一类特殊的窗体,其快捷图标显示在系统托盘中,窗体本身则隐藏不可见。在.NET之前版本的VB中编写托盘程序是十分困难的,但是VB.NET提供的新的NotifyIcon组件却使VB初学者也能轻松编写一个这样的程序:

新建“Windows应用程序”,设置主窗体Opacity属性为0,FormBorderStyle属性为None,ShowInTaskbar属性为False,这样窗体将在启动后隐藏。在窗体上放置一个NotifyIcon组件NotifyIcon1,一个ContextMenu(弹出菜单)组件ContextMenu1,并根据需要为ContextMenu1添加菜单项。

设置NotifyIcon1的ICON属性,这个图标就是应用程序出现在系统托盘中的快捷图标;设置NotifyIcon1的Text属性为“VB.NET托盘程序”,这就是鼠标移动到托盘图标时弹出的文字说明;设置NotifyIcon1的ContextMenu属性为ContextMenu1,也就是右键单击快捷图标时的弹出菜单为ContextMenu1。OK,按F5运行!

几乎不用编写代码,一个托盘程序就这样轻松实现了。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值