gpt4 book ai didi

c#悬停时在控件周围绘制矩形

转载 作者:行者123 更新时间:2023-11-30 21:51:44 26 4
gpt4 key购买 nike

我重写了 Control 类的 OnPaint 方法来创建表情符号。表情符号已创建。但是我要在它悬停时在它周围添加一个矩形。

public class EmoticonControl : Control
{
public EmoticonControl(string unicode, Image image)
{
this.unicode = unicode;
this.image = image;
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Rectangle clipRectangle = e.ClipRectangle;
graphics.DrawImage(this.image, 0, 0, this.Size.Width, this.Size.Height);

// When hovered
Brush brush = new SolidBrush(this.selectedColor);
graphics.FillRectangle(new SolidBrush(selectionColor), clipRectangle);
graphics.DrawRectangle(new Pen(selectionBorderColor), clipRectangle.X, clipRectangle.Y, clipRectangle.Width - 1, clipRectangle.Height - 1);
//
}
private string unicode;
private Image image;

private Color selectedColor = SystemColors.Highlight;
private Color selectionColor = Color.FromArgb(50, 0, 0, 150);
private Color selectionBorderColor = SystemColors.Highlight;
}

最佳答案

您可以使用 UIElement.IsMouseOver 属性来了解您的控件是否处于悬停状态。

protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Rectangle clipRectangle = e.ClipRectangle;
graphics.DrawImage(this.image, 0, 0, this.Size.Width, this.Size.Height);

// When hovered
if (IsMouseOver)
{
Brush brush = new SolidBrush(this.selectedColor);
graphics.FillRectangle(new SolidBrush(selectionColor), clipRectangle);
graphics.DrawRectangle(new Pen(selectionBorderColor), clipRectangle.X, clipRectangle.Y, clipRectangle.Width - 1, clipRectangle.Height - 1);
}
}

关于c#悬停时在控件周围绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35284050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com