完美系统菜单

作者在 2008-04-11 13:24:05 发布以下内容

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;

namespace HVS.Common
{
    public class Win32
    {
        public struct MENUINFO
        {
            public int cbSize;
            public int fMask;
            public int dwStyle;
            public int cyMax;
            public int hbrBack;
            public int dwContextHelpID;
            public int dwMenuData;
        }

        [DllImport("kernel32.dll")]
        public static extern bool SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
        //设置操作系统实际划分给进程使用的内存容量
        //返回值
        //Long,非零表示成功,零表示失败。会设置GetLastError
        //参数表
        //参数 类型及说明
        //hProcess Long,指定一个进程的句柄
        //lpMinimumWorkingSetSize Long,用于装载最小进程容量的一个变量
        //lpMaximumWorkingSetSize Long,用于装载最大进程容量的一个变量

        [DllImport("User32.dll")]
        public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        //与ShowWindow相似,只是这时的ShowWindow命令会投递到指定的窗口,然后进行异步处理。
        //这样一来,就可控制从属于另一个进程的窗口的可视情况。同时无须担心另一个进程挂起的时候,
        //自己的应用程序也会牵连其中
        [DllImport("User32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DragDetect(IntPtr hWnd, Point pt);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetFocus();

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SetFocus(IntPtr hWnd);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern uint SendMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hWnd, short cmdShow);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndAfter, int X, int Y, int Width, int Height, FlagsSetWindowPos flags);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowLong(IntPtr hWnd, int Index);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetWindowLong(IntPtr hWnd, int Index, int Value);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetWindowLong(IntPtr hWnd, int Index, Delegate gate);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int ShowScrollBar(IntPtr hWnd, int wBar, int bShow);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]

        [SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", MessageId = "0")]
        public static extern IntPtr WindowFromPoint(Point point);

        [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetCurrentThreadId();

        public delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);

        [DllImport("user32.dll")]
        public static extern int UnhookWindowsHookEx(IntPtr hhook);

        [DllImport("user32.dll")]
        public static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wParam, IntPtr lParam);

        [DllImport("gdi32.dll")]
        public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);

        [DllImport("user32.dll")]
        public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);

        [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);

        [DllImport("user32.dll")]
        public static extern int AppendMenu(IntPtr hMenu, int Flagsw, int IDNewItem, string lpNewItem);

        [DllImport("user32.dll")]
        public static extern int GetMenuItemCount(IntPtr hMenu);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern int RemoveMenu(IntPtr hMenu, int uPosition, int uFlags);

        [DllImport("user32.dll")]
        public static extern int ModifyMenu(IntPtr hMenu, int nPosition, int wFlags, int wIDNewItem, string lpString);

    }
}

 

using System.Drawing;
using System.Windows.Forms;

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

            Win32.MENUINFO MENUINFO = new Win32.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(frm.Handle.ToInt32(), 0), ref MENUINFO);

            }
            catch
            {


            }
        }

        public static void SystemMenuColor(Form frm, Image image)
        {
            Win32.MENUINFO MENUINFO = new Win32.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(frm.Handle.ToInt32(), 0), ref MENUINFO);

            }
            catch
            {


            }
        }

        public static void SystemMenuColor(Form frm, Color color1, Color color2, int direct)
        {
            Win32.MENUINFO MENUINFO = new Win32.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(frm.Handle.ToInt32(), 0), ref MENUINFO);
            }
            catch
            {


            }
        }

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

            }
            catch
            {


            }
        }

    }
}

系统菜单 | 阅读 2483 次
文章评论,共0条
游客请输入验证码
浏览24981次