Control.OnMouseMove(MouseEventArgs) 方法

定义

引发 MouseMove 事件。

C#
protected virtual void OnMouseMove(System.Windows.Forms.MouseEventArgs e);

参数

e
MouseEventArgs

包含事件数据的 MouseEventArgs

示例

下面的代码示例演示如何重写 OnMouseHover 派生类中的 和 OnMouseMove 方法。 若要运行该示例,请将以下代码粘贴到新窗体中,并将此类粘贴到表单后面,形成相同的文件。 将 类型的 FunButton 按钮添加到窗体。

C#
public class FunButton:
    Button

{
    protected override void OnMouseHover(System.EventArgs e)
    {

        // Get the font size in Points, add one to the
        // size, and reset the button's font to the larger
        // size.
        float fontSize = Font.SizeInPoints;
        fontSize += 1;
        System.Drawing.Size buttonSize = Size;
        this.Font = new System.Drawing.Font(
            Font.FontFamily, fontSize, Font.Style);

        // Increase the size width and height of the button 
        // by 5 points each.
        Size = new System.Drawing.Size(Size.Width+5, Size.Height+5);

        // Call myBase.OnMouseHover to activate the delegate.
        base.OnMouseHover(e);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {

        // Make the cursor the Hand cursor when the mouse moves 
        // over the button.
        Cursor = Cursors.Hand;

        // Call MyBase.OnMouseMove to activate the delegate.
        base.OnMouseMove(e);
    }

注解

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件

OnMouseMove 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnMouseMove(MouseEventArgs) 时,一定要调用基类的 OnMouseMove(MouseEventArgs) 方法,以便已注册的委托对事件进行接收。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