ContextMenu重绘

private void cm_DrawItem(object sender, DrawItemEventArgs e)
        {
            try
            {
                Color cms1 = SkinForm.GetColor("gv", "abcolor");
                Color cms2 = SkinForm.GetColor("gv", "rhbcolor");
                Color cms3 = SkinForm.GetColor("gv", "afcolor");

                MenuItem mnuItem = (MenuItem)sender;
               
                SolidBrush Brush1 = new SolidBrush(cms1);
                SolidBrush Brush2 = new SolidBrush(cms2);
                SolidBrush Brush3 = new SolidBrush(cms3);

                Pen cmPen = new Pen(SkinForm.GetColor("gv", "asfcolor"));

                StringFormat Format = new StringFormat();

                if ((e.State & DrawItemState.Selected) != 0)
                {
                    if (this.Enabled)
                    {
                        e.Graphics.FillRectangle(Brush1, e.Bounds);
                        e.Graphics.DrawRectangle(cmPen, e.Bounds.Left, e.Bounds.Y, e.Bounds.Width - 2, e.Bounds.Height - 2);
                    }
                }
                else
                {
                    e.Graphics.FillRectangle(Brush2, e.Bounds);
                }

                if (this.Text != "-")
                {
                    Rectangle DisplayRect = new Rectangle(e.Bounds.Left, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Bottom);
                    Format.Alignment = StringAlignment.Center;

                    if (this.Enabled)
                    {
                        e.Graphics.DrawString(mnuItem.Text, gv_songlist.Font, Brush3, DisplayRect, Format);
                    }
                    else
                    {
                        e.Graphics.DrawString(mnuItem.Text, gv_songlist.Font, Brush3, DisplayRect, Format);
                    }
                }
                else
                {
                    e.Graphics.DrawLine(cmPen, e.Bounds.Left, e.Bounds.Top, e.Bounds.Right, e.Bounds.Top);
                }
            }
            catch
            { }
        }

        private void cm_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            try
            {
                MenuItem mnuItem = (MenuItem)sender;
                e.ItemHeight = (int)e.Graphics.MeasureString(Text, gv_songlist.Font).Height + 3;
                e.ItemWidth = (int)e.Graphics.MeasureString(Text, gv_songlist.Font).Width + 3;
            }
            catch
            { }
        }

显示:
Win32.POINT point = new Win32.POINT();
                        Win32.GetCursorPos(ref point);
                        Win32.SetForegroundWindow(this.Handle);
                        Win32.TrackPopupMenuEx(this.cm.Handle, 0x40, point.x, point.y, this.Handle, new Win32.TPMPARAMS());

----------------------

public class Win32
    {

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern int SetForegroundWindow(IntPtr hwnd);

        [DllImport("user32.dll", EntryPoint = "TrackPopupMenuEx")]
        public static extern int TrackPopupMenuEx(
            IntPtr hMenu,
            int un,
            int n1,
            int n2,
            IntPtr hWnd,
            TPMPARAMS lpTPMParams
            );


        [StructLayout(LayoutKind.Sequential)]
        public struct TPMPARAMS
        {
            public int cbSize;
            public RECT rcExclude;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }


        [DllImport("user32.dll", EntryPoint = "GetCursorPos")]
        public static extern int GetCursorPos(ref POINT lpPoint);

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int x;
            public int y;
        }
    }

 

posted @   94cool  阅读(298)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2009年9月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示