系统托盘中显示额外的 NotifyIcon

发布于 2024-10-10 13:18:46 字数 2031 浏览 5 评论 0原文

我的应用程序出现问题,其中我的 NotifyIcon 显示额外的图标。重现它的步骤很简单,但问题是,在我们添加的任何实际代码隐藏之后,都会显示额外的图标。简而言之,单击按钮会触发 FooBar() 方法的执行,该方法一路运行良好,但其主要职责是激发后台工作者登录到我们的另一个应用程序。仅当单击此特定按钮时它才会出现。

奇怪的是,我们有一个 WndProc 方法重写,如果我单步执行直到额外的 NotifyIcon 出现,它总是在此方法期间出现,因此代码隐藏之外的其他东西一定会触发该行为。我们的 WndProc 方法目前是(尽管我不认为它是由 WndProc 引起的):

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    'Check for WM_COPYDATA message from other app or drag/drop action and handle message
    If m.Msg = NativeMethods.WM_COPYDATA Then
      ' get the standard message structure from lparam
      Dim CD As NativeMethods.COPYDATASTRUCT = m.GetLParam(GetType(NativeMethods.COPYDATASTRUCT))

      'setup byte array
      Dim B(CD.cbData) As Byte

      'copy data from memory into array
      Runtime.InteropServices.Marshal.Copy(New IntPtr(CD.lpData), B, 0, CD.cbData)

      'Get message as string and process
      ProcessWMCopyData(System.Text.Encoding.Default.GetString(B))

      'empty array
      Erase B

      'set message result to 'true', meaning message handled
      m.Result = New IntPtr(1)
    End If

    'pass on result and all messages not handled by this app
    MyBase.WndProc(m)
  End Sub

代码中唯一操作有问题的 NotifyIcon 的地方是在以下事件处理程序中(再次强调,不要认为这是罪魁祸首,但只是为了更多信息):

Private Sub TrayIcon_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrayIcon.MouseDoubleClick
    If Me.Visible Then
      Me.Hide()
    Else
      PositionBottomRight()
      Me.Show()
    End If
  End Sub

backgroundworker的DoWork如下(只是一个类调用来登录到我们的其他应用程序,但同样只是为了提供信息):

Private Sub LoginBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles LoginBackgroundWorker.DoWork
    Settings.IsLoggedIn = _wdService.LogOn(Settings.UserName, Settings.Password)
End Sub

是否有其他人知道可能导致此问题的原因或如何可能进一步调试这个?我一直在努力解决这个问题,但没有看到任何模式,所以另一双眼睛将非常感激。 :) 我也在 MSDN winforms 论坛上发布了此内容,但到目前为止也没有运气。

I'm having an issue with an app where my NotifyIcon displays an extra icon. The steps to reproduce it are easy, but the problem is that the extra icon shows up after any of the actual codebehind we've added fires. Put simply, clicking a button triggers execution of method FooBar() which runs all the way through fine but its primary duty is to fire a backgroundworker to log into another of our apps. It only appears if this particular button is clicked.

Strangely enough, we have a WndProc method override and if I step through until the extra NotifyIcon appears, it always appears during this method so something else beyond the codebehind must be triggering the behavior. Our WndProc method is currently (although I don't think it's caused by the WndProc):

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    'Check for WM_COPYDATA message from other app or drag/drop action and handle message
    If m.Msg = NativeMethods.WM_COPYDATA Then
      ' get the standard message structure from lparam
      Dim CD As NativeMethods.COPYDATASTRUCT = m.GetLParam(GetType(NativeMethods.COPYDATASTRUCT))

      'setup byte array
      Dim B(CD.cbData) As Byte

      'copy data from memory into array
      Runtime.InteropServices.Marshal.Copy(New IntPtr(CD.lpData), B, 0, CD.cbData)

      'Get message as string and process
      ProcessWMCopyData(System.Text.Encoding.Default.GetString(B))

      'empty array
      Erase B

      'set message result to 'true', meaning message handled
      m.Result = New IntPtr(1)
    End If

    'pass on result and all messages not handled by this app
    MyBase.WndProc(m)
  End Sub

The only place in the code where the NotifyIcon in question is manipulated at all is in the following event handler (again, don't think this is the culprit, but just for more info):

Private Sub TrayIcon_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrayIcon.MouseDoubleClick
    If Me.Visible Then
      Me.Hide()
    Else
      PositionBottomRight()
      Me.Show()
    End If
  End Sub

The backgroundworker's DoWork is as follows (just a class call to log in to our other app, but again just for info):

Private Sub LoginBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles LoginBackgroundWorker.DoWork
    Settings.IsLoggedIn = _wdService.LogOn(Settings.UserName, Settings.Password)
End Sub

Does anyone else have ideas on what might be causing this or how to possibly further debug this? I've been banging my head on this without seeing a pattern so another set of eyes would be extremely appreciated. :) I've posted this on MSDN winforms forums as well and have had no luck there so far either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-10-17 13:18:46

我记得几年前在一个应用程序中看到过类似的情况,该应用程序实例化了一个新的 NotifyIcon 而不释放以前的实例。

I remember seeing something like this years ago in an application that instantiated a new NotifyIcon without disposing the previous instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文