这是代码中的部分:
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);
}
}
这工作完美。 相反,我使用绘制事件在图片框上创建标签子控件并将其添加到图片框。 我添加了一个本地 isIn bool 变量。
鼠标进入图片框的结果:
离开时,回到原来:
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);
}
}