如何添加信息文本菜单带状隔板在vb.net或C# [英] How to add informative text to menu strip separator in vb.net or C#

查看:212
本文介绍了如何添加信息文本菜单带状隔板在vb.net或C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在窗口的形式。我想信息文本添加到菜单分隔符。任何人都可以建议如何做到这一点?
为例分离器应该会出现如下图所示。

In Window form. I want to add informative text to the Menu separator. Can anyone suggest how to do that? for example separator should appear like below

Menu Item1 
Menu Item2 
----- Separator title ----- 
Menu Item3
Menu Item4 

经过尝试很多,我只能够添加简单的分隔符。任何帮助将是巨大的。

After Lots of attempts I am only able to add simple separator. Any help would be Great

推荐答案

您可以创建自己的菜单分隔符

You can create your own menu separator

Public Class TextToolStripSeparator
    Inherits ToolStripMenuItem

    Public Overrides ReadOnly Property CanSelect() As Boolean
        Get
            Return DesignMode
        End Get
    End Property

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set
            value = value.Trim("-"C, " "C)
            MyBase.Text = "---- " & value & " -------"
        End Set
    End Property
End Class

它会自动出现在如果在同一个项目中定义的插入上下文菜单。

It will automatically appear in the "Insert" context menu if it is defined in the same project.

我也试图重写的OnPaint 方法。没有成功。有一些神奇的事情,以防止业主绘制的对象出现。

I also tried to override the OnPaint Method. Without success. There is some magic going on that prevents owner drawn objects to appear.

修改

最后,一些研究和大量的试验和错误之后,我发现了一个更令人满意的解决方案。这是结果会如何看

Finally, after some research and a lot of trial and error I found a more satisfying solution. This is how the result will look

首先,我们创造我们自己的工具条分离器类。

First, we create our own tools strip separator class.

Public Class TextToolStripSeparator
    Inherits ToolStripMenuItem

    Public Overrides ReadOnly Property CanSelect() As Boolean
        Get
            Return DesignMode
        End Get
    End Property

    Public Overrides ReadOnly Property HasDropDownItems() As Boolean
        Get
            Return False
        End Get
    End Property
End Class

正如你所看到的,这是非常简单的。更复杂的部分进入我们自己的的MenuStrip 类。在这里,我们提供了一个定制的的ToolStripRenderer ,其中,中端,做这项工作。

As you can see, it is very simple. The more complicated part goes in our own MenuStrip class. Here we provide a customized ToolStripRenderer, which, in the end, does the job.

Public Class MenuStripEx
    Inherits MenuStrip

    Public Sub New()
        Me.Renderer = New ToolStripRendererEx()
    End Sub

    Private Class ToolStripRendererEx
        Inherits ToolStripProfessionalRenderer

        Protected Overrides Sub OnRenderItemText(e As ToolStripItemTextRenderEventArgs)
            Const  flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter

            Dim item As ToolStripItem = e.Item
            If TypeOf item Is TextToolStripSeparator Then
                Dim textWidth As Integer = TextRenderer.MeasureText(item.Text, item.Font).Width
                Dim rect As Rectangle = e.TextRectangle
                rect.Width = e.ToolStrip.Width - rect.Left - 3
                TextRenderer.DrawText(e.Graphics, item.Text, item.Font, rect, Color.DimGray, flags)

                Dim y As Integer = rect.Y + rect.Height \ 2
                Dim margin As Integer = (rect.Width - textWidth) \ 2
                e.Graphics.DrawLine(Pens.DarkGray, rect.X, y, rect.X + margin, y)
                e.Graphics.DrawLine(Pens.DarkGray, rect.Right - margin, y, rect.Right, y)
            Else
                MyBase.OnRenderItemText(e)
            End If
        End Sub
    End Class
End Class

这篇关于如何添加信息文本菜单带状隔板在vb.net或C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