为什么即使鼠标光标仍在 pictureBox 控件内也会触发 MouseLeave 事件?

问题描述 投票:0回答:1

这是代码中的部分:

pictureBox.MouseEnter += (s, e) =>
{
    pictureBox.BackColor = Color.FromArgb(50, Color.Black);
    pictureBox.Controls.Add(new Label
    {
        Text = tooltipText,
        ForeColor = Color.White,
        BackColor = Color.FromArgb(200, 0, 0, 0),
        AutoSize = false,
        TextAlign = ContentAlignment.MiddleCenter,
        Dock = DockStyle.Fill,
        Font = new Font("Arial", 10, FontStyle.Bold)
    });
};

pictureBox.MouseLeave += (s, e) =>
{
    pictureBox.BackColor = Color.Transparent;
    pictureBox.Controls.Clear();
};

问题是,当我将鼠标光标移动到 pictureBox 区域内时,然后当我在 pictureBox 区域内移动鼠标而不只留在内部时,有时会触发 MouseLeave 事件代码。

完整方法代码:

private void ArrangeImagesInCollage(string[] filePaths)
{
    int imageCount = filePaths.Length;
    int margin = 5;

    int columns = (int)Math.Ceiling(Math.Sqrt(imageCount));
    int rows = (int)Math.Ceiling((double)imageCount / columns);

    int panelWidth = panelCollage.Width - (columns + 1) * margin;
    int panelHeight = panelCollage.Height - (rows + 1) * margin;
    int imageWidth = panelWidth / columns;
    int imageHeight = panelHeight / rows;

    panelCollage.Controls.Clear();

    for (int i = 0; i < imageCount; i++)
    {
        string filePath = filePaths[i];
        Image img = Image.FromFile(filePath);

        PictureBox pictureBox = new PictureBox
        {
            Image = img,
            SizeMode = PictureBoxSizeMode.StretchImage,
            BorderStyle = BorderStyle.FixedSingle,
            Size = new Size(imageWidth, imageHeight),
            Padding = new Padding(3)
        };

        int row = i / columns;
        int col = i % columns;
        int x = col * (imageWidth + margin) + margin;
        int y = row * (imageHeight + margin) + margin;

        pictureBox.Location = new Point(x, y);

        ToolTip toolTip = new ToolTip();
        FileInfo fileInfo = new FileInfo(filePath);
        string tooltipText = $"Name: {fileInfo.Name}\nSize: {fileInfo.Length / 1024} KB\nDimensions: {img.Width}x{img.Height}";
        toolTip.SetToolTip(pictureBox, tooltipText);

        pictureBox.MouseEnter += (s, e) =>
        {
            pictureBox.BackColor = Color.FromArgb(50, Color.Black);
            pictureBox.Controls.Add(new Label
            {
                Text = tooltipText,
                ForeColor = Color.White,
                BackColor = Color.FromArgb(200, 0, 0, 0),
                AutoSize = false,
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Fill,
                Font = new Font("Arial", 10, FontStyle.Bold)
            });
        };

        pictureBox.MouseLeave += (s, e) =>
        {
            pictureBox.BackColor = Color.Transparent;
            pictureBox.Controls.Clear();
        };

        panelCollage.Controls.Add(pictureBox);
    }
} 
c# winforms
1个回答
0
投票

这工作完美。 相反,我使用绘制事件在图片框上创建标签子控件并将其添加到图片框。 我添加了一个本地 isIn bool 变量。

鼠标进入图片框的结果:

when entering with the mouse cursor to the pictureBox control area

离开时,回到原来:

back to the original state when moving out the mouse cursor from the pictureBox control area

private void ArrangeImagesInCollage(string[] filePaths)
{
    int imageCount = filePaths.Length;
    int margin = 5;

    int columns = (int)Math.Ceiling(Math.Sqrt(imageCount));
    int rows = (int)Math.Ceiling((double)imageCount / columns);

    int panelWidth = panelCollage.Width - (columns + 1) * margin;
    int panelHeight = panelCollage.Height - (rows + 1) * margin;
    int imageWidth = panelWidth / columns;
    int imageHeight = panelHeight / rows;

    panelCollage.Controls.Clear();

    for (int i = 0; i < imageCount; i++)
    {
        string filePath = filePaths[i];
        Image img = Image.FromFile(filePath);

        PictureBox pictureBox = new PictureBox
        {
            Image = img,
            SizeMode = PictureBoxSizeMode.StretchImage,
            BorderStyle = BorderStyle.FixedSingle,
            Size = new Size(imageWidth, imageHeight),
            Padding = new Padding(3)
        };

        int row = i / columns;
        int col = i % columns;
        int x = col * (imageWidth + margin) + margin;
        int y = row * (imageHeight + margin) + margin;

        pictureBox.Location = new Point(x, y);

        ToolTip toolTip = new ToolTip();
        FileInfo fileInfo = new FileInfo(filePath);
        string tooltipText = $"Name: {fileInfo.Name}\nSize: {fileInfo.Length / 1024} KB\nDimensions: {img.Width}x{img.Height}";

        bool isIn = false;

        pictureBox.Paint += (s, e) =>
        {
            if (isIn)
            {
                // Define a semi-transparent overlay color
                Color overlayColor = Color.FromArgb(200, 0, 0, 0);

                // Draw the overlay rectangle
                e.Graphics.FillRectangle(new SolidBrush(overlayColor), 0, 0, pictureBox.Width, pictureBox.Height);

                // Define the font for the text
                Font font = new Font("Arial", 10, FontStyle.Bold);

                // Measure the size of the text
                SizeF textSize = e.Graphics.MeasureString(tooltipText, font);

                // Calculate the centered position for the text
                float xx = (pictureBox.Width - textSize.Width) / 2;
                float yy = (pictureBox.Height - textSize.Height) / 2;

                // Draw the text centered within the PictureBox
                e.Graphics.DrawString(tooltipText, font, Brushes.White, new PointF(xx, yy));
            }
            else
            {
                // Set the background color back to transparent when not hovering
                pictureBox.BackColor = Color.Transparent;
            }
        };

        pictureBox.MouseEnter += (s, e) =>
        {
            isIn = true;
            pictureBox.Invalidate();
        };

        pictureBox.MouseLeave += (s, e) =>
        {
            isIn = false;
            pictureBox.Invalidate();
        };

        panelCollage.Controls.Add(pictureBox);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.