如何将ShowShow显示的窗体中的消息附加到Application2? [英] How to attach Messages from a Form shown with ShowDialog to Application2?

查看:79
本文介绍了如何将ShowShow显示的窗体中的消息附加到Application2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此文章,让您知道应用程序何时处于空闲状态。

I am trying to use the code in this article that lets you know when your app is idle..

如果您的应用程序只有一种形式,则此代码非常有用。您在其上调用 Application2.Run(myOnlyForm),所有消息都会通过Application2中的过滤器进行路由。

This code works great if your application has only one form. You call Application2.Run(myOnlyForm) on it and all messages get routed through the filters in Application2.

但是如果在任何时候调用 mySecondForm.ShowDialog(),该对话框都不会通过Application2过滤其消息。

However if at any point you call mySecondForm.ShowDialog() that dialog does not get its messages filtered through Application2.

有什么方法(没有不良影响)使消息在 mySecondForm 上通过 Application2事件过滤器?

Is there any way (with out bad side effects) to get the messages on mySecondForm to go through the `Application2' event filters?

我尝试过:


  1. mySecondForm.ShowDialog 更改为 Application2.Run(mySecondForm)


    • 这会导致超出范围的窗口无法清理,并且在需要时不显示。

  1. Changing mySecondForm.ShowDialog to Application2.Run(mySecondForm).
    • This causes the window to not clean up when it goes out of scope and to not show when needed.

  • 这将导致 mySecondForm 上的主菜单不起作用(单击无效,只是发出哔声) 。


    • 这似乎是我应该使用的那个,但我有点需要在屏幕上显示菜单。看来这是常见 OpenNETCF的问题

    • 菜单项未损坏。仍然可以使用菜单的热键。仅点击菜单不起作用。

    • This causes the main menu on mySecondForm to not work (clicking has no effect, just beeps).
      • This seems like the one I should be using, but I kind of need to have menus on my screens. It seems that this is a common problem with OpenNETCF.
      • The menu item is not broken. Using the hot key for the menu still works. Just tapping the menu does not work.

      • 这不起作用,因为在关闭几次后我需要访问该对话框。

      理想情况下,我想要一种将表格附加到Application2的消息功能的方法。

      Ideally I would like a way to attach a form to the message abilities of Application2.

      但是我欢迎任何建议。

      编辑:
      根据对ctacke的建议,这是我所做的:

      Based on the suggestion for ctacke this is what I have done:

      public static DialogResult ShowDialog2(this Form form)
      {
          //form.Activated += InsertMenu;
          //Application2.ShowDialog(form);
          form.Show();
          try
          {
              do
              {
                  Application2.DoEvents();
              } while (form.Visible);
          }
          catch (ObjectDisposedException)
          {
              // This just means that the form was closed.  Not a big deal.
          }
          return form.DialogResult;
      
      }
      

      我最终呼叫ShowDialog2而不是ShowDialog

      I end up calling ShowDialog2 rather than ShowDialog

      推荐答案

      我可以解释这种行为,尽管可能无法提供直接解决方案。

      I can explain the behavior, though maybe not offer a direct solution.

      any 表单上调用Show时,该表单的事件由默认消息泵(通过调用Run设置)处理。当您调用ShowDialog时,目标窗体将获得其自己的单独的消息泵。

      When you call Show on any Form, the Form's events get handled by the default message pump (which gets set up with the call to Run). When you call ShowDialog, the target Form gets its own, separate message pump.

      现在,您添加的过滤器位于主消息泵中,并正在查看所有消息。在那里,但是ShowDialog调用规避了-发送到对话框的消息将永远无法到达过滤器。

      Now the filter you've added resides in the main message pump and it's looking at all messages there, but the ShowDialog call circumvents that - messages send to the dialog will never reach the filter.

      现在,我们确实添加了Application2.ShowDialog调用作为解决方法。这个问题,但是说实话,当我编写整个Application.Run/IMEssageFilter实现时,我没有执行ShowDialog解决方法,而且我真的不知道它的实现情况。根据您的报告,尽管这不是一个简单的解决问题,但我可能会猜测不好。此问题的根源在于,当您调用Show和ShowDialog时,SDF无法控制BCL中发生的事情-我们只是试图坐在其上方,并提供最佳行为。在这种情况下,这是有问题的。

      Now we did add the Application2.ShowDialog call as an attempt to work around this issue, but to be honest, while I wrote the entire Application.Run/IMEssageFilter implementation, I did not do the ShowDialog workaround and I really don't know how well it was implemented. Based on your report I'd hazard a guess of "not well" though it really is not a simple problem to solve. The root of this issue is that the SDF doesn't control what happens in the BCL when you call Show and ShowDialog - we're simply trying to sit above it and provide the best behavior we can. In this case it's problematic.

      您是否可以偶然地不使用对ShowDialog的调用,而只需将Show与保持TopMost形式的东西一起使用?这将允许过滤器获取伪对话框的所有消息。我能想到的另一种选择是Dialogs的基类,该基类将通知回过滤器机制,但开始变得难以控制。

      Can you, by chance, not use a call to ShowDialog, but instead just use Show coupled with something like keeping the form TopMost? This would allow the filter to get all messages for the pseudo-dialog. The other option I can think of right offhand would be a base class for Dialogs that would notify back to the filter mechanism, but that starts getting real challenging to control.

      这篇关于如何将ShowShow显示的窗体中的消息附加到Application2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