是否能够完全绘制MenuItem?

时间:2015-07-02 18:49:17

标签: c# winforms contextmenu menuitem ownerdrawn

我创建了我的控件,可以使用MenuItem DrawItem()和MeasureItem()在表单中重绘所有MenuItem。 在我运行该程序后,MenuItem被重绘,正如我所料,但是有些事情让我感到烦恼,那就是MenuItem没有完全绘制(留下小的空白区域)就像这样

My Application with custom MenuItem DrawItem() 我的应用程序与自定义MenuItem DrawItem()

如图所示,有一些未绘制的小空白,能够在不留下任何空白的情况下画出来

AIMP3 Context Menu AIMP3上下文菜单

在C#?如果是这样,我必须使用哪些方法?

这是我的一些代码

        public static void RenderMenu(Object sender, DrawItemEventArgs e, Boolean isUseKeyboardAccelerator, Image image)
        {
            Boolean selected = ( e.State & DrawItemState.Selected ) == DrawItemState.Selected;

            e.Graphics.FillRectangle(StyleBrush.Menu, 0, 0, e.Bounds.Width, e.Bounds.Height);

            if ( selected )
                e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);

            if ( ( (MenuItem)sender ).Text == "-" )
            {
                Int32 vCenter = e.Bounds.Top + ( e.Bounds.Height / 2 ) - 1;

                e.Graphics.DrawLine(SystemPens.ControlDark, e.Bounds.Left + 1, vCenter, ( e.Bounds.Left + e.Bounds.Width - 2 ), vCenter);
                e.Graphics.DrawLine(SystemPens.ControlLightLight, e.Bounds.Left + 1, vCenter + 1, ( e.Bounds.Left + e.Bounds.Width - 2 ), vCenter + 1);
            }
            else
            {
                RenderString(sender, e, selected, isUseKeyboardAccelerator);

                if ( ( (MenuItem)sender ).Checked )
                {
                    MenuGlyph menuGlyph;

                    if ( ( (MenuItem)sender ).RadioCheck )
                        menuGlyph = MenuGlyph.Bullet;
                    else
                        menuGlyph = MenuGlyph.Checkmark;

                    ControlPaint.DrawMenuGlyph(e.Graphics,
                        e.Bounds.Left + ( StyleVariable.MarginLeft + StyleVariable.GlyphBackgroundWidth + StyleVariable.MarginRight - SystemInformation.MenuCheckSize.Width ) / 2,
                        e.Bounds.Top + ( e.Bounds.Height - SystemInformation.MenuCheckSize.Height ) / 2 + 1,
                        SystemInformation.MenuCheckSize.Width, SystemInformation.MenuCheckSize.Height, menuGlyph,
                        selected ? SystemColors.HighlightText : SystemColors.MenuText,
                        selected ? SystemColors.Highlight : SystemColors.Menu);
                }
                else
                {
                    if ( image != null )
                    {
                        if ( ( (MenuItem)sender ).Enabled )
                            e.Graphics.DrawImage(image, e.Bounds.Left + StyleVariable.MarginLeft, e.Bounds.Top + ( ( e.Bounds.Height - StyleVariable.GlyphBackgroundHeight ) / 2 ), StyleVariable.GlyphBackgroundWidth, StyleVariable.GlyphBackgroundHeight);
                        else
                            ControlPaint.DrawImageDisabled(e.Graphics, image, e.Bounds.Left + StyleVariable.MarginLeft, e.Bounds.Top + ( ( e.Bounds.Height - StyleVariable.GlyphBackgroundHeight ) / 2 ), SystemColors.Menu);
                    }
                }
            }
        }

0 个答案:

没有答案