日期:2014-05-18  浏览次数:20888 次

C#-怎么让鼠标移到contextMenuStrip某一行时,背景色变蓝色,坐等
很多软件的托盘图标的右键菜单都有这样的效果,QQ也有,我用了MouseMove和MouseEnter都不行么.......它们都会在鼠标移开后才变颜色,急求~~~

------解决方案--------------------
试了一下
MouseEnter是有效果的
只不过被默认的颜色覆盖了
当你MouseLeave时
默认的颜色没了
所以才出现你设置的颜色
C# code
        Boolean bPaint = false;
        private void MenuItemMouseEnter(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;

            if (menuItem != null)
            {
                bPaint = true;
                menuItem.Invalidate();
            }
        }

        private void MenuItemMouseLeave(object sender, EventArgs e)
        {
            bPaint = false; 
        }

        private void MenuItemPaint(object sender, PaintEventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;

            if (menuItem != null)
            {
                if (bPaint)
                {
                    e.Graphics.FillRectangle(Brushes.Red, 0, 0, menuItem.Width, menuItem.Height);
                    Brush brush = new SolidBrush(Color.Black);
                    Font font = new Font(menuItem.Font.Name, menuItem.Font.Size);
                    e.Graphics.DrawString(menuItem.Text, menuItem.Font, brush, 20, 2);
                }
            }
        }

------解决方案--------------------
用WPF吧,哥们。
------解决方案--------------------
用其他控件
Dev,ComponmentOne 等