Laden...

[Gelöst] Probleme mit ControlPaint.DrawBorder zwischen Form und eigenem Control

Erstellt von braesident vor 6 Jahren Letzter Beitrag vor 6 Jahren 1.139 Views
B
braesident Themenstarter:in
15 Beiträge seit 2017
vor 6 Jahren
[Gelöst] Probleme mit ControlPaint.DrawBorder zwischen Form und eigenem Control

Ich habe ein Control erstellt in welchem mit Rectangles rumgemalt wird. Das Steuerelement funktioniert soweit auch und wird bei jedem FormBorderStyle richtig angezeigt.

ABER, wenn ich um mein Form ein Rahmen haben möchte, dann mach ich das mit DrawBorder in der Form1_Paint. Und genau dann wird um mein Control der selbe Rahmen gezeichnet - was echt unschön ist.

Hat jemand eine Idee wie ich das verhindern kann ?


using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Diagnostics;

namespace ExperimentalControl
{
    public partial class MeinControl : UserControl
    {
        FileInfo f;
        Rectangle contentRectangle = Rectangle.Empty;
        Point[] pts2 = new Point[4];
        Rectangle controlBounds = Rectangle.Empty;

        private string activeText = "ON";
        private string inActiveText = "OFF";

        public MeinControl()
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor, true);
            //InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {

            controlBounds = e.ClipRectangle;
            e.Graphics.ResetClip();

            this.MinimumSize = new Size(93, 30);
            this.MaximumSize = new Size(135, 51);
            DrawIOSStyle(e);

