MenuStrip dropdown 메뉴가 열렸을때 배경색 설정관련 도움 부탁드립니다.

안녕하세요.
MenuStrip을 이용해 메뉴를 구성했습니다.

MenuStrip 배경과 색상을 ToolStripProfessionalRenderer 으로 재정의했습니다.

MenuStrip을 투명한 배경에 하얀색 글씨를 쓰고 있습니다.
dropdown 메뉴가 열렸을때, 상위 메뉴의 글씨가 안보입니다.

하위메뉴가 열렸을때 상위메뉴의 배경색을 다른색으로 설정하고 싶습니다.

이것 저것 해봐도, 적용이 안되네요.

참고할 키워드 부탁드립니다.

image

Color Table을 재정의하는 부분의 코드도 같이 올려주시면 좋을 것 같네요.

오늘 외국형님에게 큰 도움을 받았습니다.
필요하신 분들 계실듯해, 자답합니다.

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form2 : 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 Form2()
        {
            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);
    }
}

image

2개의 좋아요