예제 #1
0
 public static extern int SetMenuInfo(IntPtr hmenu, ref MENUINFO mi);
        private void UpdateMainMenuBrush()
        {
            // screen states where there is no menu yet
            if (Menu == null)
                return;

            if (WindowState == FormWindowState.Minimized)
                return;

            // alias colorized resources
            ColorizedResources cres = ColorizedResources.Instance;

            // dispose any existing brush and/or bitmaps
            if (_hMainMenuBrush != IntPtr.Zero)
                DisposeGDIObject(ref _hMainMenuBrush);
            if (_hMainMenuBitmap != IntPtr.Zero)
                DisposeGDIObject(ref _hMainMenuBitmap);
            if (_hMainMenuBrushBitmap != IntPtr.Zero)
                DisposeGDIObject(ref _hMainMenuBrushBitmap);
            if (_mainMenuBitmap != null)
            {
                Bitmap tmp = _mainMenuBitmap;
                _mainMenuBitmap = null;
                tmp.Dispose();
            }

            // create a brush which contains the menu background
            _mainMenuBitmap = new Bitmap(Width, -RelativeWindowBounds.Y);
            _hMainMenuBitmap = _mainMenuBitmap.GetHbitmap();
            using (Graphics g = Graphics.FromImage(_mainMenuBitmap))
            {
                Rectangle bounds = MenuBounds;
                Debug.WriteLine("MenuBounds: " + bounds);
                if (cres.CustomMainMenuPainting)
                {
                    // paint custom menu background
                    CustomPaintMenuBackground(g, bounds);
                }
                else
                {
                    using (Brush brush = new SolidBrush(SystemMainMenuColor))
                        g.FillRectangle(brush, bounds);
                }
            }

            _hMainMenuBrushBitmap = _mainMenuBitmap.GetHbitmap();
            _hMainMenuBrush = Gdi32.CreatePatternBrush(_hMainMenuBrushBitmap);

            // set the brush
            MENUINFO mi = new MENUINFO();
            mi.cbSize = Marshal.SizeOf(typeof(MENUINFO));
            mi.fMask = MIM.BACKGROUND;
            mi.hbrBack = _hMainMenuBrush;
            User32.SetMenuInfo(Menu.Handle, ref mi);
            User32.DrawMenuBar(Handle);
        }