110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
public class CustomMenuItem : ToolStripMenuItem
{
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
var rect = new Rectangle(0, 0, this.Size.Height, this.Size.Width);
if (IsMouseHover == true)
{
using (var b = new SolidBrush(Color.FromArgb(49, 106, 197)))
{
e.Graphics.FillRectangle(b, rect);
}
}
else
{
using (var b = new SolidBrush(Color.FromArgb(255, 255, 255)))
{
e.Graphics.FillRectangle(b, rect);
}
}
SolidBrush drawBrush = new SolidBrush(Color.Black);
System.Drawing.Size txtSize = TextRenderer.MeasureText(this.Text, this.Font);
PointF drawPoint = new PointF(10, (this.Height - txtSize.Height) / 2);
e.Graphics.DrawString(this.Text.ToString(), this.Font, drawBrush, drawPoint);
}
bool IsMouseHover = false;
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
IsMouseHover = true;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
IsMouseHover = false;
}
}
}
好像没有起到效果,麻烦试试看。
[/quote]
在两个
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
IsMouseHover = true;
this.Invalidate(); //要加这句
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
IsMouseHover = false;
this.Invalidate();//要加这句
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
public class CustomMenuItem : ToolStripMenuItem
{
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
var rect = new Rectangle(0, 0, this.Size.Height, this.Size.Width);
if (IsMouseHover == true)
{
using (var b = new SolidBrush(Color.FromArgb(49, 106, 197)))
{
e.Graphics.FillRectangle(b, rect);
}
}
else
{
using (var b = new SolidBrush(Color.FromArgb(255, 255, 255)))
{
e.Graphics.FillRectangle(b, rect);
}
}
SolidBrush drawBrush = new SolidBrush(Color.Black);
System.Drawing.Size txtSize = TextRenderer.MeasureText(this.Text, this.Font);
PointF drawPoint = new PointF(10, (this.Height - txtSize.Height) / 2);
e.Graphics.DrawString(this.Text.ToString(), this.Font, drawBrush, drawPoint);
}
bool IsMouseHover = false;
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
IsMouseHover = true;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
IsMouseHover = false;
}
}
}
好像没有起到效果,麻烦试试看。