簡體   English   中英

如何檢查鼠標是否在上下文菜單欄上

[英]How to check if mouse is on the context menu strip

我有一個winform,其中包含一個按鈕,當鼠標懸停在其上時,該菜單的上下文菜單帶會下拉。

在按鈕的鼠標離開事件中檢查鼠標是否在上下文菜單欄中的條件不起作用。

private void button1_MouseHover(object sender, EventArgs e)
{           
    contextMenuStrip1.Show(button1, new Point(0, button1.Height));
}

private void button1_MouseLeave(object sender, EventArgs e)
{
    if (contextMenuStrip1.ClientRectangle.Contains(PointToClient(Cursor.Position)))
    {
        return;
    }
    else
    {
        contextMenuStrip1.Hide();
    }
}      

這不准確,但是您似乎希望此方法有效。

在此處輸入圖片說明

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp27
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            contextMenuStrip1.MouseLeave += ContextMenuStrip1_MouseLeave;
        }

        private void ContextMenuStrip1_MouseLeave(object sender, EventArgs e)
        {
            contextMenuStrip1.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            contextMenuStrip1.Show(button1, new Point(0, button1.Height));
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM