我正在构建自定义控件,该控件将显示瓷砖,例如彩色网格。我已经完成了绘制,滚动和基本逻辑的工作,但是在为每个图块创建工具提示时遇到了问题。
每个图块颜色取决于与该图块“绑定”的数据。
我将尝试描述我的想法:
上图显示了我的控件,我在那里绘制了4个正方形,当用户将控件悬停在控件的不同部分时,我想显示不同的工具提示。下面是我的工具提示(红色小矩形),下面是标准WinForms Chart组件。
我想在控件中得到这种行为(工具提示不在控件内,因此长文本可以正确显示)。
以下是我的控制代码,仅包含基本功能:
public sealed class UC1 : UserControl
{
private bool _showTooltip;
private string _tooltipText;
private Point _mousePosition;
public UC1()
{
MinimumSize = new Size(100, 100);
Size = new Size(100, 100);
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.LightGoldenrodYellow, 0, 0, Width/2, Height/2);
e.Graphics.FillRectangle(Brushes.LightGray, Width/2, 0, Width, Height/2);
e.Graphics.FillRectangle(Brushes.LightSlateGray, 0, Height/2, Width/2, Height);
e.Graphics.FillRectangle(Brushes.LightSteelBlue, Width/2, Height/2, Width, Height);
using (var p = new Pen(Color.Black, 2))
{
e.Graphics.DrawLine(p, Width/2, 0, Width/2, Height);
e.Graphics.DrawLine(p, 0, Height/2, Width, Height/2);
}
if (_showTooltip)
{
SizeF c = e.Graphics.MeasureString(_tooltipText, DefaultFont);
int width = (int) c.Width;
int height = (int) c.Height;
const int offset = 12;
var x = _mousePosition.X + width + offset > Width ? _mousePosition.X - width : _mousePosition.X + offset;
var y = _mousePosition.Y + height + offset > Height ? _mousePosition.Y - height : _mousePosition.Y + offset;
e.Graphics.FillRectangle(Brushes.Red, x, y, width, height);
e.Graphics.DrawString(_tooltipText, DefaultFont, Brushes.Black, x, y);
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.X > 0 && e.X < Width/2 && e.Y > 0 && e.Y < Height/2)
{
Debug.WriteLine("1,1 square");
_tooltipText = "1,1";
}
else if (e.X > Width/2 && e.X < Width && e.Y > 0 && e.Y < Height/2)
{
Debug.WriteLine("1,2 square");
_tooltipText = "1,2";
}
else if (e.X > 0 && e.X < Width/2 && e.Y > Height/2 && e.Y < Height)
{
Debug.WriteLine("2,1 square");
_tooltipText = "2,1";
}
else if (e.X > Width/2 && e.X < Width && e.Y > Height/2 && e.Y < Height)
{
Debug.WriteLine("2,2 square");
_tooltipText = "2,2";
}
_mousePosition = e.Location;
_showTooltip = true;
Refresh();
}
protected override void OnMouseLeave(EventArgs e)
{
_showTooltip = false;
Refresh();
base.OnMouseLeave(e);
}
}
我发现了类似的问题:如何在Windows.Forms中制作一个浮动(工具提示)控件?但我想避免创建自定义工具提示,而是使用标准提示。
当悬停在不同的图块上时,如何显示将更改其文本的标准工具提示。我想将所有内容添加到我的用户控件中,因此不必添加任何代码即可托管我的控件。
为什么您尝试自己绘制工具提示而不是使用系统提示?
只需添加一个到UC类
// private bool _showTooltip; ?? probably not needed any more..
private string _tooltipText;
// private Point _mousePosition; ??..
ToolTip ttip = new ToolTip();
并设置如下:
// _mousePosition = e.Location; ??..
// _showTooltip = true; ??..
ttip.SetToolTip(this, _tooltipText); // use this in the mousemove
Refresh();
当然,现在您可以跳过整个绘画部分。
如果要控制显示位置,请ToolTip
使用ShowToolTip()
重载之一而不是SetToolTip()
.. !!
当两者都仍然存在时,这就是结果,越过UC的边界并显示了很好的阴影。
如果您确实希望ToolTip
与通常的外观有所不同,则可以将其设置为on并对其事件OwnerDraw
进行true
编码Draw
,就像使用GDI+
图形方法的任何其他控件一样。
更新:
存在一个固有的闪烁问题。有关解释,请参见汉斯的回答; 他的建议2和答案之一是有帮助的:
记住鼠标的最后一个位置,仅在鼠标位置更改时设置工具提示。
因此,我们需要添加最后一个工具提示位置:
Point tipLoc = Point.Empty;
我们测试并在鼠标移动中进行设置:
if (tipLoc != e.Location )
{
tipLoc = e.Location;
ttip.SetToolTip(this, _tooltipText);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句