VB.NET 2005 - “全球"事件处理程序? [英] VB.NET 2005 - "Global" Event Handler?

查看:35
本文介绍了VB.NET 2005 - “全球"事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设对于 WinForms 应用程序中的每个 Form,您希望将光标更改为 WaitCursor.执行此操作的显而易见的方法是将代码添加到表单实例化/显示的每个位置:

Suppose that for every Form in a WinForms application, you want to change the cursor to the WaitCursor. The obvious way to do this would be to add the code to every place where a form is instantiated/shown:

Try
    Me.Cursor = Cursors.WaitCursor

    Dim f As New frmMyForm
    f.Show()

Catch ex As Exception
    Throw
Finally
    Me.Cursor = Cursors.Default
End Try

但是,我想知道是否有办法告诉您的应用程序,每当任何表单 Load 事件触发时,显示一个 WaitCursor.当表单事件 Shown 完成时,将光标设置回默认值."这样,Me.Cursor 代码就可以只在一个地方,而不是分散在整个应用程序中 - 并且不会忘记将其放入每个表单实例化中.

However, I was wondering if there is a way to tell your application, "Whenever any form Load event fires, show a WaitCursor. When the form event Shown is complete, set the cursor back to Default." This way, the Me.Cursor code could be only in one place and not scattered throughout the application - and not forgotten to put it in to each form instantiation.

我想您可以子类化常规 Form 类并在重写的事件中添加光标设置,但我相信当您子类化 Form 对象时,您会失去可视化设计器的功能.

I suppose you could subclass the regular Form class and add the cursor settings in an overridden event, but I believe you lose the visual designer capability when you subclass the Form object.

推荐答案

回答您的问题 - 没有全局 .Net 事件可以实现您想要的.对此没有任何纯 .net 解决方案.您可以查看面向方面的编程和横切关注点 - 可能有一个 AOP 解决方案(一些谷歌搜索可以帮助您入门,然后在此处发回详细信息).

To answer your question - there are no global .Net events to achieve what you want. There isn'y any pure .net solution to this. You could take a look at Aspect Orientated Programming and Cross Cutting Concerns - there may be an AOP solution to this (some googling will get you started then post back here for details).

但是,关于如何使用 win32 消息传递来实现这一点,接下来的内容更多的是一个想法,而不是一个完整的解决方案.

However, what follows is more of an idea rather than a complete solution as to how you might achieve this using win32 messaging.

  1. 您需要确定哪些 win32 消息对应于 Load 事件,以及是否存在始终在 load 事件之后发生的 win32 消息.加载事件的一个候选对象可能是 WM_SHOWWINDOW但我不确定.
  2. 编写一个消息过滤器类(即实现IMessageFilter).
  3. 在消息过滤器类的 PreMessageFilter 方法中检查消息类型,如果它是带有正确参数的 WM_SHOWWINDOW(或其他)消息,则您可以设置/重置光标(使用 Cursor.Current = Cursors.WaitCursor - 再次需要测试这个)

这篇关于VB.NET 2005 - “全球"事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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