카테고리 없음2009. 3. 20. 14:56

5

public void DrawIcon(Graphics g, Image icon, Rectangle bounds, bool selected, bool enabled, bool ischecked)
        {
            // make icon transparent
            Color transparentColor = Color.FromArgb(0, 128, 128);
            Bitmap tempIcon = (Bitmap)icon;
            tempIcon.MakeTransparent(transparentColor);

            int iconTop = bounds.Top + (itemHeight - BITMAP_SIZE) / 2;
            int iconLeft = bounds.Left + (STRIPE_WIDTH - BITMAP_SIZE) / 2;
            if (enabled)
            {
                if (selected)
                {
                    ControlPaint.DrawImageDisabled(g, icon, iconLeft + 1, iconTop, Color.Black);
                    g.DrawImage(icon, iconLeft, iconTop - 1);
                }
                else
                {
                    g.DrawImage(icon, iconLeft + 1, iconTop);
                }
            }
            else
            {
                ControlPaint.DrawImageDisabled(g, icon, iconLeft + 1, iconTop, SystemColors.HighlightText);
            }
        }

        public void DrawSeparator(Graphics g, Rectangle bounds)
        {
            int y = bounds.Y + bounds.Height / 2;
            g.DrawLine(new Pen(SystemColors.ControlDark), bounds.X + iconSize + 7, y, bounds.X + bounds.Width - 2, y);
        }

        public void DrawBackground(Graphics g, Rectangle bounds, DrawItemState state, bool toplevel, bool hasicon, bool enabled)
        {
            bool selected = (state & DrawItemState.Selected) > 0;

            if (selected || ((state & DrawItemState.HotLight) > 0))
            {
                if (toplevel && selected)
                {   // draw toplevel, selected menuitem
                    bounds.Inflate(-1, 0);
                    g.FillRectangle(new SolidBrush(stripeColor), bounds);
                    ControlPaint.DrawBorder3D(g, bounds.Left, bounds.Top, bounds.Width,
                        bounds.Height, Border3DStyle.Flat, Border3DSide.Top | Border3DSide.Left | Border3DSide.Right);
                }
                else
                {   // draw menuitem hotlighted
                    if (enabled)
                    {
                        g.FillRectangle(new SolidBrush(selectionColor), bounds);
                        g.DrawRectangle(new Pen(borderColor), bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
                    }
                    else
                    {
                        // if disabled draw menuitem unselected
                        g.FillRectangle(new SolidBrush(stripeColor), bounds);
                        bounds.X += STRIPE_WIDTH;
                        bounds.Width -= STRIPE_WIDTH;
                        g.FillRectangle(new SolidBrush(bgColor), bounds);
                    }
                }
            }
            else
            {
                if (!toplevel)
                {   // draw menuitem, unselected
                    g.FillRectangle(new SolidBrush(stripeColor), bounds);
                    bounds.X += STRIPE_WIDTH;
                    bounds.Width -= STRIPE_WIDTH;
                    g.FillRectangle(new SolidBrush(bgColor), bounds);
                }
                else
                {
                    // draw toplevel, unselected menuitem
                    g.FillRectangle(SystemBrushes.Control, bounds);
                }
            }
        }

        public void DrawMenuText(Graphics g, Rectangle bounds, string text, string shortcut, bool enabled, bool toplevel, DrawItemState state)
        {
            StringFormat stringformat = new StringFormat();
            stringformat.HotkeyPrefix = ((state & DrawItemState.NoAccelerator) > 0) ? HotkeyPrefix.Hide : HotkeyPrefix.Show;

            // if 3D background happens to be black, as it is the case when
            // using a high contrast color theme, then make sure text is white
            bool highContrast = false;
            bool whiteHighContrast = false;
            if (SystemColors.Control.ToArgb() == Color.FromArgb(255, 0, 0, 0).ToArgb()) highContrast = true;
            if (SystemColors.Control.ToArgb() == Color.FromArgb(255, 255, 255, 255).ToArgb()) whiteHighContrast = true;

            // if menu is a top level, extract the ampersand that indicates the shortcut character
            // so that the menu text is centered
            string textTopMenu = text;
            if (toplevel)
            {
                int index = text.IndexOf("&");
                if (index != -1)
                {
                    // remove it
                    text = text.Remove(index, 1);
                }
            }

           

Posted by penguindori
카테고리 없음2009. 3. 20. 14:55

4

  // overrides
        // ---------------------------------------------------------
        protected override void OnMeasureItem(MeasureItemEventArgs e)
        {
            base.OnMeasureItem(e);

            // measure shortcut text
            if (Shortcut != Shortcut.None)
            {
                string text = "";
                int key = (int)Shortcut;
                int ch = key & 0xFF;
                if (((int)Keys.Control & key) > 0)
                    text += "Ctrl+";
                if (((int)Keys.Shift & key) > 0)
                    text += "Shift+";
                if (((int)Keys.Alt & key) > 0)
                    text += "Alt+";

                if (ch >= (int)Shortcut.F1 && ch <= (int)Shortcut.F12)
                    text += "F" + (ch - (int)Shortcut.F1 + 1);
                else
                {
                    if (Shortcut == Shortcut.Del)
                    {
                        text += "Del";
                    }
                    else
                    {
                        text += (char)ch;
                    }
                }
                shortcuttext = text;
            }

            if (Text == "-")
            {
                e.ItemHeight = 8;
                e.ItemWidth = 4;
                return;
            }

            bool topLevel = Parent == Parent.GetMainMenu();
            string tempShortcutText = shortcuttext;
            if (topLevel)
            {
                tempShortcutText = "";
            }
            //int textwidth = (int)(e.Graphics.MeasureString(Text + tempShortcutText, SystemInformation.MenuFont).Width);
            int textwidth = (int)(e.Graphics.MeasureString(Text + tempShortcutText, new Font("Tahoma", 9)).Width);
            int extraHeight = 4;
            e.ItemHeight = SystemInformation.MenuHeight + extraHeight;
            if (topLevel)
                e.ItemWidth = textwidth - 5;
            else
                e.ItemWidth = Math.Max(160, textwidth + 50);

            // save menu item heihgt for later use
            itemHeight = e.ItemHeight;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (doColorUpdate)
            {
                DoUpdateMenuColors();
            }

            base.OnDrawItem(e);

            Graphics g = e.Graphics;
            Rectangle bounds = e.Bounds;
            bool selected = (e.State & DrawItemState.Selected) > 0;
            bool toplevel = (Parent == Parent.GetMainMenu());
            bool hasicon = Icon != null;
            bool enabled = Enabled;

            DrawBackground(g, bounds, e.State, toplevel, hasicon, enabled);
            if (hasicon)
                DrawIcon(g, Icon, bounds, selected, Enabled, Checked);
            else
                if (Checked)
                    DrawCheckmark(g, bounds, selected);

            if (Text == "-")
            {
                DrawSeparator(g, bounds);
            }
            else
            {
                DrawMenuText(g, bounds, Text, shortcuttext, Enabled, toplevel, e.State);
            }
        }

        public void DrawCheckmark(Graphics g, Rectangle bounds, bool selected)
        {
            int checkTop = bounds.Top + (itemHeight - BITMAP_SIZE) / 2;
            int checkLeft = bounds.Left + (STRIPE_WIDTH - BITMAP_SIZE) / 2;
            ControlPaint.DrawMenuGlyph(g, new Rectangle(checkLeft, checkTop, BITMAP_SIZE, BITMAP_SIZE), MenuGlyph.Checkmark);
            g.DrawRectangle(new Pen(borderColor), checkLeft - 1, checkTop - 1, BITMAP_SIZE + 1, BITMAP_SIZE + 1);
        }

Posted by penguindori