关于ToolStripMenuItem重绘问题,救命问题

yunhaiC QQ654777694 2015-04-02 05:27:54



第一张图,用base.OnPaint(e);当鼠标放在其中一个MenuItem上的时候,就出现蓝色框子框住。
第二张图,我把base.OnPaint(e);注释掉,重绘文字,但是当鼠标放在上面的时候,没有出现蓝色框子。
因为篇幅问题,我就不叙述为什么我要用重绘文字的方式。

现在问题是:如果我用第二张图的方法重绘文字,那当鼠标放在MenuItem上的那种蓝色框子的效果,应该怎么实现呢?
...全文
351 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
本拉灯 2015-04-02
  • 打赏
  • 举报
回复
引用 6 楼 yunhaiC 的回复:
[quote=引用 5 楼 wyd1520 的回复:] [quote=引用 4 楼 yunhaiC 的回复:] [quote=引用 3 楼 wyd1520 的回复:] 就在你的那个 //base.OnPaint(e); if(mouseHover) //判断鼠标是否在当前项 { 这里画框; } 这里画文字代码;
OnMouseHover里面不能使用Graphics,也就是说无法去画框。[/quote] OnMouseHover 这里面是 { ismouseHover=true } OnMouseLeave 这里面是 { ismouseHover=false } 然后Paint里面的if就调用这个ismouseHover 是否要不要画框,明白没? [/quote]
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();//要加这句 }
  • 打赏
  • 举报
回复
引用 5 楼 wyd1520 的回复:
[quote=引用 4 楼 yunhaiC 的回复:] [quote=引用 3 楼 wyd1520 的回复:] 就在你的那个 //base.OnPaint(e); if(mouseHover) //判断鼠标是否在当前项 { 这里画框; } 这里画文字代码;
OnMouseHover里面不能使用Graphics,也就是说无法去画框。[/quote] OnMouseHover 这里面是 { ismouseHover=true } OnMouseLeave 这里面是 { ismouseHover=false } 然后Paint里面的if就调用这个ismouseHover 是否要不要画框,明白没? [/quote]
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;
        }
    }
}
好像没有起到效果,麻烦试试看。
本拉灯 2015-04-02
  • 打赏
  • 举报
回复
引用 4 楼 yunhaiC 的回复:
[quote=引用 3 楼 wyd1520 的回复:] 就在你的那个 //base.OnPaint(e); if(mouseHover) //判断鼠标是否在当前项 { 这里画框; } 这里画文字代码;
OnMouseHover里面不能使用Graphics,也就是说无法去画框。[/quote] OnMouseHover 这里面是 { ismouseHover=true } OnMouseLeave 这里面是 { ismouseHover=false } 然后Paint里面的if就调用这个ismouseHover 是否要不要画框,明白没?
  • 打赏
  • 举报
回复
引用 3 楼 wyd1520 的回复:
就在你的那个 //base.OnPaint(e); if(mouseHover) //判断鼠标是否在当前项 { 这里画框; } 这里画文字代码;
OnMouseHover里面不能使用Graphics,也就是说无法去画框。
本拉灯 2015-04-02
  • 打赏
  • 举报
回复
就在你的那个 //base.OnPaint(e); if(mouseHover) //判断鼠标是否在当前项 { 这里画框; } 这里画文字代码;
  • 打赏
  • 举报
回复
引用 1 楼 wyd1520 的回复:
你不能把Base.Pant给注释掉,画蓝框的代码在里面的。 除非你自己再实现画蓝框。
朋友,如何自己画呢,在什么方法里面重绘蓝框
本拉灯 2015-04-02
  • 打赏
  • 举报
回复
你不能把Base.Pant给注释掉,画蓝框的代码在里面的。 除非你自己再实现画蓝框。

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