Help with setting the background color when the MenuStrip dropdown menu is opened.

Youngs 40 Reputation points
2024-05-22T09:00:16.8533333+00:00

hello.

I have organized my menu using MenuStrip.

I have overridden the MenuStrip background and color with ToolStripProfessionalRenderer.

I am using the MenuStrip with white text on a transparent background.

When the dropdown menu is opened, the text of the parent menu is not visible.

I want to set the parent menu to a different background color when the submenu is opened.

I've tried this and that, but it doesn't work.

Please provide keywords for reference..

1111

Sorry about that.

You now know how to upload an image.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,479 questions
{count} votes

Accepted answer
  1. KOZ6.0 5,735 Reputation points
    2024-05-26T03:51:13.1266667+00:00

    This is sample:

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    public partial class Form1 : Form
    {
        private readonly MenuStrip  menuStrip = new MenuStrip();
        private readonly ToolStripMenuItem mnuFile = new ToolStripMenuItem();
        private readonly ToolStripMenuItem mnuEdit = new ToolStripMenuItem();
        private readonly ToolStripMenuItem mnuFileOpen = new ToolStripMenuItem();
        private readonly ToolStripMenuItem mnuFileExit = new ToolStripMenuItem();
        private readonly Color menuItemBackColor = Color.White;
    
        public Form1() {
            InitializeComponent();
    
            Opacity = 0.7d;
            BackColor = Color.Gray;
    
            menuStrip.SuspendLayout();
    
            menuStrip.Font = new Font("Arial", 12F, FontStyle.Bold);
            menuStrip.ForeColor = Color.White;
            menuStrip.BackColor = Color.Transparent;
            menuStrip.Renderer = new CustomRenderer();
    
            mnuFile.Text = "File(&F)";
            mnuFile.DropDownOpened += MnuFile_DropDownOpened;
    
            mnuEdit.Text = "Edit(&E)";
    
            mnuFileOpen.Text = "Open(&O)";
            mnuFileExit.BackColor = menuItemBackColor;
    
            mnuFileExit.Text = "Exit(&X)";
            mnuFileExit.Click += MnuFileExit_Click;
            mnuFileExit.BackColor = menuItemBackColor;
    
            mnuFile.DropDownItems.AddRange(new ToolStripItem[] { mnuFileOpen, mnuFileExit });
            menuStrip.Items.AddRange(new ToolStripItem[] { mnuFile, mnuEdit });
    
            Controls.Add(menuStrip);
            menuStrip.ResumeLayout();
        }
    
        private void MnuFile_DropDownOpened(object sender, EventArgs e) {
            var item = (ToolStripMenuItem)sender;
            IntPtr hwnd = item.DropDown.Handle;
            int styleEx = GetWindowLong(hwnd, GWL_EXSTYLE);
            if ((styleEx & WS_EX_LAYERED) == 0) {
                SetWindowLong(hwnd, GWL_EXSTYLE, styleEx | WS_EX_LAYERED);
                SetLayeredWindowAttributes(hwnd, 0, 180, LWA_ALPHA);
            }
        }
    
        private void MnuFileExit_Click(object sender, EventArgs e) {
            Close();
        }
    
        class CustomRenderer : ToolStripRenderer
        {
            public Color SelectionBackColor { get; set; } = Color.Black;
            public Color SelectionForeColor { get; set; } = Color.White;
    
            protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e) {
                Rectangle bounds = new Rectangle(Point.Empty, e.Item.Size);
                Color backColor = e.Item.Selected ? SelectionBackColor : e.Item.BackColor;
                using (var backBrush = new SolidBrush(backColor)) {
                    e.Graphics.FillRectangle(backBrush, bounds);
                }
            }
    
            protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e) {
                e.TextColor = e.Item.Selected ? SelectionForeColor : e.Item.ForeColor;
                base.OnRenderItemText(e);
            }
        }
    
        const int WS_EX_LAYERED = 0x80000;
        const int GWL_EXSTYLE = -20;
        const int LWA_COLORKEY = 0x00000001;
        const int LWA_ALPHA = 0x00000002;
    
        [DllImport("user32.dll")]
        static extern bool SetLayeredWindowAttributes(
                IntPtr hwnd, int crKey, byte bAlpha, int dwFlags);
    
        [DllImport("user32.dll")]
        static extern int GetWindowLong(IntPtr hwnd, int nIndex);
    
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
    }
    

    enter image description here

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Youngs 40 Reputation points
    2024-05-28T09:24:54.8133333+00:00

    I understood exactly what you meant.

    The sample code impressed me greatly.

    Thank you very much.

    but I have one more question.

    After applying the source, I don't see the backgroundImage of the button that I added as a ToolStripButton.

    Could you please help me with this?