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

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



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

现在问题是:如果我用第二张图的方法重绘文字,那当鼠标放在MenuItem上的那种蓝色框子的效果,应该怎么实现呢?
...全文
336 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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给注释掉,画蓝框的代码在里面的。 除非你自己再实现画蓝框。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace HistoryMenu { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "*.*(所有文件)|*.*";//设置打开文件格式 if (openFileDialog1.ShowDialog() == DialogResult.OK)//判断是否打开选择文件对话框 { StreamWriter s = new StreamWriter("Menu.ini", true);//实例化写入流对象 s.WriteLine(openFileDialog1.FileName);//向INI文件写入内容 s.Flush();//清除缓冲区 s.Close();//关闭写入流 System.Diagnostics.Process.Start(openFileDialog1.FileName);//打开选择的文件 } Form1_Load(sender, e);//重新加载菜单 } private void Form1_Load(object sender, EventArgs e) { 文件ToolStripMenuItem.DropDownItems.Clear();//清空菜单 ToolStripMenuItem menuitem1 = new ToolStripMenuItem("打开");//实例化打开菜单 文件ToolStripMenuItem.DropDownItems.Insert(0, menuitem1);//添加打开菜单 menuitem1.Click += new EventHandler(打开ToolStripMenuItem_Click);//为打开菜单指定单击事件 ToolStripMenuItem menuitem2 = new ToolStripMenuItem("退出");//实例化退出菜单 文件ToolStripMenuItem.DropDownItems.Insert(1, menuitem2);//添加退出菜单 StreamReader sr = new StreamReader("Menu.ini");//实例化读取流对象 int i = this.文件ToolStripMenuItem.DropDownItems.Count - 1;//定义历史记录位置 while (sr.Peek() >= 0)//从INI文件读取历史记录 { ToolStripMenuItem menuitem = new ToolStripMenuItem(sr.ReadLine());//实例化历史菜单 this.文件ToolStripMenuItem.DropDownItems.Insert(i, menuitem);//添加历史菜单 i++;//重新指定历史记录位置 menuitem.Click += new EventHandler(menuitem_Click);//为历史菜单指定单击事件 } sr.Close();//关闭读取流 } private void menuitem_Click(object sender, EventArgs e) { try { ToolStripMenuItem menu = (ToolStripMenuItem)sender;//获取菜单单击项 System.Diagnostics.Process.Start(menu.Text);//根据历史菜单打开指定文件 } catch { } } } }

111,098

社区成员

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

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

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