Avatar billede mikkel_sommer Nybegynder
06. januar 2006 - 11:39 Der er 15 kommentarer og
1 løsning

X i stedet for V når checkboxen er checked

Er der nogen som ved om man kan gøre sådan at det bliver markeret med et X og ikke et V når en checkbox checkes.
Avatar billede bitsch Nybegynder
06. januar 2006 - 11:49 #1
Det kan du ikke umiddelbart.
Avatar billede Syska Mester
06. januar 2006 - 11:57 #2
Så kan du til at design din egen checkbox

// ouT
Avatar billede bitsch Nybegynder
06. januar 2006 - 12:02 #3
Nemlig ja
Avatar billede spif2001 Nybegynder
06. januar 2006 - 14:08 #4
Avatar billede bitsch Nybegynder
06. januar 2006 - 15:01 #5
Du kan også få lidt inspiration i følgende eksempel som jeg lige har strikket sammen.
Eksemplet anvendder renderer klasserne for at få oplysninger om de relevante CheckBox dele.

Eksemplet er ikke en komplet løsning. Der mangler at blive implementeret states (når musen bevæges over kontrollen). Og man vil næppe heller acceptere dette simple X som jeg har lagt ind i demoen. Men der er mange muligheder og jeg tror at det vil give dig et fint overblik over hvad der skal til.

