You could estimate the location of a rectangle that encompases the bounds of
the text by using a GetTextExtent method. The drawback is that it requires a
Graphics object in order to know the context in which to draw, so you'll have
to do that in a Paint event (or OnPaint override). For example (which
assumes there is a button1 object, in a class named Form1, with a Label
control whose variable is named label1, button1_MouseMove is subscribed to
button1's MouseMove event, and Form1_Paint is subscribed to Form1's Paint
event):
private Rectangle textRectangle;
private void Form1_Paint ( object sender, PaintEventArgs e )
{
VisualStyleRenderer vsr = new
VisualStyleRenderer(VisualStyleElement.CreateElement(VisualStyleElement.Button.PushButton.Normal.ClassName,
VisualStyleElement.Button.PushButton.Normal.Part,
VisualStyleElement.Button.PushButton.Normal.State));
// get the extent of the text in a button relative to the button.
textRectangle = vsr.GetTextExtent(e.Graphics, button1.Bounds, button1.Text,
TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter);
}
private void button1_MouseMove ( object sender, MouseEventArgs e )
{
if (textRectangle.Contains(e.Location))
{
label1.Text = "Over button text";
}
else
{
label1.Text = "NOT Over button text";
}
}