如何为没有click事件的东西设置click处理程序

本文关键字:click 设置 程序 处理 事件 | 更新日期: 2023-09-27 18:05:19

我通过ErrorProvider组件有一个Icon(因为它本身不是Control)。我想把点击处理程序钩到图标本身。当它显示时,我想让我的用户单击它,并生成一个详细的弹出窗口或类似帮助文章的界面来提供详细信息。它暴露了一个IntPtr句柄,但我对Win32世界并不精通。我想这就是我需要的(WndProc的东西,也许?? ?)因为我试过在包含控件的表单上添加一个点击,但这并没有削减它。

我该如何继续?谢谢。

如何为没有click事件的东西设置click处理程序

ErrorProvider动态创建窗口来显示错误图标。检测这些窗口上的鼠标点击需要非常糟糕的代码。我会告诉你怎么做,我建议你不要实际使用它。启动一个新的WF应用程序,并在表单上放置一个文本框和一个错误提供程序。粘贴以下代码:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsFormsApplication1 {
  public partial class Form1 : Form, IMessageFilter {
    public Form1() {
      InitializeComponent();
      Application.AddMessageFilter(this);
      errorProvider1.SetError(textBox1, "Test");
    }
    protected override void OnFormClosing(FormClosingEventArgs e) {
      Application.RemoveMessageFilter(this);
      base.OnFormClosing(e);
    }
    public bool PreFilterMessage(ref Message m) {
      if (m.Msg == 0x201) {
        // MouseDown, check if the icon was clicked
        NativeWindow wnd = NativeWindow.FromHandle(m.HWnd);
        if (wnd == null) return false;
        Type t = wnd.GetType();
        if (t.Name != "ErrorWindow") return false;
        // Yes, use Reflection to find the control for that icon
        FieldInfo fi = t.GetField("items", BindingFlags.NonPublic | BindingFlags.Instance);
        System.Collections.ArrayList items = fi.GetValue(wnd) as System.Collections.ArrayList;
        if (items == null || items.Count == 0) return false;
        object item = items[0];
        FieldInfo fi2 = item.GetType().GetField("control", BindingFlags.NonPublic | BindingFlags.Instance);
        Control ctl = fi2.GetValue(item) as Control;
        // Got it.
        MessageBox.Show("You clicked the icon for " + ctl.Name);
      }
      return false;
    }
  }
}