namespace CheckBox
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.VisualStyles;

    class CheckBoxX : CheckBox
    {
        private CheckBoxState checkBoxState = CheckBoxState.UncheckedNormal;
        private VisualStyleRenderer visualStyleRenderer = null;

        private Rectangle checkMarkBounds = Rectangle.Empty;
        private Color textColor = Color.Empty;

        /// <summary>
        /// Default Constructor.
        /// </summary>
        public CheckBoxX() : base()
        {

            this.GetPartDetails();
        }

        /// <summary>
        /// Calculate the text bounds, exluding the check box.
        /// </summary>
        internal Rectangle TextRectangle
        {
            get
            {
                Rectangle rectangle = new Rectangle();

                using (Graphics graphics = this.CreateGraphics())
                {
                    rectangle.X = ClientRectangle.X + CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal).Width;
                    rectangle.Y = ClientRectangle.Y;
                    rectangle.Width = ClientRectangle.Width - CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal).Width;
                    rectangle.Height = ClientRectangle.Height;
                }

                return rectangle;
            }
        }

        /// <summary>
        /// Override in order to draw checkmark as "X"
        /// </summary>
        /// <param name="paintEventArgs">PaintEventArgs.</param>
        protected override void OnPaint(PaintEventArgs paintEventArgs)
        {
            // Note! Remember to enabme VisualStyles (Application.EnableVisualStyles();)
            // This demo will only work then VisualStyles is enabled and Appearance is normal.
            if (Application.RenderWithVisualStyles && this.Appearance == Appearance.Normal) // Dont do anything in case that the CheckBox is drawn as a button.
            {
                CheckBoxRenderer.DrawParentBackground(paintEventArgs.Graphics, paintEventArgs.ClipRectangle, this);

                // In case that you want to draw all using the Renderer insert these lines.
                //CheckBoxRenderer.DrawCheckBox(paintEventArgs.Graphics,
                //    this.ClientRectangle.Location,
                //    this.TextRectangle,
                //    this.Text,
                //    this.Font,
                //    TextFormatFlags.HorizontalCenter,
                //    this.Checked,
                //    this.checkBoxState);

                this.DrawCheckMark(paintEventArgs.Graphics, this.Checked, this.checkBoxState);

                this.DrawText(paintEventArgs.Graphics);
            }
            else
            {
                base.OnPaint(paintEventArgs);
            }
        }

        /// <summary>
        /// Draws the text.
        /// </summary>
        /// <param name="paintEventArgs"></param>
        private void DrawText(Graphics graphics)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }

            TextRenderer.DrawText(graphics, this.Text, this.Font,
                this.TextRectangle,
                this.textColor,
                TextFormatFlags.Left);
        }

        /// <summary>
        /// Draws CheckMark.
        /// Note that this is only a short demo! CheckBoxStates needs to be implemented.
        /// </summary>
        /// <param name="paintEventArgs"></param>
        private void DrawCheckMark(Graphics graphics, bool checkBoxChecked, CheckBoxState checkBoxState)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }

            // TODO: This demo will not implement the different CheckBoxStates. This is only a demo!
            float penSize = 1.0f;

            if (this.Focused) // Note that this is only a silly example!
            {
                penSize = 2.0F;
            }

            using (Pen pen = new Pen(Color.Red, penSize))
            {
                graphics.DrawRectangle(pen, this.checkMarkBounds);

                if (checkBoxChecked) // Draw "X"
                {
                    graphics.DrawLine(pen, this.checkMarkBounds.Location, new Point(this.checkMarkBounds.Right, this.checkMarkBounds.Bottom));
                    graphics.DrawLine(pen, new Point(this.checkMarkBounds.Left, this.checkMarkBounds.Bottom), new Point(this.checkMarkBounds.Right, this.checkMarkBounds.Top));
                }
            }

        }

        /// <summary>
        /// Draw the check box in the checked or unchecked state, alternately.
        /// </summary>
        /// <param name="e">MouseEventArgs</param>
        protected override void OnMouseDown(MouseEventArgs mouseEventArgs)
        {
            base.OnMouseDown(mouseEventArgs);

            this.checkBoxState = this.Checked ? CheckBoxState.UncheckedNormal : CheckBoxState.CheckedPressed;
            this.Invalidate();
        }

        /// <summary>
        /// Draw the check box in the hot state.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseHover(EventArgs eventArgs)
        {
            base.OnMouseHover(eventArgs);
           
            this.checkBoxState = this.Checked ? CheckBoxState.CheckedHot : CheckBoxState.UncheckedHot;
            this.Invalidate();
        }

        /// <summary>
        /// Draw the check box in the hot state.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs mouseEventArgs)
        {
            base.OnMouseUp(mouseEventArgs);
           
            this.OnMouseHover(mouseEventArgs);
        }

        /// <summary>
        /// Draw the check box in the unpressed state.
        /// </summary>
        /// <param name="eventArgs"></param>
        protected override void OnMouseLeave(EventArgs eventArgs)
        {
            base.OnMouseLeave(eventArgs);

            this.checkBoxState = this.Checked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal;
            this.Invalidate();
        }

        /// <summary>
        /// Get the sizes and offsets for the window parts as specified by the visual style.
        /// </summary>
        private void GetPartDetails()
        {
            // Do nothing further if visual styles are not enabled.
            if (!Application.RenderWithVisualStyles)
            {
                return;
            }

            using (Graphics graphics = this.CreateGraphics())
            {
                if (this.SetRenderer(VisualStyleElement.Button.CheckBox.UncheckedNormal))
                {
                    #region CheckMark

                    Size size = this.visualStyleRenderer.GetPartSize(graphics, ThemeSizeType.True);
                    Point point = this.visualStyleRenderer.GetPoint(PointProperty.Offset);

                    this.checkMarkBounds = new Rectangle(point, size);

                    #endregion // CheckMark

                    #region Text

                    this.textColor = this.visualStyleRenderer.GetColor(ColorProperty.TextColor);

                    #endregion // Text
                }
            }
        }

        /// <summary>
        /// Set the VisualStyleRenderer to a new element.
        /// </summary>
        /// <param name="element">Visual style element to be set for the renderer.</param>
        /// <returns>Returns true in case that a valid renderer has been returned.</returns>
        private bool SetRenderer(VisualStyleElement visualStyleElement)
        {
            bool value = false;
           
            if (VisualStyleRenderer.IsElementDefined(visualStyleElement))
            {
                if (visualStyleRenderer == null)
                {
                    this.visualStyleRenderer = new VisualStyleRenderer(visualStyleElement);
                }
                else
                {
                    this.visualStyleRenderer.SetParameters(visualStyleElement);
                }

                value = true;
            }

            return value;
        }

    }
}
Avatar billede bitsch Nybegynder
06. januar 2006 - 19:05 #6
Og så kan man naturligvis snyde sig færdig ved blot at tegne et "X" på baggrunden af den alm. CheckBox (Hvis man kan acceptere dette). For at gøre dette skal du fjerne Checked state, og derefter blot tegne et "X" i boxen.

      /// <summary>
        /// Override in order to draw checkmark as "X"
        /// </summary>
        /// <param name="paintEventArgs">PaintEventArgs.</param>
        protected override void OnPaint(PaintEventArgs paintEventArgs)
        {
            // Note! Remember to enabme VisualStyles (Application.EnableVisualStyles();)
            // This demo will only work then VisualStyles is enabled and Appearance is normal.
            if (Application.RenderWithVisualStyles && this.Appearance == Appearance.Normal) // Dont do anything in case that the CheckBox is drawn as a button.
            {
                CheckBoxRenderer.DrawParentBackground(paintEventArgs.Graphics, paintEventArgs.ClipRectangle, this);

                CheckBoxState state = RemoveCheckedState(this.checkBoxState);

                // In case that you want to draw all using the Renderer insert these lines.
                CheckBoxRenderer.DrawCheckBox(paintEventArgs.Graphics,
                    this.ClientRectangle.Location,
                    this.TextRectangle,
                    this.Text,
                    this.Font,
                    TextFormatFlags.Left,
                    this.Checked,
                    state);

                if (this.Checked)
                {
                    this.DrawCheckMark(paintEventArgs.Graphics);
                }
            }
            else
            {
                base.OnPaint(paintEventArgs);
            }
        }


        /// <summary>
        /// </summary>
        /// <param name="paintEventArgs"></param>
        private void DrawCheckMark(Graphics graphics)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }

            float penSize = 2.0f;

            Rectangle bounds = this.checkMarkBounds;
            bounds.Inflate(-3, -3);

            using (Pen pen = new Pen(this.textColor, penSize))
            {
                graphics.DrawLine(pen, bounds.Location, new Point(bounds.Right -1, bounds.Bottom));
                graphics.DrawLine(pen, new Point(bounds.Left, bounds.Bottom), new Point(bounds.Right -1, bounds.Top));
            }
        }

        private static CheckBoxState RemoveCheckedState(CheckBoxState checkBoxState)
        {
            // Cheat! Remove the checkmark by changing the checked state!
            switch (checkBoxState)
            {
                case CheckBoxState.CheckedDisabled:
                    checkBoxState = CheckBoxState.UncheckedDisabled;
                    break;

                case CheckBoxState.CheckedHot:
                    checkBoxState = CheckBoxState.UncheckedHot;
                    break;

                case CheckBoxState.CheckedNormal:
                    checkBoxState = CheckBoxState.UncheckedNormal;
                    break;

                case CheckBoxState.CheckedPressed:
                    checkBoxState = CheckBoxState.UncheckedPressed;
                    break;

                case CheckBoxState.MixedDisabled:
                    checkBoxState = CheckBoxState.UncheckedDisabled;
                    break;

                case CheckBoxState.MixedHot:
                    checkBoxState = CheckBoxState.UncheckedHot;
                    break;

                case CheckBoxState.MixedNormal:
                    checkBoxState = CheckBoxState.UncheckedNormal;
                    break;

                case CheckBoxState.MixedPressed:
                    checkBoxState = CheckBoxState.UncheckedPressed;
                    break;

                default:
                    // Do Nothing
                    break;
            }
            return checkBoxState;
        }
