你仍然可以使用的MessageFilter,只是過濾了的ActiveForm:在您的形式
private class MouseDownFilter : IMessageFilter {
public event EventHandler FormClicked;
private int WM_LBUTTONDOWN = 0x201;
private Form form = null;
[DllImport("user32.dll")]
public static extern bool IsChild(IntPtr hWndParent, IntPtr hWnd);
public MouseDownFilter(Form f) {
form = f;
}
public bool PreFilterMessage(ref Message m) {
if (m.Msg == WM_LBUTTONDOWN) {
if (Form.ActiveForm != null && Form.ActiveForm.Equals(form)) {
OnFormClicked();
}
}
return false;
}
protected void OnFormClicked() {
if (FormClicked != null) {
FormClicked(form, EventArgs.Empty);
}
}
}
然後,將其附加:
public Form1() {
InitializeComponent();
MouseDownFilter mouseFilter = new MouseDownFilter(this);
mouseFilter.FormClicked += mouseFilter_FormClicked;
Application.AddMessageFilter(mouseFilter);
}
void mouseFilter_FormClicked(object sender, EventArgs e) {
// do something...
}
你有沒有看MouseHover事件? http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx – Bit
@downvoter注意評論爲什麼? – ispiro
儘管有您的要求,您已經刪除了所有可能性。你將不得不從這兩個選擇中選擇一個。 (我不是下來的選民)。 – LarsTech