重绘系统菜单

 using System;
using System.Drawing;

namespace HVS.Common
{
    public class ChangeSystemMenuColor
    {
        public static void SystemMenuColor(IntPtr Hwnd, Color color)
        {

            MENUINFO MENUINFO = new MENUINFO();
            MENUINFO.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(MENUINFO);
            MENUINFO.fMask = 2;

            Bitmap bmp = new Bitmap(200, 200);

            Brush b;
            b = new SolidBrush(color);

            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(b, new Rectangle(0, 0, 200, 200));

            if (bmp == null) MENUINFO.hbrBack = 0; else MENUINFO.hbrBack = Win32.CreatePatternBrush((bmp.GetHbitmap()).ToInt32());
            try
            {
                Win32.SetMenuInfo(Win32.GetSystemMenu(Hwnd.ToInt32(), 0), ref MENUINFO);

            }
            catch
            {


            }
        }

        public static void SystemMenuColor(IntPtr Hwnd, Image image)
        {
            MENUINFO MENUINFO = new MENUINFO();
            MENUINFO.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(MENUINFO);
            MENUINFO.fMask = 2;

            Bitmap bmp = new Bitmap(200, 200);

            Brush b;
            b = new System.Drawing.TextureBrush(image);

            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(b, new Rectangle(0, 0, 200, 200));

            if (bmp == null) MENUINFO.hbrBack = 0; else MENUINFO.hbrBack = Win32.CreatePatternBrush((bmp.GetHbitmap()).ToInt32());
            try
            {
                Win32.SetMenuInfo(Win32.GetSystemMenu(Hwnd.ToInt32(), 0), ref MENUINFO);

            }
            catch
            {


            }
        }

        public static void SystemMenuColor(IntPtr Hwnd, Color color1, Color color2, int direct)
        {
            MENUINFO MENUINFO = new MENUINFO();
            MENUINFO.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(MENUINFO);
            MENUINFO.fMask = 2;

            Bitmap bmp = new Bitmap(200, 200);

            Brush b;
            Point p1, p2;
            if (direct == 0)
            {
                p1 = new Point(0, 0);
                p2 = new Point(200, 0);
            }
            else if (direct == 1)
            {
                p1 = new Point(0, 0);
                p2 = new Point(0, 200);
            }
            else if (direct == 2)
            {
                p2 = new Point(0, 0);
                p1 = new Point(200, 200);
            }
            else
            {
                p2 = new Point(200, 0);
                p1 = new Point(0, 200);
            }
            b = new System.Drawing.Drawing2D.LinearGradientBrush(p1, p2, color1, color2);

            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(b, new Rectangle(0, 0, 200, 200));

            if (bmp == null)
                MENUINFO.hbrBack = 0;
            else
                MENUINFO.hbrBack = Win32.CreatePatternBrush((bmp.GetHbitmap()).ToInt32());
            try
            {
                Win32.SetMenuInfo(Win32.GetSystemMenu(Hwnd.ToInt32(), 0), ref MENUINFO);
            }
            catch
            {


            }
        }

        public static void UnSystemMenuColor(IntPtr Hwnd)
        {
            MENUINFO MENUINFO = new MENUINFO();
            MENUINFO.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(MENUINFO);
            MENUINFO.fMask = 2;
            MENUINFO.hbrBack = 0;
            try
            {
                Win32.SetMenuInfo(Win32.GetSystemMenu(Hwnd.ToInt32(), 0), ref MENUINFO);

            }
            catch
            {


            }
        }

 

  [DllImport("user32")]
        public static extern IntPtr GetSystemMenu(int hwnd, int bRevert);

        [DllImport("user32")]
        public static extern IntPtr GetMenu(int hwnd);

        [DllImport("user32")]
        public static extern int SetMenuInfo(IntPtr hMenu, ref MENUINFO mi);

        [DllImport("gdi32")]
        public static extern int CreatePatternBrush(int hBitmap);

    }

 public struct MENUINFO
    {
        public int cbSize;
        public uint fMask;
        public int dwStyle;
        public int cyMax;
        public int hbrBack;
        public int dwContextHelpID;
        public int dwMenuData;
    }


}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值