            base.OnPaint(e);
        }
        
        private void DrawIOSStyle(PaintEventArgs e)
        {
            this.BackColor = Color.Transparent;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            Rectangle rectControl = new Rectangle(0, 0, this.Width, this.Height);
            contentRectangle = rectControl;
            if (!isMouseMoved)
            {
                if (this.ToggleState == ToggleButtonState.ON)
                    ipadx = this.contentRectangle.Right - (this.contentRectangle.Height - 3);
                else
                    ipadx = 2;
            }
            //Rectangle rectToggleButton = new Rectangle(ipadx, r.Y, r.Height - 5, r.Height);
            Rectangle rectToggleButton = new Rectangle(ipadx, rectControl.Y, rectControl.Height, rectControl.Height);
            Rectangle rectActiveMirroring = new Rectangle(this.Width / 6 - 10, this.Height / 2, (this.Width / 6 - 10) + (rectToggleButton.X + rectToggleButton.Width / 2), this.Height / 2);

            System.Drawing.Drawing2D.GraphicsPath graphicsPathBorder = new System.Drawing.Drawing2D.GraphicsPath();
            int d = this.Height;
            graphicsPathBorder.AddArc(rectControl.X, rectControl.Y, d, d, 180, 90);
            graphicsPathBorder.AddArc(rectControl.X + rectControl.Width - d, rectControl.Y, d, d, 270, 90);
            graphicsPathBorder.AddArc(rectControl.X + rectControl.Width - d, rectControl.Y + rectControl.Height - d, d, d, 0, 90);
            graphicsPathBorder.AddArc(rectControl.X, rectControl.Y + rectControl.Height - d, d, d, 90, 90);
            this.Region = new Region(graphicsPathBorder);
            #region inner Rounded Rectangle

            System.Drawing.Drawing2D.GraphicsPath graphicsPathActiveMirror = new System.Drawing.Drawing2D.GraphicsPath();
            d = this.Height / 2;
            graphicsPathActiveMirror.AddArc(rectActiveMirroring.X, rectActiveMirroring.Y, d, d, 180, 90);
            graphicsPathActiveMirror.AddArc(rectActiveMirroring.X + rectActiveMirroring.Width - d, rectActiveMirroring.Y, d, d, 270, 90);
            graphicsPathActiveMirror.AddArc(rectActiveMirroring.X + rectActiveMirroring.Width - d, rectActiveMirroring.Y + rectActiveMirroring.Height - d, d, d, 0, 90);
            graphicsPathActiveMirror.AddArc(rectActiveMirroring.X, rectActiveMirroring.Y + rectActiveMirroring.Height - d, d, d, 90, 90);

            Rectangle rectInactiveMirroring = new Rectangle((rectToggleButton.X + rectToggleButton.Width / 2), this.Height / 2, (((this.Width / 2) + (this.Width / 4)) - (rectToggleButton.X + rectToggleButton.Width / 2)) + this.Height / 2, this.Height / 2); //4 * (this.Width / 6) + 20
            System.Drawing.Drawing2D.GraphicsPath graphicsPathInactiveMirror = new System.Drawing.Drawing2D.GraphicsPath();
            graphicsPathInactiveMirror.AddArc(rectInactiveMirroring.X, rectInactiveMirroring.Y, d, d, 180, 90);
            graphicsPathInactiveMirror.AddArc(rectInactiveMirroring.X + rectInactiveMirroring.Width - d, rectInactiveMirroring.Y, d, d, 270, 90);
            graphicsPathInactiveMirror.AddArc(rectInactiveMirroring.X + rectInactiveMirroring.Width - d, rectInactiveMirroring.Y + rectInactiveMirroring.Height - d, d, d, 0, 90);
            graphicsPathInactiveMirror.AddArc(rectInactiveMirroring.X, rectInactiveMirroring.Y + rectInactiveMirroring.Height - d, d, d, 90, 90);
            #endregion

            if (ipadx < contentRectangle.Width / 2)
                iosSelected = false;
            else if (ipadx == contentRectangle.Right - (contentRectangle.Height - 3) || ipadx > contentRectangle.Width / 2)
                iosSelected = true;


            Rectangle ar1 = new Rectangle(rectControl.X, rectControl.Y, rectControl.X + rectToggleButton.Right, rectControl.Height);
            Rectangle ar2 = new Rectangle(rectToggleButton.X + rectToggleButton.Width / 2, rectControl.Y, (rectToggleButton.X + rectToggleButton.Width / 2) + rectControl.Right, rectControl.Height);

            LinearGradientBrush BrushActiveMirrorEffekt;
            LinearGradientBrush activeToggleBrush;

            LinearGradientBrush BrushInactiveMirrorEffekt;
            LinearGradientBrush inactiveToggleBrush;

            BrushActiveMirrorEffekt = new LinearGradientBrush(ar1, Color.Transparent, Color.Transparent, LinearGradientMode.Vertical);
            activeToggleBrush = new LinearGradientBrush(ar1, Color.Red, Color.Maroon, LinearGradientMode.Vertical);

            BrushInactiveMirrorEffekt = new LinearGradientBrush(ar2, Color.Transparent, Color.Transparent, LinearGradientMode.Vertical);
            inactiveToggleBrush = new LinearGradientBrush(ar2, Color.Gray, Color.DarkGray, LinearGradientMode.Vertical);

            e.Graphics.FillRectangle(activeToggleBrush, ar1);
            e.Graphics.FillPath(BrushActiveMirrorEffekt, graphicsPathActiveMirror);

            e.Graphics.FillRectangle(inactiveToggleBrush, ar2);
            e.Graphics.FillPath(BrushInactiveMirrorEffekt, graphicsPathInactiveMirror);

            SizeF onSizeF = e.Graphics.MeasureString(activeText, Font, 0, StringFormat.GenericTypographic);
            SizeF offSizeF = e.Graphics.MeasureString(inActiveText, Font, 0, StringFormat.GenericTypographic);


            if (iosSelected)
                e.Graphics.DrawString(activeText, Font, new SolidBrush(ForeColor), new PointF(((rectControl.Width - rectControl.Height) - onSizeF.Width) / 2, (rectControl.Height - Font.Height) / 2));
            else
                e.Graphics.DrawString(inActiveText, Font, new SolidBrush(ForeColor), new PointF((((rectControl.Width - rectControl.Height) - offSizeF.Width) / 2 + rectControl.Height), (rectControl.Height - Font.Height) / 2));



            #region Center Ellipse
            Color c = this.Parent != null ? this.Parent.BackColor : Color.White;
            e.Graphics.DrawEllipse(new Pen(Color.LightGray, 2f), rectToggleButton);
            LinearGradientBrush br2 = new LinearGradientBrush(rectToggleButton, Color.White, Color.Silver, LinearGradientMode.Vertical);
            e.Graphics.FillEllipse(br2, rectToggleButton);
            #endregion

            e.Graphics.DrawPath(new Pen(c, 2f), graphicsPathBorder);

            e.Graphics.ResetClip();
        }

        #region Event Handlers

        bool iosSelected = false;

        bool dblclick = false;



        public event ToggleButtonStateChanged ButtonStateChanged;

        protected void RaiseButtonStateChanged()
        {
            if (this.ButtonStateChanged != null)
                ButtonStateChanged(this, new ToggleButtonStateEventArgs(this.ToggleState));
        }


        public delegate void ToggleButtonStateChanged(object sender, ToggleButtonStateEventArgs e);

        public class ToggleButtonStateEventArgs : EventArgs
        {
            public ToggleButtonStateEventArgs(ToggleButtonState ButtonState)
            {
            }
        }
                

        
        bool isMouseDown = false;
        Point downpos = Point.Empty;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (!this.DesignMode)
            {
                isMouseDown = true;
                downpos = e.Location;
            }
            this.Invalidate();
        }
        
        bool isMouseMoved = false;
        Point sliderPoint = Point.Empty;
        int padx = 0; int ipadx = 2;

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.Button == MouseButtons.Left && !this.DesignMode)
            {
                sliderPoint = e.Location;
                isMouseMoved = true;
                ipadx = e.X;
                if (ipadx <= 2)
                {
                    ipadx = 2;
                    this.ToggleState = ToggleButtonState.OFF;
                }

                if (ipadx >= contentRectangle.Right - (contentRectangle.Height - 3))
                {
                    ipadx = contentRectangle.Right - (contentRectangle.Height - 3);
                    this.ToggleState = ToggleButtonState.ON;
                }

                Refresh();
            }
        }

        bool switchrec = false;
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (!this.DesignMode)
            {
                this.Invalidate();
                if (isMouseMoved)
                {
                    ipadx = e.Location.X;
                    if (ipadx < contentRectangle.Width / 2)
                    {
                        ipadx = 2;
                        this.ToggleState = ToggleButtonState.OFF;
                    }
                    else
                    {
                        ipadx = contentRectangle.Right - (contentRectangle.Height - 3);
                        this.ToggleState = ToggleButtonState.ON;
                    }

                    Invalidate();
                    Update();
                }

                isMouseMoved = false;
                isMouseDown = false;
            }
        }
        #endregion




        #region properties
        private ToggleButtonState toggleState = ToggleButtonState.OFF;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public ToggleButtonState ToggleState
        {
            get
            {
                return toggleState;
            }
            set
            {
                if (toggleState != value)
                {
                    RaiseButtonStateChanged();
                    toggleState = value;
                    Invalidate();
                    this.Refresh();
                }
            }
        }

        public enum ToggleButtonState
        {
            ON,
            OFF
        }
        #endregion
        
    }
}

B
braesident Themenstarter:in
15 Beiträge seit 2017
vor 6 Jahren

Ok, sorry hab des Rätsels Lösung,

In Form1_Paint hab ich DrawBorder mit e.ClipRectangle aufgerufen. Besser ist wohl für den 2 Parameter 'ClientRectangle')