Avatar billede bitsch Nybegynder
06. januar 2006 - 19:07 #7
Det er naturligvis et væld af alternativer, men i ovenstående løsning tegnes mindst muligt.
Avatar billede Syska Mester
06. januar 2006 - 19:31 #8
lol, hvem end der har lavet det eksemple overfor er der nogle sjove engelske stavefejl :-) *heheh*

// ouT
Avatar billede bitsch Nybegynder
06. januar 2006 - 19:41 #9
Det er nu mig selv ;-) men det gik lidt stærkt
Avatar billede Syska Mester
06. januar 2006 - 19:42 #10
okay :-) Men bare det er forståeligt, det er jo det væsentlige

// ouT
Avatar billede bitsch Nybegynder
06. januar 2006 - 19:43 #11
Det håber jeg at det er alligevel, det var jo tanken.
Avatar billede Syska Mester
06. januar 2006 - 20:59 #12
Er det, man kan også tydeligt se at det var "hastigheds" fejl, når man læser det...

Men nok om det, og tilbage til hans spm, som virker lidt som om det måske er løst

Men jeg vile nu nok bare personligt holde mig til "V'et"....

// ouT
Avatar billede bitsch Nybegynder
06. januar 2006 - 21:01 #13
Ja helt klart. Jeg er fuldstændigt enig.
Avatar billede bitsch Nybegynder
06. januar 2006 - 23:57 #14
Men alligevel... her er en rettet udgave som også retter text bounds så det svarer til en alm. CheckBox. Jeg håber ikke at jeg har overset stavefejl denne gang ;-)

