Beispiel #1
0
        //ensure child is visible in the container using parent coordinates
        //where child.Parent == parent and parent.Parent == container
        public static void EnsureVisibleChild(this Control container, Control innerCtrl, Control innerCtrlChild, AnchorStyles ancors, int margin = 20, bool bAlways = false)
        {
            //visible bounds in innerCtrl coordinates
            int left   = -innerCtrl.Left;
            int top    = -innerCtrl.Top;
            int right  = container.Width - innerCtrl.Left;
            int bottom = container.Height - innerCtrl.Top;

            if (bAlways || innerCtrlChild.Left < left || innerCtrlChild.Top < top || innerCtrlChild.Right > right || innerCtrlChild.Bottom > bottom)
            {
                if (ancors == AnchorStyles.None) //center child if out of view
                {
                    innerCtrlChild.Left = left + (right - left - innerCtrlChild.Width) / 2;
                    innerCtrlChild.Top  = top + (bottom - top - innerCtrlChild.Height) / 2;
                }
                if (ancors.HasFlag(AnchorStyles.Left))
                {
                    innerCtrlChild.Left = left + margin;
                }
                if (ancors.HasFlag(AnchorStyles.Right))
                {
                    innerCtrlChild.Left = right - innerCtrlChild.Width - margin;
                }
                if (ancors.HasFlag(AnchorStyles.Top))
                {
                    innerCtrlChild.Top = top + margin;
                }
                if (ancors.HasFlag(AnchorStyles.Bottom))
                {
                    innerCtrlChild.Top = bottom - innerCtrlChild.Height - margin;
                }
            }
        }
Beispiel #2
0
 void timer1_Tick(object sender, EventArgs e)
 {
     innerForm.SuspendLayout();
     if (resizeType.HasFlag(AnchorStyles.Right))
     {
         int newWidth = resizeStartBound.Width + Cursor.Position.X - resizeStartPosition.X;
         if (newWidth < 0)
         {
             newWidth = 0;
         }
         if (innerForm.Width != newWidth)
         {
             innerForm.Width = newWidth;
         }
     }
     if (resizeType.HasFlag(AnchorStyles.Bottom))
     {
         int newHeight = resizeStartBound.Height + Cursor.Position.Y - resizeStartPosition.Y;
         if (newHeight < 0)
         {
             newHeight = 0;
         }
         if (innerForm.Height != newHeight)
         {
             innerForm.Height = newHeight;
         }
     }
     if (resizeType.HasFlag(AnchorStyles.Left))
     {
         int newWidth = resizeStartBound.Width - Cursor.Position.X + resizeStartPosition.X;
         if (newWidth < 0)
         {
             newWidth = 0;
         }
         if (innerForm.Width != newWidth)
         {
             innerForm.Width    = newWidth;
             innerForm.Location = new Point(resizeStartBound.X - (newWidth - resizeStartBound.Width), innerForm.Location.Y);
         }
     }
     if (resizeType.HasFlag(AnchorStyles.Top))
     {
         int newHeight = resizeStartBound.Height - Cursor.Position.Y + resizeStartPosition.Y;
         if (newHeight < 0)
         {
             newHeight = 0;
         }
         if (innerForm.Height != newHeight)
         {
             innerForm.Height   = newHeight;
             innerForm.Location = new Point(innerForm.Location.X, resizeStartBound.Y - (newHeight - resizeStartBound.Height));
         }
     }
     innerForm.ResumeLayout();
 }
        private string ConvertAnchorStyles(AnchorStyles styles)
        {
            string result = "";

            if (styles.HasFlag(AnchorStyles.Left))
            {
                result += (result.Length > 0 ? "| " : "") + "ANCHOR_LEFT";
            }
            if (styles.HasFlag(AnchorStyles.Right))
            {
                result += (result.Length > 0 ? "| " : "") + "ANCHOR_RIGHT";
            }
            if (styles.HasFlag(AnchorStyles.Top))
            {
                result += (result.Length > 0 ? "| " : "") + "ANCHOR_TOP";
            }
            if (styles.HasFlag(AnchorStyles.Bottom))
            {
                result += (result.Length > 0 ? "| " : "") + "ANCHOR_BOTTOM";
            }
            return(result);
        }
