【问题标题】:How to add Back and Fore Color on ContextMenu如何在 ContextMenu 上添加背景颜色和前景色
【发布时间】:2014-11-06 12:23:55
【问题描述】:

每当用户右键单击DataGridView 中的特定位置时,我都会显示ContextMenu。 我希望 ContextMenu 的项目根据其内容具有背景色和前景色。

由于ContextMenu 没有背景颜色或前景色属性,我该怎么做? 我尝试查找ContextMenuStrip,但这必须连接到我没有也不想使用的ToolStripButton

【问题讨论】:

  • 改用 ContextMenuStrip,提供您自己的color table
  • 好吧,我找到了一个使用我当前使用的 drawItem 处理程序的解决方案。该应用程序在很大程度上依赖于 ContextMenus,更改为其他内容意味着更改 100 行代码,而我必须只添加处理程序

标签: c# .net winforms datagridview


【解决方案1】:

为了更改MenuItem 的背景颜色,您需要指定一个绘制项目处理程序并将每个项目的所有者绘制设置为true。此外,要使颜色实际占用一些空间,您需要实现一个 MeasureMenuItem 处理程序。 比如

                color.MenuItems.Add(new MenuItem("#123456", menuHandler));
                color.MenuItems.Add(new MenuItem("Green", menuHandler));
                color.MenuItems.Add(new MenuItem("Red", menuHandler));
                foreach (MenuItem item in color.MenuItems)
                {
                    item.OwnerDraw = true;
                    item.DrawItem += item_DrawItem;
                    item.MeasureItem += MeasureMenuItem;
                }

以上代码连接了项目及其处理程序。

void item_DrawItem(object sender, DrawItemEventArgs e)
        {
            MenuItem cmb = sender as MenuItem;
            string color = SystemColors.Window.ToString();
            if (e.Index > -1)
            {
                color = cmb.Text;
            }
            if (checkHtmlColor(color))
            {

                e.DrawBackground();
                e.Graphics.FillRectangle(new SolidBrush(ColorTranslator.FromHtml(color)), e.Bounds);

                e.Graphics.DrawString(color, new Font("Lucida Sans", 10), new SolidBrush(ColorTranslator.FromHtml(color)), e.Bounds);

            }
        }

上面的代码获取 MenuItem 的内容,将其转换为一种颜色,为该颜色创建一个矩形并绘制它。

   void MeasureMenuItem(object sender, MeasureItemEventArgs e)
        {
            MenuItem m = (MenuItem)sender;
            Font font = new Font(Font.FontFamily, Font.Size, Font.Style);
            SizeF sze = e.Graphics.MeasureString(m.Text, font);
            e.ItemHeight = (int)sze.Height;
            e.ItemWidth = (int)sze.Width;
        }

最后,上面几行简单地测量了 MenuItem 在绘制之前应该占用的区域(基本上测量它的字符串内容的空间),因此 draw_item 处理程序知道要占用多少空间

【讨论】:

    【解决方案2】:

    我允许自己挖掘这篇文章,因为我遇到了同样的问题(在 ContextMenu 中为 MenuItem 添加背景颜色)并找到了这篇文章。 但答案似乎很复杂。所以我继续搜索并找到一个简单的解决方案:使用 ContextMenuStrip 和 ToolStripMenuItem 以下是遇到相同问题的用户的示例:

    ContextMenuStrip cMenu=new ContextMenuStrip();
    ToolStripMenuItem mi;
    // Item 1, null in constructor to say : no picture on the label
    mi=new ToolStripMenuItem("item 1",null , (s,a)=> actionOnClicItem1());
    mi.BackColor = Color.Red;
    cMenu.Items.add(mi);
    
    // Separator
    cMenu.Items.Add(new ToolStripSeparator());
    
    // Item 2
    mi=new ToolStripMenuItem("item 2",null , (s,a)=> actionOnClicItem2());
    mi.BackColor = Color.Blue;
    cMenu.Items.add(mi);
    
    // show the context menu near by the mouse pointer
    cMenu.Show(myDataGridView,new Point(e.X,e.Y));
    

    【讨论】:

      【解决方案3】:

      myToolStripMenuItem.GetCurrentParent().BackColor = Color.Red

      【讨论】:

        猜你喜欢
        • 2021-07-26
        • 1970-01-01
        • 2019-01-19
        • 1970-01-01
        • 2011-03-08
        • 2021-02-13
        • 2010-10-03
        • 1970-01-01
        • 2023-01-09
        相关资源
        最近更新 更多