【问题标题】:creating events for newly created windows form control (in wpf)为新创建的 Windows 窗体控件创建事件(在 wpf 中)
【发布时间】:2013-06-12 18:58:48
【问题描述】:

我使用以下代码在 wpf 项目中创建了一个通知图标:

Dim ni = New System.Windows.Forms.NotifyIcon
Private Sub btnsystemtray_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles btnTaskbar.MouseUp
    ni.Visible = True
    ni.Icon = My.Resources.myicon
    Me.Hide()
End Sub

我能够使它工作,将窗口最小化到系统托盘。但是,我不知道如何为应该显示窗口的 notifyicon 创建一个单击事件。谢谢!

【问题讨论】:

标签: .net wpf vb.net visual-studio-2010


【解决方案1】:

WPF 没有对 NotifyIcon 的设计时支持,因此您必须在代码中完成。

Private WithEvents ni As New System.Windows.Forms.NotifyIcon

Private Sub ni_Click(sender As Object, e As System.EventArgs) Handles ni.Click
  Me.Show()
End Sub

请务必在退出时手动处理 NotifyIcon 以将其从系统托盘中移除:

Private Sub MainWindow_Closed(sender As Object, e As System.EventArgs) Handles Me.Closed
  ni.Dispose()
End Sub

【讨论】:

  • +1 - 如果您想在窗口恢复后摆脱托盘中的图标,请确保在 ni_Click 中调用 ni.Visible = False
  • 不要使用Form的Closed事件,它是obsolute since .NET 2.0,改用FormClosed event
  • 另外,在Dispose()Dispose() 中调用ni.Dispose() MainWindow (因为当您关闭 MainWindow 时不一定会处理它......因此它的资源不应该得到处置)。
猜你喜欢
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 2011-01-15
  • 2016-11-17
  • 2021-08-10
相关资源
最近更新 更多