Beispiel #4
0
        public void Show(Form owner, Control target, AnchorStyles anchor)
        {
            var l      = target.PointToScreen(Point.Empty);
            var screen = Screen.FromControl(owner).WorkingArea;
            var mh     = 0;

            if (anchor.HasFlag(AnchorStyles.Top))
            {
                mh = screen.Bottom - l.Y;
            }
            else if (anchor.HasFlag(AnchorStyles.Bottom))
            {
                mh = l.Y - screen.Top;
            }
            else
            {
                mh = screen.Height / 2;
            }

            this.MaximumSize = new Size(0, mh);

            this.SizeChanged += delegate
            {
                var p = target.PointToScreen(Point.Empty);

                if (anchor.HasFlag(AnchorStyles.Left))
                {
                }
                else if (anchor.HasFlag(AnchorStyles.Right))
                {
                    p.X += target.Width - this.Width;
                }
                else
                {
                    p.X += target.Width / 2 - this.Width / 2;
                }

                if (anchor.HasFlag(AnchorStyles.Top))
                {
                }
                else if (anchor.HasFlag(AnchorStyles.Bottom))
                {
                    p.Y += target.Height - this.Height;
                }
                else
                {
                    p.Y += target.Height / 2 - this.Height / 2;
                }

                this.Location = p;
            };

            AttachControls(target);

            this.Show(owner);
        }
