注册 登录
编程论坛 VB.NET论坛

右下角提示框

wysh1014 发布于 2009-11-16 17:14, 3236 次点击
语言:
效果:像QQ新闻的提示框(数据来于远程数据库,access)在桌面的右下角,在软件上没有任何点击,当窗体最小化后,鼠标移到图标上就会出现。

23 回复
#2
不说也罢2009-11-16 19:57
下面是一个例子:
新建一个工程

Form1.Designer.VB:
程序代码:
<Global.Microsoft.()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form 重写 Dispose,以清理组件列表。
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Windows 窗体设计器所必需的
    Private components As '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改它。
    '不要使用代码编辑器修改它。
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
         = New resources As = New (GetType(Form1))
        Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon()
        Me.LinkLabel1 = New System.Windows.Forms.LinkLabel
        Me.LinkLabel2 = New System.Windows.Forms.LinkLabel
        Me.SuspendLayout()
        '
        'NotifyIcon1
        '
        Me.NotifyIcon1.Icon = CType(resources.GetObject("NotifyIcon1.Icon"), System.Drawing.Icon)
        Me.NotifyIcon1.Text = "NotifyIcon1"
        Me.NotifyIcon1.Visible = True
        '
        'LinkLabel1
        '
        Me.LinkLabel1.AutoSize = True
        Me.LinkLabel1.ForeColor = System.Drawing.Color.Lime
        Me.LinkLabel1.Location = New System.Drawing.Point(8, 61)
        Me.LinkLabel1.MinimumSize = New System.Drawing.Size(150, 0)
        Me.LinkLabel1.Name = "LinkLabel1"
        Me.LinkLabel1.Size = New System.Drawing.Size(150, 12)
        Me.LinkLabel1.TabIndex = 0
        Me.LinkLabel1.TabStop = True
        Me.LinkLabel1.Text = "https://bbs.bccn.net"
        '
        'LinkLabel2
        '
        Me.LinkLabel2.AutoSize = True
        Me.LinkLabel2.Location = New System.Drawing.Point(8, 105)
        Me.LinkLabel2.MinimumSize = New System.Drawing.Size(150, 0)
        Me.LinkLabel2.Name = "LinkLabel2"
        Me.LinkLabel2.Size = New System.Drawing.Size(150, 12)
        Me.LinkLabel2.TabIndex = 1
        Me.LinkLabel2.TabStop = True
        Me.LinkLabel2.Text = "退出本程序"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(185, 166)
        Me.ControlBox = False
        Me.Controls.Add(Me.LinkLabel2)
        Me.Controls.Add(Me.LinkLabel1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "Form1"
        Me.Opacity = 0.5
        Me.ShowInTaskbar = False
        Me.TransparencyKey = System.Drawing.Color.Yellow
        Me.ResumeLayout(False)
        Me.PerformLayout()
        'Me.SetStyle(ControlStyles.UserPaint, True)
    End Sub
    Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon
    Friend WithEvents LinkLabel1 As System.Windows.Forms.LinkLabel
    Friend WithEvents LinkLabel2 As System.Windows.Forms.LinkLabel

End Class

Form1.VB:
程序代码:
Public Class Form1
    Dim x As Double = Me.Width
    Dim y As Double = Me.Height
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        NotifyIcon1.Dispose()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        x = Me.Width
        y = Me.Height
        Me.Width = 0
        Me.Height = 0
        Me.Visible = False
    End Sub

    Private Sub NotifyIcon1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseMove
        Me.TopMost = True
        Me.Visible = True
        Me.WindowState = FormWindowState.Normal
        Me.SetBounds(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - x, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - y, x, y)
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        System.Diagnostics.Process.Start(LinkLabel1.Text)
        Me.Visible = False
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        NotifyIcon1.Dispose()
        End
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.X = 0 Or e.X = Me.Width Or e.Y = 0 Or e.Y = Me.Height Then Me.Visible = False
    End Sub

    Public Sub New()
        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
        ' 在 InitializeComponent() 调用之后添加任何初始化。
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class



'如果是从SUBMAIN运行,就没有这么复杂
#3
wysh10142009-11-16 21:58
回复 2楼 不说也罢
我把这些代码放上去。有几处下划线,是不是还要有什么引用才行呀?这个是新建的工程,那怎么和我以前做的工程连起来呀?像这样的效果
只有本站会员才能查看附件,请 登录
怎么实现。就是当鼠标放在图标上就出来那些提示。提示框里的信息来自远程  access文件
#4
不说也罢2009-11-17 11:44
示例工程,解压后打开WindowsApplication1.sln文件即可

只有本站会员才能查看附件,请 登录
#5
wysh10142009-11-17 12:21
回复 4楼 不说也罢
运行了你的程序,就是这种效果,但是打不开。提示说我的版本太低了……很想用你的代码……
#6
不说也罢2009-11-17 13:00
哦,这我没预计到,我用的是2008,framework3.5
#7
wysh10142009-11-17 13:27
回复 6楼 不说也罢
我是03版的。net
#8
不说也罢2009-11-17 13:44
版本虽不同,但过程代码基本没什么区别

窗体中添加两个LABLELINK控件,一个NotifyIcon控件即可,然后将三个控件及窗体的过程代码复制过去就可以了

程序代码:
Public Class Form1
    Dim x As Double = Me.Width
    Dim y As Double = Me.Height
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        NotifyIcon1.Dispose()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        x = Me.Width
        y = Me.Height
        Me.Width = 0
        Me.Height = 0
        Me.Visible = False
    End Sub

    Private Sub NotifyIcon1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseMove
        Me.TopMost = True
        Me.Visible = True
        Me.WindowState = FormWindowState.Normal
        Me.SetBounds(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - x, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - y, x, y)
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        System.Diagnostics.Process.Start(LinkLabel1.Text)
        Me.Visible = False
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        NotifyIcon1.Dispose()
        End
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.X = 0 Or e.X = Me.Width Or e.Y = 0 Or e.Y = Me.Height Then Me.Visible = False
    End Sub
End Class

将上述代码复制到窗体FORM1中,在设计时,为NotifyIcon1指定一个ICON图标,不然在托盘里是看不见的

[ 本帖最后由 不说也罢 于 2009-11-17 13:47 编辑 ]
#9
wysh10142009-11-17 14:59
回复 8楼 不说也罢
代码可以了。但是好像大了点。那个提示框的大小能控制吗?还有。提示框中的数据如果是从服务器中(如:http://www.baidu.com)这个网站的数据库(access数据库)上得到的可以吗?当提示框出现一次,就重新访问一次数据库。把最新的数据显示在提示框中……
#10
不说也罢2009-11-17 17:40
当提示框出现一次,就重新访问一次数据库。-----这功能可以在NotifyIcon1_MouseMove事件中实现,也可以用定时器定时地访问远程数据库

那个提示框的大小能控制吗?————可以动态实现

程序代码:
Public Class Form1
    Dim x As Double = 200'修改X,Y值即可
    Dim y As Double = 120
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        NotifyIcon1.Dispose()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Width = 0
        Me.Height = 0
        Me.Visible = False
    End Sub

    Private Sub NotifyIcon1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseMove
        Me.TopMost = True
        Me.Visible = True
        Me.WindowState = FormWindowState.Normal
        Me.SetBounds(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - x - 30, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - y, x, y)
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        System.Diagnostics.Process.Start(LinkLabel1.Text)
        Me.Visible = False
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        NotifyIcon1.Dispose()
        End
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.X = 0 Or e.X = Me.Width Or e.Y = 0 Or e.Y = Me.Height Then Me.Visible = False
    End Sub

End Class
#11
wysh10142009-11-17 17:51
回复 10楼 不说也罢
那访问数据库的代码要怎么写??不好意思。我是新手……很我都不懂的
#12
不说也罢2009-11-17 18:06
建议你找本入门的书先研究一下,不要急于写程序,好吗?

从你问的问题看不应当是刚入门的,应当有一定基础的哦
#13
不说也罢2009-11-17 18:59
这是一个带有动画的:

程序代码:
Public Class Form1
    Dim x As Double = 200
    Dim y As Double = 120
    Dim i As Integer
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        NotifyIcon1.Dispose()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Width = 0
        Me.Height = 0
        Me.Visible = False
    End Sub

    Private Sub NotifyIcon1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseMove
        If Me.Visible = True And Me.Top = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - y Then Exit Sub
        Me.TopMost = True
        Me.Visible = True
        Me.WindowState = FormWindowState.Normal
        Do While i < y
            i = i + 1
            Me.SetBounds(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - x - 30, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - i, x, y)
        Loop
        i = 0
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        System.Diagnostics.Process.Start(LinkLabel1.Text)
        Me.Visible = False
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        NotifyIcon1.Dispose()
        End
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.X = 0 Or e.X >= Me.Width - 10 Or e.Y = 0 Or e.Y >= Me.Height - 5 Then Me.Visible = False
    End Sub

End Class
#14
wysh10142009-11-17 19:30
回复 13楼 不说也罢
我现在换一种方法来实现,但有点小问题,请教一下。比如我在窗体form1的代码中放入
Private Sub NotifyIcon1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseMove
        Dim show As New Form2
        show.Show()
    End Sub
就是当鼠标移动到图标时就出现窗体form2,但是会出现无数个窗体2,这个该怎么解决。再一个,出现的窗体2没在右下角?
版主能不能帮解决这两个问题,因为从窗体2中,我就可以访问远程数据库并做到显示一次重新访问一次了
#15
不说也罢2009-11-17 20:04
你要学会举一反三:

FORM1代码:

程序代码:
Public Class Form1
'此窗体只需要一个NotifyIcon1控件
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        NotifyIcon1.Dispose()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Width = 0
        Me.Height = 0
        Me.Visible = False
    End Sub

    Private Sub NotifyIcon1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseMove
        Form2.Show()
    End Sub

End Class

FORM2代码:

程序代码:
Public Class Form2
    ' 此窗体需要一个Label1控件,一个LinkLabel1控件
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim r As New Random
        Dim i As Integer = r.Next
        Me.Visible = False
        Me.ShowInTaskbar = False
        Me.SetBounds(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - 250, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - 200, 200, 200)
        Me.Visible = True
        Me.TopMost = True
        Label1.Text = i '这是随机数,FORM2每次加载都不一样,这里就可以读取数据库了
    End Sub


    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.X = 0 Or e.X >= Me.Width - 10 Or e.Y = 0 Or e.Y >= Me.Height - 5 Then Me.Close()
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        System.Diagnostics.Process.Start(LinkLabel1.Text)
        'Me.Visible = False'修改一下,不然不会随机显示,改为下面一行
        Me.Close()
    End Sub
End Class



[ 本帖最后由 不说也罢 于 2009-11-17 20:16 编辑 ]
#16
不说也罢2009-11-17 20:35
如果继续想实现单击托盘图标退出本程序的话

FORM1窗体中还需要添加一个ContextMenuStrip控件,并将NotifyIcon1控件的ContextMenuStrip属性设置与之关联

然后在ContextMenuStrip事件中写退出程序的代码
#17
wysh10142009-11-17 20:54
回复 15楼 不说也罢
嗯。窗体2可以在右下角实现了,也可以重新访问数据了。但是还有一个问题,就是还是会有很多窗体2出现在右下角,重叠在一起,怎么才能让窗体只出现一次呢?
#18
不说也罢2009-11-18 12:33
程序代码:
Public Class Form1
    '此窗体只需要一个NotifyIcon1控件
    Public isShow As Boolean = False
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        NotifyIcon1.Dispose()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Width = 0
        Me.Height = 0
        Me.Visible = False
    End Sub

    Private Sub NotifyIcon1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseMove
        If isShow = True Then Exit Sub
        Form2.Show()
    End Sub

 End Class

程序代码:
Public Class Form2
    ' 此窗体需要一个Label1控件,一个LinkLabel1控件
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Form1.isShow = True
        Dim r As New Random
        Dim i As Integer = r.Next
        Me.Visible = False
        Me.ShowInTaskbar = False
        Me.SetBounds(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - 250, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - 200, 200, 200)
        Me.Visible = True
        Me.TopMost = True
        Label1.Text = i '这是随机数,FORM2每次加载都不一样,这里就可以读取数据库了
    End Sub

    Private Sub Form2_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Form1.isShow = False
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.X = 0 Or e.X >= Me.Width - 10 Or e.Y = 0 Or e.Y >= Me.Height - 5 Then Me.Close()
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        System.Diagnostics.Process.Start(LinkLabel1.Text)
        Me.Close()
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        Me.Close()
    End Sub
End Class

让窗体只出现一次


.
#19
wysh10142009-11-18 12:44
回复 18楼 不说也罢
Form1.isShow = True
        放到窗体2里出现下划线,( 提示对非共享成员的引用要求对象引用)???怎么处理?
#20
不说也罢2009-11-18 12:48
form1中有定义,你没有复制FORM1中的代码:
#21
wysh10142009-11-18 15:41
回复 19楼 wysh1014
有呀。但是还是有下划线,我也按你的方法,在form1中定义了。是不是版本问题?还是要引用什么,定义成全局变量可以不?
#22
不说也罢2009-11-18 17:18
可以定义成全局变量
#23
wysh10142009-11-18 18:59
谢谢版主,问题解决了……
#24
不说也罢2009-11-19 12:31
不谢啊
请及时结贴,今天我又打开了此贴,以为你有新的跟贴呢。
1