protected override void OnPaint(PaintEventArgs pevent)
        {
            using (Brush brush = new SolidBrush(this.BackColor))
            {
                if ((this.pageInfo == null) || (this.pageInfo.Length == 0))
                {
                    pevent.Graphics.FillRectangle(brush, base.ClientRectangle);
                    if ((this.pageInfo != null) || this.exceptionPrinting)
                    {
                        StringFormat format = new StringFormat {
                            Alignment     = ControlPaint.TranslateAlignment(ContentAlignment.MiddleCenter),
                            LineAlignment = ControlPaint.TranslateLineAlignment(ContentAlignment.MiddleCenter)
                        };
                        SolidBrush brush2 = new SolidBrush(this.ForeColor);
                        try
                        {
                            if (this.exceptionPrinting)
                            {
                                pevent.Graphics.DrawString(System.Windows.Forms.SR.GetString("PrintPreviewExceptionPrinting"), this.Font, brush2, base.ClientRectangle, format);
                            }
                            else
                            {
                                pevent.Graphics.DrawString(System.Windows.Forms.SR.GetString("PrintPreviewNoPages"), this.Font, brush2, base.ClientRectangle, format);
                            }
                            goto Label_04E9;
                        }
                        finally
                        {
                            brush2.Dispose();
                            format.Dispose();
                        }
                    }
                    base.BeginInvoke(new MethodInvoker(this.CalculatePageInfo));
                }
                else
                {
                    Point point2;
                    if (!this.layoutOk)
                    {
                        this.ComputeLayout();
                    }
                    Size  size  = new Size(PixelsToPhysical(new Point(base.Size), this.screendpi));
                    Point point = new Point(this.VirtualSize);
                    point2 = new Point(Math.Max(0, (base.Size.Width - point.X) / 2), Math.Max(0, (base.Size.Height - point.Y) / 2))
                    {
                        X = point2.X - this.Position.X,
                        Y = point2.Y - this.Position.Y
                    };
                    this.lastOffset = point2;
                    int         num            = PhysicalToPixels(10, this.screendpi.X);
                    int         num2           = PhysicalToPixels(10, this.screendpi.Y);
                    Region      clip           = pevent.Graphics.Clip;
                    Rectangle[] rectangleArray = new Rectangle[this.rows * this.columns];
                    Point       empty          = Point.Empty;
                    int         num3           = 0;
                    try
                    {
                        for (int j = 0; j < this.rows; j++)
                        {
                            empty.X = 0;
                            empty.Y = num3 * j;
                            for (int k = 0; k < this.columns; k++)
                            {
                                int index = (this.StartPage + k) + (j * this.columns);
                                if (index < this.pageInfo.Length)
                                {
                                    Size physicalSize = this.pageInfo[index].PhysicalSize;
                                    if (this.autoZoom)
                                    {
                                        double num7 = (size.Width - (10 * (this.columns + 1))) / ((double)(this.columns * physicalSize.Width));
                                        double num8 = (size.Height - (10 * (this.rows + 1))) / ((double)(this.rows * physicalSize.Height));
                                        this.zoom = Math.Min(num7, num8);
                                    }
                                    this.imageSize = new Size((int)(this.zoom * physicalSize.Width), (int)(this.zoom * physicalSize.Height));
                                    Point point4 = PhysicalToPixels(new Point(this.imageSize), this.screendpi);
                                    int   x      = (point2.X + (num * (k + 1))) + empty.X;
                                    int   y      = (point2.Y + (num2 * (j + 1))) + empty.Y;
                                    empty.X += point4.X;
                                    num3     = Math.Max(num3, point4.Y);
                                    rectangleArray[index - this.StartPage] = new Rectangle(x, y, point4.X, point4.Y);
                                    pevent.Graphics.ExcludeClip(rectangleArray[index - this.StartPage]);
                                }
                            }
                        }
                        pevent.Graphics.FillRectangle(brush, base.ClientRectangle);
                    }
                    finally
                    {
                        pevent.Graphics.Clip = clip;
                    }
                    for (int i = 0; i < rectangleArray.Length; i++)
                    {
                        if ((i + this.StartPage) < this.pageInfo.Length)
                        {
                            Rectangle rect = rectangleArray[i];
                            pevent.Graphics.DrawRectangle(Pens.Black, rect);
                            using (SolidBrush brush3 = new SolidBrush(this.ForeColor))
                            {
                                pevent.Graphics.FillRectangle(brush3, rect);
                            }
                            rect.Inflate(-1, -1);
                            if (this.pageInfo[i + this.StartPage].Image != null)
                            {
                                pevent.Graphics.DrawImage(this.pageInfo[i + this.StartPage].Image, rect);
                            }
                            rect.Width--;
                            rect.Height--;
                            pevent.Graphics.DrawRectangle(Pens.Black, rect);
                        }
                    }
                }
            }
Label_04E9:
            base.OnPaint(pevent);
        }
Пример #2
0
        /// <include file='doc\PrintPreviewControl.uex' path='docs/doc[@for="PrintPreviewControl.OnPaint"]/*' />
        /// <internalonly/>
        /// <devdoc>
        ///    <para>
        ///       Paints the control.
        ///    </para>
        /// </devdoc>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            Brush backBrush = new SolidBrush(BackColor);

            try {
                if (pageInfo == null || pageInfo.Length == 0)
                {
                    pevent.Graphics.FillRectangle(backBrush, ClientRectangle);

                    if (pageInfo != null || exceptionPrinting)
                    {
                        // Calculate formats
                        StringFormat format = new StringFormat();
                        format.Alignment     = ControlPaint.TranslateAlignment(ContentAlignment.MiddleCenter);
                        format.LineAlignment = ControlPaint.TranslateLineAlignment(ContentAlignment.MiddleCenter);

                        // Do actual drawing
                        SolidBrush brush = new SolidBrush(ForeColor);
                        try {
                            if (exceptionPrinting)
                            {
                                pevent.Graphics.DrawString(SR.PrintPreviewExceptionPrinting, Font, brush, ClientRectangle, format);
                            }
                            else
                            {
                                pevent.Graphics.DrawString(SR.PrintPreviewNoPages, Font, brush, ClientRectangle, format);
                            }
                        }
                        finally {
                            brush.Dispose();
                            format.Dispose();
                        }
                    }
                    else
                    {
                        BeginInvoke(new MethodInvoker(CalculatePageInfo));
                    }
                }
                else
                {
                    if (!layoutOk)
                    {
                        ComputeLayout();
                    }

                    Size controlPhysicalSize = new Size(PixelsToPhysical(new Point(Size), screendpi));

                    //Point imagePixels = PhysicalToPixels(new Point(imageSize), screendpi);
                    Point virtualPixels = new Point(VirtualSize);

                    // center pages on screen if small enough
                    Point offset = new Point(Math.Max(0, (Size.Width - virtualPixels.X) / 2),
                                             Math.Max(0, (Size.Height - virtualPixels.Y) / 2));
                    offset.X  -= Position.X;
                    offset.Y  -= Position.Y;
                    lastOffset = offset;

                    int borderPixelsX = PhysicalToPixels(border, screendpi.X);
                    int borderPixelsY = PhysicalToPixels(border, screendpi.Y);

                    Region      originalClip   = pevent.Graphics.Clip;
                    Rectangle[] pageRenderArea = new Rectangle[rows * columns];
                    Point       lastImageSize  = Point.Empty;
                    int         maxImageHeight = 0;

                    try {
                        for (int row = 0; row < rows; row++)
                        {
                            //Initialize our LastImageSize variable...
                            lastImageSize.X = 0;
                            lastImageSize.Y = maxImageHeight * row;

                            for (int column = 0; column < columns; column++)
                            {
                                int imageIndex = StartPage + column + row * columns;
                                if (imageIndex < pageInfo.Length)
                                {
                                    Size pageSize = pageInfo[imageIndex].PhysicalSize;
                                    if (autoZoom)
                                    {
                                        double zoomX = ((double)controlPhysicalSize.Width - border * (columns + 1)) / (columns * pageSize.Width);
                                        double zoomY = ((double)controlPhysicalSize.Height - border * (rows + 1)) / (rows * pageSize.Height);
                                        zoom = Math.Min(zoomX, zoomY);
                                    }

                                    imageSize = new Size((int)(zoom * pageSize.Width), (int)(zoom * pageSize.Height));
                                    Point imagePixels = PhysicalToPixels(new Point(imageSize), screendpi);


                                    int x = offset.X + borderPixelsX * (column + 1) + lastImageSize.X;
                                    int y = offset.Y + borderPixelsY * (row + 1) + lastImageSize.Y;

                                    lastImageSize.X += imagePixels.X;
                                    //The Height is the Max of any PageHeight..
                                    maxImageHeight = Math.Max(maxImageHeight, imagePixels.Y);

                                    pageRenderArea[imageIndex - StartPage] = new Rectangle(x, y, imagePixels.X, imagePixels.Y);
                                    pevent.Graphics.ExcludeClip(pageRenderArea[imageIndex - StartPage]);
                                }
                            }
                        }

                        pevent.Graphics.FillRectangle(backBrush, ClientRectangle);
                    }
                    finally {
                        pevent.Graphics.Clip = originalClip;
                    }

                    for (int i = 0; i < pageRenderArea.Length; i++)
                    {
                        if (i + StartPage < pageInfo.Length)
                        {
                            Rectangle box = pageRenderArea[i];
                            pevent.Graphics.DrawRectangle(Pens.Black, box);
                            using (SolidBrush brush = new SolidBrush(ForeColor)) {
                                pevent.Graphics.FillRectangle(brush, box);
                            }
                            box.Inflate(-1, -1);
                            if (pageInfo[i + StartPage].Image != null)
                            {
                                pevent.Graphics.DrawImage(pageInfo[i + StartPage].Image, box);
                            }
                            box.Width--;
                            box.Height--;
                            pevent.Graphics.DrawRectangle(Pens.Black, box);
                        }
                    }
                }
            }
            finally {
                backBrush.Dispose();
            }

            base.OnPaint(pevent); // raise paint event
        }
        /// <include file='doc\PrintPreviewControl.uex' path='docs/doc[@for="PrintPreviewControl.OnPaint"]/*' />
        /// <internalonly/>
        /// <devdoc>
        ///    <para>
        ///       Paints the control.
        ///    </para>
        /// </devdoc>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            Brush backBrush = new SolidBrush(BackColor);

            try {
                if (pageInfo == null || pageInfo.Length == 0)
                {
                    pevent.Graphics.FillRectangle(backBrush, ClientRectangle);

                    if (pageInfo != null || exceptionPrinting)
                    {
                        // Calculate formats
                        StringFormat format = new StringFormat();
                        format.Alignment     = ControlPaint.TranslateAlignment(ContentAlignment.MiddleCenter);
                        format.LineAlignment = ControlPaint.TranslateLineAlignment(ContentAlignment.MiddleCenter);

                        // Do actual drawing
                        SolidBrush brush = new SolidBrush(ForeColor);
                        try {
                            if (exceptionPrinting)
                            {
                                pevent.Graphics.DrawString(SR.GetString(SR.PrintPreviewExceptionPrinting), Font, brush, ClientRectangle, format);
                            }
                            else
                            {
                                pevent.Graphics.DrawString(SR.GetString(SR.PrintPreviewNoPages), Font, brush, ClientRectangle, format);
                            }
                        }
                        finally {
                            brush.Dispose();
                            format.Dispose();
                        }
                    }
                    else
                    {
                        BeginInvoke(new MethodInvoker(CalculatePageInfo));
                    }
                }
                else
                {
                    if (!layoutOk)
                    {
                        ComputeLayout();
                    }

                    Point imagePixels   = PhysicalToPixels(new Point(imageSize), screendpi);
                    Point virtualPixels = new Point(VirtualSize);

                    // center pages on screen if small enough
                    Point offset = new Point(Math.Max(0, (Size.Width - virtualPixels.X) / 2),
                                             Math.Max(0, (Size.Height - virtualPixels.Y) / 2));
                    offset.X  -= Position.X;
                    offset.Y  -= Position.Y;
                    lastOffset = offset;

                    int borderPixelsX = PhysicalToPixels(border, screendpi.X);
                    int borderPixelsY = PhysicalToPixels(border, screendpi.Y);

                    Region      originalClip   = pevent.Graphics.Clip;
                    Rectangle[] pageRenderArea = new Rectangle[rows * columns];

                    try {
                        for (int row = 0; row < rows; row++)
                        {
                            for (int column = 0; column < columns; column++)
                            {
                                int imageIndex = StartPage + column + row * columns;
                                if (imageIndex < pageInfo.Length)
                                {
                                    int x = offset.X + borderPixelsX * (column + 1) + imagePixels.X * column;
                                    int y = offset.Y + borderPixelsY * (row + 1) + imagePixels.Y * row;

                                    pageRenderArea[imageIndex - StartPage] = new Rectangle(x, y, imagePixels.X, imagePixels.Y);
                                    pevent.Graphics.ExcludeClip(pageRenderArea[imageIndex - StartPage]);
                                }
                            }
                        }

                        pevent.Graphics.FillRectangle(backBrush, ClientRectangle);
                    }
                    finally {
                        pevent.Graphics.Clip = originalClip;
                    }

                    for (int i = 0; i < pageRenderArea.Length; i++)
                    {
                        if (i + StartPage < pageInfo.Length)
                        {
                            Rectangle box = pageRenderArea[i];
                            pevent.Graphics.DrawRectangle(Pens.Black, box);
                            box.Inflate(-1, -1);
                            pevent.Graphics.FillRectangle(new SolidBrush(ForeColor), box);
                            if (pageInfo[i + StartPage].Image != null)
                            {
                                pevent.Graphics.DrawImage(pageInfo[i + StartPage].Image, box);
                            }
                            box.Width--;
                            box.Height--;
                            pevent.Graphics.DrawRectangle(Pens.Black, box);
                        }
                    }
                }
            }
            finally {
                backBrush.Dispose();
            }

            base.OnPaint(pevent); // raise paint event
        }