Beispiel #5
0
        public static Image CreateTornEdge(Image sourceImage, int toothHeight, int horizontalToothRange, int verticalToothRange, AnchorStyles sides)
        {
            Image result = sourceImage.CreateEmptyBitmap();

            using (GraphicsPath path = new GraphicsPath())
            {
                Random random            = new Random();
                int    horizontalRegions = sourceImage.Width / horizontalToothRange;
                int    verticalRegions   = sourceImage.Height / verticalToothRange;

                Point previousEndingPoint = new Point(horizontalToothRange, random.Next(1, toothHeight));
                Point newEndingPoint;

                if (sides.HasFlag(AnchorStyles.Top))
                {
                    for (int i = 0; i < horizontalRegions; i++)
                    {
                        int x = previousEndingPoint.X + horizontalToothRange;
                        int y = random.Next(1, toothHeight);
                        newEndingPoint = new Point(x, y);
                        path.AddLine(previousEndingPoint, newEndingPoint);
                        previousEndingPoint = newEndingPoint;
                    }
                }
                else
                {
                    previousEndingPoint = new Point(0, 0);
                    newEndingPoint      = new Point(sourceImage.Width, 0);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                if (sides.HasFlag(AnchorStyles.Right))
                {
                    for (int i = 0; i < verticalRegions; i++)
                    {
                        int x = sourceImage.Width - random.Next(1, toothHeight);
                        int y = previousEndingPoint.Y + verticalToothRange;
                        newEndingPoint = new Point(x, y);
                        path.AddLine(previousEndingPoint, newEndingPoint);
                        previousEndingPoint = newEndingPoint;
                    }
                }
                else
                {
                    previousEndingPoint = new Point(sourceImage.Width, 0);
                    newEndingPoint      = new Point(sourceImage.Width, sourceImage.Height);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                if (sides.HasFlag(AnchorStyles.Bottom))
                {
                    for (int i = 0; i < horizontalRegions; i++)
                    {
                        int x = previousEndingPoint.X - horizontalToothRange;
                        int y = sourceImage.Height - random.Next(1, toothHeight);
                        newEndingPoint = new Point(x, y);
                        path.AddLine(previousEndingPoint, newEndingPoint);
                        previousEndingPoint = newEndingPoint;
                    }
                }
                else
                {
                    previousEndingPoint = new Point(sourceImage.Width, sourceImage.Height);
                    newEndingPoint      = new Point(0, sourceImage.Height);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                if (sides.HasFlag(AnchorStyles.Left))
                {
                    for (int i = 0; i < verticalRegions; i++)
                    {
                        int x = random.Next(1, toothHeight);
                        int y = previousEndingPoint.Y - verticalToothRange;
                        newEndingPoint = new Point(x, y);
                        path.AddLine(previousEndingPoint, newEndingPoint);
                        previousEndingPoint = newEndingPoint;
                    }
                }
                else
                {
                    previousEndingPoint = new Point(0, sourceImage.Height);
                    newEndingPoint      = new Point(0, 0);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                path.CloseFigure();

                using (Graphics graphics = Graphics.FromImage(result))
                {
                    graphics.SmoothingMode      = SmoothingMode.HighQuality;
                    graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;

                    // Draw the created figure with the original image by using a TextureBrush so we have anti-aliasing
                    using (Brush brush = new TextureBrush(sourceImage))
                    {
                        graphics.FillPath(brush, path);
                    }
                }
            }

            return(result);
        }
Beispiel #6
0
        public static Image CreateTornEdge(Image sourceImage, int toothHeight, int horizontalToothRange, int verticalToothRange, AnchorStyles sides)
        {
            Image result = sourceImage.CreateEmptyBitmap();

            using (GraphicsPath path = new GraphicsPath())
            {
                Random random = new Random();
                int horizontalRegions = sourceImage.Width / horizontalToothRange;
                int verticalRegions = sourceImage.Height / verticalToothRange;

                Point previousEndingPoint = new Point(horizontalToothRange, random.Next(1, toothHeight));
                Point newEndingPoint;

                if (sides.HasFlag(AnchorStyles.Top))
                {
                    for (int i = 0; i < horizontalRegions; i++)
                    {
                        int x = previousEndingPoint.X + horizontalToothRange;
                        int y = random.Next(1, toothHeight);
                        newEndingPoint = new Point(x, y);
                        path.AddLine(previousEndingPoint, newEndingPoint);
                        previousEndingPoint = newEndingPoint;
                    }
                }
                else
                {
                    previousEndingPoint = new Point(0, 0);
                    newEndingPoint = new Point(sourceImage.Width, 0);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                if (sides.HasFlag(AnchorStyles.Right))
                {
                    for (int i = 0; i < verticalRegions; i++)
                    {
                        int x = sourceImage.Width - random.Next(1, toothHeight);
                        int y = previousEndingPoint.Y + verticalToothRange;
                        newEndingPoint = new Point(x, y);
                        path.AddLine(previousEndingPoint, newEndingPoint);
                        previousEndingPoint = newEndingPoint;
                    }
                }
                else
                {
                    previousEndingPoint = new Point(sourceImage.Width, 0);
                    newEndingPoint = new Point(sourceImage.Width, sourceImage.Height);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                if (sides.HasFlag(AnchorStyles.Bottom))
                {
                    for (int i = 0; i < horizontalRegions; i++)
                    {
                        int x = previousEndingPoint.X - horizontalToothRange;
                        int y = sourceImage.Height - random.Next(1, toothHeight);
                        newEndingPoint = new Point(x, y);
                        path.AddLine(previousEndingPoint, newEndingPoint);
                        previousEndingPoint = newEndingPoint;
                    }
                }
                else
                {
                    previousEndingPoint = new Point(sourceImage.Width, sourceImage.Height);
                    newEndingPoint = new Point(0, sourceImage.Height);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                if (sides.HasFlag(AnchorStyles.Left))
                {
                    for (int i = 0; i < verticalRegions; i++)
                    {
                        int x = random.Next(1, toothHeight);
                        int y = previousEndingPoint.Y - verticalToothRange;
                        newEndingPoint = new Point(x, y);
                        path.AddLine(previousEndingPoint, newEndingPoint);
                        previousEndingPoint = newEndingPoint;
                    }
                }
                else
                {
                    previousEndingPoint = new Point(0, sourceImage.Height);
                    newEndingPoint = new Point(0, 0);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                path.CloseFigure();

                using (Graphics graphics = Graphics.FromImage(result))
                {
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    // Draw the created figure with the original image by using a TextureBrush so we have anti-aliasing
                    using (Brush brush = new TextureBrush(sourceImage))
                    {
                        graphics.FillPath(brush, path);
                    }
                }
            }

            return result;
        }
Beispiel #7
0
        public static Image TornEdges(Image img, int tornDepth, int tornRange, AnchorStyles sides)
        {
            if (sides == AnchorStyles.None)
            {
                return img;
            }

            using (GraphicsPath gp = new GraphicsPath())
            {
                Point previousPoint, currentPoint;
                int horizontalTornCount = img.Width / tornRange;
                int verticalTornCount = img.Height / tornRange;

                if (sides.HasFlag(AnchorStyles.Top))
                {
                    previousPoint = new Point(tornRange, MathHelpers.Random(0, tornDepth));

                    for (int i = 0; i < horizontalTornCount - 1; i++)
                    {
                        currentPoint = new Point(previousPoint.X + tornRange, MathHelpers.Random(0, tornDepth));
                        gp.AddLine(previousPoint, currentPoint);
                        previousPoint = currentPoint;
                    }
                }
                else
                {
                    previousPoint = new Point(0, 0);
                    currentPoint = new Point(img.Width - 1, 0);
                    gp.AddLine(previousPoint, currentPoint);
                    previousPoint = currentPoint;
                }

                if (sides.HasFlag(AnchorStyles.Right))
                {
                    for (int i = 0; i < verticalTornCount; i++)
                    {
                        currentPoint = new Point(img.Width - 1 - MathHelpers.Random(0, tornDepth), previousPoint.Y + tornRange);
                        gp.AddLine(previousPoint, currentPoint);
                        previousPoint = currentPoint;
                    }
                }
                else
                {
                    currentPoint = new Point(img.Width - 1, img.Height - 1);
                    gp.AddLine(previousPoint, currentPoint);
                    previousPoint = currentPoint;
                }

                if (sides.HasFlag(AnchorStyles.Bottom))
                {
                    for (int i = 0; i < horizontalTornCount; i++)
                    {
                        currentPoint = new Point(previousPoint.X - tornRange, img.Height - 1 - MathHelpers.Random(0, tornDepth));
                        gp.AddLine(previousPoint, currentPoint);
                        previousPoint = currentPoint;
                    }
                }
                else
                {
                    currentPoint = new Point(0, img.Height - 1);
                    gp.AddLine(previousPoint, currentPoint);
                    previousPoint = currentPoint;
                }

                if (sides.HasFlag(AnchorStyles.Left))
                {
                    for (int i = 0; i < verticalTornCount; i++)
                    {
                        currentPoint = new Point(MathHelpers.Random(0, tornDepth), previousPoint.Y - tornRange);
                        gp.AddLine(previousPoint, currentPoint);
                        previousPoint = currentPoint;
                    }
                }
                else
                {
                    currentPoint = new Point(0, 0);
                    gp.AddLine(previousPoint, currentPoint);
                    previousPoint = currentPoint;
                }

                gp.CloseFigure();

                Bitmap result = img.CreateEmptyBitmap();

                using (img)
                using (Graphics g = Graphics.FromImage(result))
                using (TextureBrush brush = new TextureBrush(img))
                {
                    g.SetHighQuality();
                    g.FillPath(brush, gp);

                    return result;
                }
            }
        }