如何根据表单上的视图大小缩放矩形和位置?

时间:2017-05-16 17:38:45

标签: c# winforms

我正在处理一个项目,在用户输入详细信息后,为他们创建了一个设计。原始版本只是在一英寸为72像素的基础上创建了设计。我的工作基础相同。但是,无论形式有多大或多小,我都希望扩展我的设计。

我设法完全像原版一样设计了设计,我也设法正确地缩放了大矩形(红色),但我不知道如何缩小较小的矩形(黑色),所以它们适合彼此之间的正确空间。

看起来应该是这样的: Design

但是当我尝试它时,我得到了这个: My design

正如你所看到的,我的小矩形不在正确的x或y位置,也没有像设计那样分开。

这是我的代码:

  private void ValidateFormBeforeDiagram(object sedner, OptionOneCheckedEventArgs e) {
    try {
        //Create variables
        int pagesUp = e.IsOptionOneChecked ? view.OptionOneAround : view.OptionTwoAround;
        int pagesAcross = e.IsOptionOneChecked ? view.OptionOneAcross : view.OptionTwoAcross;
        float pageWidth = e.IsOptionOneChecked ? view.PageSizeLength : view.PageSizeWidth;
        float pageLength = e.IsOptionOneChecked ? view.PageSizeWidth : view.PageSizeLength;


        ParametersToCreatePrintingDiagram parameters = new ParametersToCreatePrintingDiagram(view.SheetSizeAcross, view.SheetSizeAround,
            pagesUp, pagesAcross, pageWidth, pageLength);

        //Check if form is open, if so close it

        if (Application.OpenForms.OfType<PrintingDesignForm>().Any()) {
            Application.OpenForms.OfType<PrintingDesignForm>().First().Close();
        }

        //Create a new instance of the form
        PrintingDesignForm form = new PrintingDesignForm();
        float ratio = 0;

        //If rectangle is larger than the client viewable size then scale the rectangle so it will fit.
        if (parameters.RectangleHeight > form.ClientRectangle.Height || parameters.RectangleWidth > form.ClientRectangle.Height) {
            var ratioX = form.ClientRectangle.Height / parameters.RectangleHeight;
            var ratioY = form.ClientRectangle.Width / parameters.RectangleWidth;

            ratio = Math.Min(ratioX, ratioY);

            parameters.RectangleHeight = parameters.RectangleHeight * ratio - parameters.RectangleXPosition;
            parameters.RectangleWidth = parameters.RectangleWidth * ratio - parameters.RectangleYPosition;
        }

        //Draw the main rectangle
        form.Paint += (se, pe) => {
            var r = new Rectangle(parameters.RectangleXPosition, parameters.RectangleYPosition,
                (int)Math.Round(parameters.RectangleWidth), (int)Math.Round(parameters.RectangleHeight));

            var brush = new SolidBrush(Color.FromArgb(255, 255, 204));

            pe.Graphics.FillRectangle(brush, r);
            using (var pen = new Pen(brush.Color, 2))
                pe.Graphics.DrawRectangle(pen, r);
        };

        float leftXInches;
        float topYInches;
        float xInch = 0;
        float yInch = 0;
        List<Rectangle> liRect = new List<Rectangle>();


        //Calculate the smaller rectangle sizes and positions
        for (int i = 1; i < parameters.PagesUp; i++) {
            for (int j = 1; j < parameters.PagesAcross; j++) {
                if (e.IsOptionOneChecked) {
                    leftXInches = parameters.RectangleXPosition
                    + ((view.SheetSizeAcross - (view.PageSizeLength * view.OptionOneAcross) - (view.OptionOneAcross - 1) * view.Bleeds) / 2)
                    + ((j - 1) * parameters.PageWidth)
                    + ((j - 1) * view.Bleeds);

                    topYInches = parameters.RectangleYPosition
                    + ((view.SheetSizeAround - (view.PageSizeLength * view.OptionOneAround) - (view.OptionOneAround - 1) * view.Bleeds) / 2)
                    + ((i - 1) * parameters.PageHeight)
                    + ((i - 1) * view.Bleeds);

                }
                else {

                    leftXInches = parameters.RectangleXPosition
                 + ((view.SheetSizeAcross - (view.PageSizeWidth * view.OptionTwoAcross) - (view.OptionTwoAcross - 1) * view.Bleeds) / 2)
                 + ((j - 1) * parameters.PageWidth)
                 + ((j - 1) * view.Bleeds);

                    topYInches = parameters.RectangleYPosition
                 + ((view.SheetSizeAround - (view.PageSizeLength * view.OptionTwoAround) - (view.OptionTwoAround - 1) * view.Bleeds) / 2)
                 + ((i - 1) * parameters.PageHeight)
                 + ((i - 1) * view.Bleeds);
                }

                var width = (Math.Round(parameters.PageWidth) * 72);
                var height = (Math.Round(parameters.PageHeight) * 72);

                yInch = (topYInches * 72);

                xInch = (leftXInches * 72);

                liRect.Add(new Rectangle((int)Math.Round(xInch * ratio), (int)Math.Round(yInch * ratio), (int)Math.Round(width * ratio),
                    (int)Math.Round(height * ratio)));
            }
        }

        //Paint the smaller rectangles onto the form
        for (int i = 0; i < liRect.Count; i++) {
            //Have to use x as i will only be the last increment of i due to form.Paint being an event.
            int x = i;

            form.Paint += (se, pe) => {
                var r = liRect[x];

                var brush = new SolidBrush(Color.FromArgb(255, 0, 0));

                pe.Graphics.FillRectangle(brush, r);
                using (var pen = new Pen(brush.Color, 2))
                    pe.Graphics.DrawRectangle(pen, r);
            };
        }

        form.Show();
    }
    catch (Exception ex) {
        LogErrorToView(this, new ErrorEventArgs(ex.Message));
    }
}

那么如何调整较小的矩形以适应较大的矩形,并在它们之间留出正确的空间?

我已经在这个问题上坚持了一段时间,并希望得到任何帮助。

由于

0 个答案:

没有答案