[英]How do I change TabControl Close Button Image on mouse hover?
I'm using the code from this answer , and it's working perfectly!我正在使用此答案中的代码,并且运行良好!
But, now I need to add a hover image, because it feels dull without one.但是,现在我需要添加一个 hover 图像,因为没有它感觉很沉闷。
I already drew the images, I just need to change the image on hover, and change it back when it stops hovering.我已经画了图像,我只需要在hover上更改图像,并在停止悬停时将其更改回来。
There are suggestions for Invalidate()
, but I don't quite understand exactly how to use it . Invalidate()
有一些建议,但我不太了解如何使用它。
I tried putting the following code into the MouseMove
event of the TabControl
,我尝试将以下代码放入TabControl
的MouseMove
事件中,
for (var i = 0; i < this.tabControl1.TabPages.Count; i++)
{
var tabRect = this.tabControl1.GetTabRect(i);
tabRect.Inflate(-2, -2);
var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
CloseImage.Width,
CloseImage.Height);
if (imageRect.Contains(e.Location))
{
isHover = true;
isNormal = false;
Invalidate(tabRect);
}
else
{
isNormal = true;
isHover = false;
Invalidate(tabRect);
}
}
but that doesn't seem to be working either.但这似乎也不起作用。
The variables isNormal
and isHover
have already been created in earlier code, and inside of the DrawItem
event, I have:变量isNormal
和isHover
已经在早期的代码中创建,在DrawItem
事件中,我有:
try
{
if (isHover == false && isNormal == true)
{
CloseImage = Properties.Resources.normalImage;
var tabRect = this.tabControl1.GetTabRect(e.Index);
tabRect.Inflate(-2, -2);
var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
CloseImage.Width,
CloseImage.Height);
var sf = new StringFormat(StringFormat.GenericDefault);
if (this.tabControl1.RightToLeft == System.Windows.Forms.RightToLeft.Yes &&
this.tabControl1.RightToLeftLayout == true)
{
tabRect = GetRTLCoordinates(this.tabControl1.ClientRectangle, tabRect);
imageRect = GetRTLCoordinates(this.tabControl1.ClientRectangle, imageRect);
sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text,
this.Font, Brushes.Black, tabRect, sf);
e.Graphics.DrawImage(CloseImage, imageRect.Location);
}
else if (isHover == true && isNormal == false)
{
CloseImage = Properties.Resources.hoverImage;
var tabRect = this.tabControl1.GetTabRect(tabControl1.SelectedIndex);
tabRect.Inflate(-2, -2);
var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
CloseImage.Width,
CloseImage.Height);
var sf = new StringFormat(StringFormat.GenericDefault);
if (this.tabControl1.RightToLeft == System.Windows.Forms.RightToLeft.Yes &&
this.tabControl1.RightToLeftLayout == true)
{
tabRect = GetRTLCoordinates(this.tabControl1.ClientRectangle, tabRect);
imageRect = GetRTLCoordinates(this.tabControl1.ClientRectangle, imageRect);
sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
e.Graphics.DrawString(this.tabControl1.TabPages[tabControl1.SelectedIndex].Text,
this.Font, Brushes.Black, tabRect, sf);
e.Graphics.DrawImage(CloseImage, imageRect.Location);
isHover = false;
isNormal = true;
}
}
catch (Exception) { }
But it still isn't working.但它仍然无法正常工作。
My apologies if my question isn't clear:)如果我的问题不清楚,我深表歉意:)
I'm using the code from this answer , and it's working perfectly!我正在使用此答案中的代码,并且运行良好!
But, now I need to add a hover image, because it feels dull without one.但是,现在我需要添加一个 hover 图像,因为没有它感觉很沉闷。
I already drew the images, I just need to change the image on hover, and change it back when it stops hovering.我已经画了图像,我只需要在hover上更改图像,并在停止悬停时将其更改回来。
There are suggestions for Invalidate()
, but I don't quite understand exactly how to use it . Invalidate()
有一些建议,但我不太了解如何使用它。
I tried putting the following code into the MouseMove
event of the TabControl
,我尝试将以下代码放入TabControl
的MouseMove
事件中,
for (var i = 0; i < this.tabControl1.TabPages.Count; i++)
{
var tabRect = this.tabControl1.GetTabRect(i);
tabRect.Inflate(-2, -2);
var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
CloseImage.Width,
CloseImage.Height);
if (imageRect.Contains(e.Location))
{
isHover = true;
isNormal = false;
Invalidate(tabRect);
}
else
{
isNormal = true;
isHover = false;
Invalidate(tabRect);
}
}
but that doesn't seem to be working either.但这似乎也不起作用。
The variables isNormal
and isHover
have already been created in earlier code, and inside of the DrawItem
event, I have:变量isNormal
和isHover
已经在早期的代码中创建,在DrawItem
事件中,我有:
try
{
if (isHover == false && isNormal == true)
{
CloseImage = Properties.Resources.normalImage;
var tabRect = this.tabControl1.GetTabRect(e.Index);
tabRect.Inflate(-2, -2);
var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
CloseImage.Width,
CloseImage.Height);
var sf = new StringFormat(StringFormat.GenericDefault);
if (this.tabControl1.RightToLeft == System.Windows.Forms.RightToLeft.Yes &&
this.tabControl1.RightToLeftLayout == true)
{
tabRect = GetRTLCoordinates(this.tabControl1.ClientRectangle, tabRect);
imageRect = GetRTLCoordinates(this.tabControl1.ClientRectangle, imageRect);
sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text,
this.Font, Brushes.Black, tabRect, sf);
e.Graphics.DrawImage(CloseImage, imageRect.Location);
}
else if (isHover == true && isNormal == false)
{
CloseImage = Properties.Resources.hoverImage;
var tabRect = this.tabControl1.GetTabRect(tabControl1.SelectedIndex);
tabRect.Inflate(-2, -2);
var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
CloseImage.Width,
CloseImage.Height);
var sf = new StringFormat(StringFormat.GenericDefault);
if (this.tabControl1.RightToLeft == System.Windows.Forms.RightToLeft.Yes &&
this.tabControl1.RightToLeftLayout == true)
{
tabRect = GetRTLCoordinates(this.tabControl1.ClientRectangle, tabRect);
imageRect = GetRTLCoordinates(this.tabControl1.ClientRectangle, imageRect);
sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
e.Graphics.DrawString(this.tabControl1.TabPages[tabControl1.SelectedIndex].Text,
this.Font, Brushes.Black, tabRect, sf);
e.Graphics.DrawImage(CloseImage, imageRect.Location);
isHover = false;
isNormal = true;
}
}
catch (Exception) { }
But it still isn't working.但它仍然无法正常工作。
My apologies if my question isn't clear:)如果我的问题不清楚,我深表歉意:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.