namespace CheckBoxX
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.VisualStyles;

    class CheckBoxX : CheckBox
    {
        private CheckBoxState checkBoxState = CheckBoxState.UncheckedNormal;
        private VisualStyleRenderer visualStyleRenderer = null;

        private Rectangle checkMarkBounds = Rectangle.Empty;
        private Rectangle textBounds = Rectangle.Empty;
        private Color glyphTextColor = Color.Empty;

        /// <summary>
        /// Default Constructor.
        /// </summary>
        public CheckBoxX() : base()
        {
            this.GetPartDetails();
        }

        /// <summary>
        /// Override in order to draw checkmark as "X"
        /// </summary>
        /// <param name="paintEventArgs">PaintEventArgs.</param>
        protected override void OnPaint(PaintEventArgs paintEventArgs)
        {
            // Note! Remember to enabme VisualStyles (Application.EnableVisualStyles();)
            // This demo will only work then VisualStyles is enabled and Appearance is normal.
            if (Application.RenderWithVisualStyles && this.Appearance == Appearance.Normal) // Dont do anything in case that the CheckBox is drawn as a button.
            {
                CheckBoxRenderer.DrawParentBackground(paintEventArgs.Graphics, paintEventArgs.ClipRectangle, this);

                CheckBoxState state = RemoveCheckedState(this.checkBoxState);

                // In case that you want to draw all using the Renderer insert these lines.
                CheckBoxRenderer.DrawCheckBox(paintEventArgs.Graphics,
                    this.ClientRectangle.Location,
                    this.textBounds,
                    this.Text,
                    this.Font,
                    TextFormatFlags.Left,
                    this.ShowFocusCues & this.Focused,
                    state);

                if (this.Checked)
                {
                    this.DrawCheckMark(paintEventArgs.Graphics);
                }
            }
            else
            {
                base.OnPaint(paintEventArgs);
            }
        }

        /// <summary>
        /// </summary>
        /// <param name="paintEventArgs"></param>
        private void DrawCheckMark(Graphics graphics)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }

            float penSize = 2.0f;

            Rectangle bounds = this.checkMarkBounds;

            using (Pen pen = new Pen(this.glyphTextColor, penSize))
            {
                graphics.DrawLine(pen, bounds.Location, new Point(bounds.Right - 1, bounds.Bottom));
                graphics.DrawLine(pen, new Point(bounds.Left, bounds.Bottom), new Point(bounds.Right - 1, bounds.Top));
            }
        }

        /// <summary>
        /// Replaces a possible Checked state into a unchecked state.
        /// </summary>
        /// <param name="checkBoxState">Current CheckBoxState.</param>
        /// <returns>Modified CheckBoxState.</returns>
        private static CheckBoxState RemoveCheckedState(CheckBoxState checkBoxState)
        {
            switch (checkBoxState)
            {
                case CheckBoxState.CheckedDisabled:
                    checkBoxState = CheckBoxState.UncheckedDisabled;
                    break;

                case CheckBoxState.CheckedHot:
                    checkBoxState = CheckBoxState.UncheckedHot;
                    break;

                case CheckBoxState.CheckedNormal:
                    checkBoxState = CheckBoxState.UncheckedNormal;
                    break;

                case CheckBoxState.CheckedPressed:
                    checkBoxState = CheckBoxState.UncheckedPressed;
                    break;

                case CheckBoxState.MixedDisabled:
                    checkBoxState = CheckBoxState.UncheckedDisabled;
                    break;

                case CheckBoxState.MixedHot:
                    checkBoxState = CheckBoxState.UncheckedHot;
                    break;

                case CheckBoxState.MixedNormal:
                    checkBoxState = CheckBoxState.UncheckedNormal;
                    break;

                case CheckBoxState.MixedPressed:
                    checkBoxState = CheckBoxState.UncheckedPressed;
                    break;

                default:
                    break;
            }
       
            return checkBoxState;
        }

        /// <summary>
        /// Draw the check box in the checked or unchecked state, alternately.
        /// </summary>
        /// <param name="e">MouseEventArgs</param>
        protected override void OnMouseDown(MouseEventArgs mouseEventArgs)
        {
            base.OnMouseDown(mouseEventArgs);

            this.checkBoxState = this.Checked ? CheckBoxState.UncheckedNormal : CheckBoxState.CheckedPressed;
            this.Invalidate();
        }

        /// <summary>
        /// Draw the check box in the hot state.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseHover(EventArgs eventArgs)
        {
            base.OnMouseHover(eventArgs);

            this.checkBoxState = this.Checked ? CheckBoxState.CheckedHot : CheckBoxState.UncheckedHot;
            this.Invalidate();
        }

        /// <summary>
        /// Draw the check box in the hot state.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs mouseEventArgs)
        {
            base.OnMouseUp(mouseEventArgs);

            this.OnMouseHover(mouseEventArgs);
        }

        /// <summary>
        /// Draw the check box in the unpressed state.
        /// </summary>
        /// <param name="eventArgs"></param>
        protected override void OnMouseLeave(EventArgs eventArgs)
        {
            base.OnMouseLeave(eventArgs);

            this.checkBoxState = this.Checked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal;
            this.Invalidate();
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);

            this.GetPartDetails();
        }

        /// <summary>
        /// Get the sizes and offsets for the window parts as specified by the visual style.
        /// </summary>
        private void GetPartDetails()
        {
            // Do nothing further if visual styles are not enabled.
            if (!Application.RenderWithVisualStyles)
            {
                return;
            }

            using (Graphics graphics = this.CreateGraphics())
            {
                if (this.SetRenderer(VisualStyleElement.Button.CheckBox.UncheckedNormal))
                {
                    #region CheckMark

                    Size glyphSize = CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal);
                    Point point = this.visualStyleRenderer.GetPoint(PointProperty.Offset);

                    this.checkMarkBounds = new Rectangle(point, glyphSize);
                    this.checkMarkBounds.Inflate(-3, -3);

                    #endregion // CheckMark

                    #region Text

                    this.glyphTextColor = this.visualStyleRenderer.GetColor(ColorProperty.GlyphTextColor);

                    if (!string.IsNullOrEmpty(this.Text))
                    {
                        TextMetrics textMetrics = this.visualStyleRenderer.GetTextMetrics(graphics);

                        Point textLocation = new Point(glyphSize.Width + textMetrics.InternalLeading, 0);

                        Rectangle textExtent = this.visualStyleRenderer.GetTextExtent(graphics, this.Text, TextFormatFlags.Left);

                        Size textSize = new Size(textExtent.Width - glyphSize.Width, textMetrics.Ascent);

                        this.textBounds = new Rectangle(textLocation, textSize);

                        this.textBounds.Location = textLocation;
                    }

                    #endregion // Text
                }
            }
        }

        /// <summary>
        /// Set the VisualStyleRenderer to a new element.
        /// </summary>
        /// <param name="element">Visual style element to be set for the renderer.</param>
        /// <returns>Returns true in case that a valid renderer has been returned.</returns>
        private bool SetRenderer(VisualStyleElement visualStyleElement)
        {
            bool value = false;

            if (VisualStyleRenderer.IsElementDefined(visualStyleElement))
            {
                if (visualStyleRenderer == null)
                {
                    this.visualStyleRenderer = new VisualStyleRenderer(visualStyleElement);
                }
                else
                {
                    this.visualStyleRenderer.SetParameters(visualStyleElement);
                }

                value = true;
            }

            return value;
        }
    }
}
Avatar billede mikkel_sommer Nybegynder
02. februar 2006 - 17:51 #15
Tror jeg dropper projektet men tak for jeres store indsats, smid et svar
Avatar billede bitsch Nybegynder
04. februar 2006 - 09:00 #16
svar
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester