本文介绍了在窗体的标题栏上处理鼠标悬停-在工具提示中显示最小MDI子项的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找类似 WM_NCMOUSEMOVE 的消息,它表示鼠标悬停在窗体的标题栏上。



当前,我已将此代码放在子窗体中,但问题是它们有很多子窗体,而且它不能处理标题栏上的鼠标悬停:

 私有常量WM_NCMOUSEMOVE =& HA0 
Dim stado_min As布尔值

受保护的替代子DefWndProc(ByRef m As System.Windows.Forms.Message)
如果stado_min AndAlso CLng(m.Msg)= WM_NCMOUSEMOVE然后
form_principal.ToolTipTitulo.SetToolTip(Me,Label1.Text)
如果
,则结束MyBase.DefWndProc(m)
End Sub

Private Sub schanged()处理MyBase.SizeChanged
stado_min =(Me.WindowState = FormWindowState.Minimized)
End Sub

事实上,我在寻找一种解决方案,当鼠标单击时在工具提示中显示MDI子标题将鼠标悬停在最小化的MDI上孩子我该怎么办?

解决方案

要在非客户区域上悬停鼠标,您可以捕获为其帖子。处理 WM_NCMOUSEHOVER 的初始代码已从此处获取,并进行了一些更改并删除了某些部分。


I'm looking for a message like WM_NCMOUSEMOVE that represents the mouse is hovering on title bar of a form.

Currently I've put this code in child forms, but the problem is that they are many child forms and also it doesn't handle a mouse hover on title bar:

Private Const WM_NCMOUSEMOVE = &HA0
Dim stado_min As Boolean

Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
    If stado_min AndAlso CLng(m.Msg) = WM_NCMOUSEMOVE Then 
        form_principal.ToolTipTitulo.SetToolTip(Me, Label1.Text)
    End If
    MyBase.DefWndProc(m)
End Sub

Private Sub schanged() Handles MyBase.SizeChanged
    stado_min = (Me.WindowState = FormWindowState.Minimized)
End Sub

In fact I'm looking for a solution to show title of an MDI child in tooltip when the mouse hovers over the minimized MDI child. How can I do that?

解决方案

To handle a mouse hover over non-client area, you can trap WM_NCMOUSEHOVER in WndProc. As mentioned in documentations hover tracking stops when this message is generated. The application must call TrackMouseEvent again if it requires further tracking of mouse hover behavior.

NonClientMouseHover Event Implementation

In below code, a NonClientMouseHover has been raised by trapping WM_NCMOUSEHOVER. You can handle NonClientMouseHover event like any other events of the form:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class SampleForm : Form
{
    [DllImport("user32.dll")]
    private static extern int TrackMouseEvent(ref TRACK_MOUSE_EVENT lpEventTrack);
    [StructLayout(LayoutKind.Sequential)]
    private struct TRACK_MOUSE_EVENT {
        public uint cbSize;
        public uint dwFlags;
        public IntPtr hwndTrack;
        public uint dwHoverTime;
        public static readonly TRACK_MOUSE_EVENT Empty;
    }
    private TRACK_MOUSE_EVENT track = TRACK_MOUSE_EVENT.Empty;
    const int WM_NCMOUSEMOVE = 0xA0;
    const int WM_NCMOUSEHOVER = 0x2A0;
    const int TME_HOVER = 0x1;
    const int TME_NONCLIENT = 0x10;
    public event EventHandler NonClientMouseHover;
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_NCMOUSEMOVE) {
            track.hwndTrack = this.Handle;
            track.cbSize = (uint)Marshal.SizeOf(track);
            track.dwFlags = TME_HOVER | TME_NONCLIENT;
            track.dwHoverTime = 500;
            TrackMouseEvent(ref track);
        }
        if (m.Msg == WM_NCMOUSEHOVER) {
            var handler = NonClientMouseHover;
            if (handler != null)
                NonClientMouseHover(this, EventArgs.Empty);
        }
    }
}

Example

Based on your question it seems you are interested to the event for a minimized mdi child window. The event also raises for a minimized mdi child form, so if for any reason you want to do something when the mouse hover title bar of a minimized mdi child, you can check if(((Form)sender).WindowState== FormWindowState.Minimized). Also ((Form)sender).Text is text of the form which raised the event.

public partial class Form1 : Form
{
    ToolTip toolTip1 = new ToolTip();
    public Form1()
    {
        //InitializeComponent();
        this.Text = "Form1";
        this.IsMdiContainer = true;
        var f1 = new SampleForm() { Text = "Some Form", MdiParent = this };
        f1.NonClientMouseHover += child_NonClientMouseHover;
        f1.Show();
        var f2 = new SampleForm() { Text = "Some Other Form", MdiParent = this };
        f2.NonClientMouseHover += child_NonClientMouseHover;
        f2.Show();
    }
    void child_NonClientMouseHover(object sender, EventArgs e)
    {
        var f = (Form)sender;
        var p = f.PointToClient(f.Parent.PointToScreen(f.Location));
        p.Offset(0, -24);
        toolTip1.Show(f.Text, f, p, 2000);
    }
    protected override void OnFormClosed(FormClosedEventArgs e)
    {
        toolTip1.Dispose();
        base.OnFormClosed(e);
    }
}

Note: Thanks to Bob for his post here. The initial code for handling WM_NCMOUSEHOVER has taken from there and made working with some changes and removing some parts.

这篇关于在窗体的标题栏上处理鼠标悬停-在工具提示中显示最小MDI子项的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 15:30