Ejemplo n.º 1
0
        /// <summary>
        /// 打印指定页面
        /// </summary>
        /// <param name="myPage">页面对象</param>
        /// <param name="g">绘图操作对象</param>
        /// <param name="MainClipRect">主剪切矩形</param>
        /// <param name="UseMargin">是否启用页边距</param>
        public virtual void DrawPage(
            PrintPage myPage,
            System.Drawing.Graphics g,
            System.Drawing.Rectangle MainClipRect,
            bool UseMargin)
        {
            //XPageSettings pageSettings = myPage.PageSettings;
            int LeftMargin   = 0;
            int TopMargin    = 0;
            int RightMargin  = 0;
            int BottomMargin = 0;

            if (UseMargin)
            {
                LeftMargin   = (int)myPage.ViewLeftMargin;
                TopMargin    = (int)myPage.ViewTopMargin;
                RightMargin  = (int)myPage.ViewRightMargin;
                BottomMargin = (int)myPage.ViewBottomMargin;
            }

            this.OnBeforeDrawPage(myPage, g);
            IntPtr          hdc = g.GetHdc();
            DeviceCapsClass dcc = new DeviceCapsClass(hdc);

            g.ReleaseHdc();

            g.PageUnit = myDocument.DocumentGraphicsUnit;
            System.Drawing.Rectangle ClipRect = System.Drawing.Rectangle.Empty;
            if (this.strPageHeadText != null)
            {
                // 绘制标题文本
                g.DrawString(
                    strPageHeadText,
                    System.Windows.Forms.Control.DefaultFont,
                    System.Drawing.Brushes.Red,
                    20,
                    20,
                    System.Drawing.StringFormat.GenericDefault);
            }

            float printableAreaOffsetX = (float)GraphicsUnitConvert.Convert(
                myPrintableAreaOffset.X / 100.0,
                System.Drawing.GraphicsUnit.Inch,
                myDocument.DocumentGraphicsUnit);
            float printableAreaOffsetY = (float)GraphicsUnitConvert.Convert(
                myPrintableAreaOffset.Y / 100.0,
                System.Drawing.GraphicsUnit.Inch,
                myDocument.DocumentGraphicsUnit);

            if (this.bolDrawHead)
            {
                // 绘制页眉
                if (myPage.HeaderHeight > 0)
                {
                    g.ResetTransform();
                    g.ResetClip();

                    ClipRect = new System.Drawing.Rectangle(
                        0,
                        0,
                        myPage.Width,
                        myPage.HeaderHeight);

                    g.ScaleTransform(this.XZoomRate, this.YZoomRate);
                    g.TranslateTransform(
                        LeftMargin - printableAreaOffsetX,
                        TopMargin - printableAreaOffsetY);

                    g.SetClip(new System.Drawing.Rectangle(
                                  ClipRect.Left,
                                  ClipRect.Top,
                                  ClipRect.Width + 1,
                                  ClipRect.Height + 1));

                    PageDocumentPaintEventArgs args = new PageDocumentPaintEventArgs(
                        g,
                        ClipRect,
                        myDocument,
                        myPage,
                        PageContentPartyStyle.Header);
                    args.ContentBounds = ClipRect;
                    args.PageIndex     = myPage.GlobalIndex;
                    args.NumberOfPages = this.Pages.Count;
                    args.ContentBounds = ClipRect;
                    myDocument.DrawContent(args);
                    //DesignPaintEventArgs e = new DesignPaintEventArgs( g , ClipRect );
                    //myDocument.RefreshView( e );
                }
                g.ResetClip();
                g.ResetTransform();
            }

            // 绘制页面正文
            ClipRect = new System.Drawing.Rectangle(
                0,
                myPage.Top,
                myPage.Width,
                myPage.Height);

            if (!MainClipRect.IsEmpty)
            {
                ClipRect = System.Drawing.Rectangle.Intersect(ClipRect, MainClipRect);
            }
            if (!ClipRect.IsEmpty)
            {
                g.ScaleTransform(this.XZoomRate, this.YZoomRate);
                g.TranslateTransform(
                    LeftMargin - printableAreaOffsetX,
                    TopMargin - myPage.Top + myPage.HeaderHeight - printableAreaOffsetY);

                //System.Drawing.Drawing2D.GraphicsPath clipPath = new System.Drawing.Drawing2D.GraphicsPath();
                //clipPath.AddRectangle( ClipRect );
                //g.SetClip( clipPath );

                //g.TranslateTransform( myPages.LeftMargin , myPages.TopMargin - myPage.Top + myPages.HeadHeight );

                System.Drawing.RectangleF rect = DrawerUtil.FixClipBounds(
                    g,
                    ClipRect.Left,
                    ClipRect.Top,
                    ClipRect.Width,
                    ClipRect.Height);

                rect.Offset(-4, -4);
                rect.Width  = rect.Width + 8;
                rect.Height = rect.Height + 8;
                g.SetClip(rect);

                //				System.Drawing.RectangleF rect2 = g.ClipBounds ;
                //				if( rect.Top < rect2.Top )
                //				{
                //					float dy = rect2.Top - rect.Top ;
                //					rect.Y = rect.Y - dy * 2 ;
                //					rect.Height = rect.Height + dy * 4 ;
                //				}
                //				g.SetClip( rect );

                PageDocumentPaintEventArgs args = new PageDocumentPaintEventArgs(
                    g,
                    ClipRect,
                    myDocument,
                    myPage,
                    PageContentPartyStyle.Body);
                args.PageIndex     = myPage.GlobalIndex;
                args.NumberOfPages = this.Pages.Count;
                args.ContentBounds = ClipRect;
                myDocument.DrawContent(args);

                //myDocument.DrawDocument( g , ClipRect );
                //DesignPaintEventArgs e = new DesignPaintEventArgs( g , ClipRect );
                //myDocument.RefreshView( e );
            }

            if (this.bolDrawFooter)
            {
                // 绘制页脚
                if (myPage.FooterHeight > 0)
                {
                    g.ResetClip();
                    g.ResetTransform();
                    int documentHeight = myPage.DocumentHeight;

                    ClipRect = new System.Drawing.Rectangle(
                        0,
                        documentHeight - myPage.FooterHeight,
                        myPage.Width,
                        myPage.FooterHeight);

                    int dy = 0;

                    if (UseMargin)
                    {
                        dy = (int)(myPage.ViewPaperHeight
                                   - myPage.ViewBottomMargin);
                    }
                    else
                    {
                        dy = (int)(myPage.ViewPaperHeight
                                   - myPage.ViewBottomMargin
                                   - myPage.ViewTopMargin);
                    }


                    g.ScaleTransform(this.XZoomRate, this.YZoomRate);
                    g.TranslateTransform(
                        LeftMargin - printableAreaOffsetX,
                        dy - printableAreaOffsetY);

                    g.SetClip(new System.Drawing.Rectangle(
                                  ClipRect.Left,
                                  ClipRect.Top,
                                  ClipRect.Width + 1,
                                  ClipRect.Height + 1));

                    PageDocumentPaintEventArgs args = new PageDocumentPaintEventArgs(
                        g,
                        ClipRect,
                        myDocument,
                        myPage,
                        PageContentPartyStyle.Footer);
                    args.ContentBounds = ClipRect;
                    args.PageIndex     = myPage.GlobalIndex;
                    args.NumberOfPages = this.Pages.Count;
                    myDocument.DrawContent(args);
                    //DesignPaintEventArgs e = new DesignPaintEventArgs( g , ClipRect );
                    //myDocument.RefreshView( e );
                }
            } //if( this.bolDrawFooter )
        }     //public void DrawPage()
Ejemplo n.º 2
0
        /// <summary>
        /// 已重载:绘制文档内容
        /// </summary>
        /// <param name="e">绘制事件参数</param>
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            //if (this.DesignMode)
            {
                base.OnPaint(e);
            }
            if (this.IsUpdating)
            {
                return;
            }

            System.Drawing.Rectangle clipRect = e.ClipRectangle;
            clipRect.Height += 1;
            System.Drawing.Point sp = this.AutoScrollPosition;
            //int ax = - this.AutoScrollPosition.X ;
            //int ay = - this.AutoScrollPosition.Y ;
            this.RefreshScaleTransform();
            if (this.PagesTransform == null || this.PagesTransform.Count == 0)
            {
                // 没有任何内容可以绘制
                return;
            }
            if (this.ViewMode == PageViewMode.Normal)
            {
                using (SolidBrush b = new SolidBrush(this.PageBackColor))
                {
                    e.Graphics.FillRectangle(b, e.ClipRectangle);
                }
                SimpleRectangleTransform item = this.PagesTransform[0];
                PrintPage myPage = (PrintPage)item.PageObject;

                Rectangle rect = item.SourceRect;
                rect.Width = rect.Width + 30;
                rect       = Rectangle.Intersect(
                    clipRect,
                    rect);

                if (rect.IsEmpty == false)
                {
                    // 保存状态
                    System.Drawing.Drawing2D.GraphicsState state2 = e.Graphics.Save();

                    //try
                    {
                        PaintEventArgs e2 = this.CreatePaintEventArgs(e, item);
                        if (e2 != null)
                        {
                            PageDocumentPaintEventArgs e3 = new PageDocumentPaintEventArgs(
                                e2.Graphics,
                                e2.ClipRectangle,
                                myPage.Document,
                                myPage,
                                item.ContentStyle);
                            e3.ContentBounds = item.DescRect;
                            e3.RenderMode    = ContentRenderMode.Paint;
                            e3.PageIndex     = myPage.PageIndex;
                            e3.NumberOfPages = this.Pages.Count;
                            //e3.EditMode = this.EditMode;
                            if (myPage.Document != null)
                            {
                                myPage.Document.DrawContent(e3);
                            }//if
                        }
                    }
                    //catch (Exception ext)
                    //{
                    //    System.Console.WriteLine(ext.ToString());
                    //}
                    // 恢复状态
                    e.Graphics.Restore(state2);
                }
                return;
            }
//
            MultiPageTransform trans = ( MultiPageTransform )this.Transform;

//			trans.ClearSourceOffset();
//			trans.OffsetSource( sp.X , sp.Y , true );

            System.Drawing.Graphics g = e.Graphics;
            //System.Drawing.Drawing2D.GraphicsState stateBack = e.Graphics.Save();
            foreach (PrintPage myPage in this.Pages)
            {
                System.Drawing.Rectangle ClientBounds = myPage.ClientBounds;
                ClientBounds.Offset(sp);
                ClientBounds.Width = ClientBounds.Width + 20;
                //if( clipRect.Top <= ClientBounds.Bottom  + 5
                //    && clipRect.Bottom >= ClientBounds.Top )
                if (clipRect.IntersectsWith(
                        new Rectangle(
                            ClientBounds.Left,
                            ClientBounds.Top,
                            ClientBounds.Width + 5,
                            ClientBounds.Height + 5)))
                {
                    //this.SetPageIndex( myPage.Index );

                    //e.Graphics.Restore(stateBack);
                    //e.Graphics.ResetClip();
                    DrawPageFrame(
                        myPage,
                        e.Graphics,
                        clipRect,
                        true);

                    for (int iCount = trans.Count - 1; iCount >= 0; iCount--)
                    {
                        SimpleRectangleTransform item = trans[iCount];
                        if (item.Visible && item.PageObject == myPage)
                        {
                            // 显示页眉页脚标记文本
                            if (item.ContentStyle == PageContentPartyStyle.Header)
                            {
                                if (this.HeaderFooterFlagVisible == HeaderFooterFlagVisible.Header ||
                                    this.HeaderFooterFlagVisible == HeaderFooterFlagVisible.HeaderFooter)
                                {
                                    // 绘制页眉标记
                                    //e.Graphics.Restore(stateBack);
                                    DrawHeaderFooterFlag(
                                        PrintingResources.Header,
                                        item.PartialAreaSourceBounds,
                                        e.Graphics);
                                }
                            }
                            else if (item.ContentStyle == PageContentPartyStyle.Footer)
                            {
                                if (this.HeaderFooterFlagVisible == HeaderFooterFlagVisible.Footer ||
                                    this.HeaderFooterFlagVisible == HeaderFooterFlagVisible.HeaderFooter)
                                {
                                    // 绘制页脚标记
                                    //e.Graphics.Restore(stateBack);
                                    DrawHeaderFooterFlag(
                                        PrintingResources.Footer,
                                        item.PartialAreaSourceBounds,
                                        e.Graphics);
                                }
                            }
                            Rectangle rect = item.SourceRect;

                            rect = Rectangle.Intersect(
                                clipRect,
                                rect);

                            if (rect.IsEmpty == false)
                            {
                                System.Drawing.Drawing2D.GraphicsState state2 = e.Graphics.Save();

                                PaintEventArgs e2 = this.CreatePaintEventArgs(e, item);
                                if (e2 != null)
                                {
                                    PageDocumentPaintEventArgs e3 = new PageDocumentPaintEventArgs(
                                        e2.Graphics,
                                        e2.ClipRectangle,
                                        myPage.Document,
                                        myPage,
                                        item.ContentStyle);
                                    e3.ContentBounds = item.DescRect;
                                    e3.RenderMode    = ContentRenderMode.Paint;
                                    e3.PageIndex     = myPage.PageIndex;
                                    e3.NumberOfPages = this.Pages.Count;
                                    //e3.EditMode = this.EditMode;
                                    if (myPage.Document != null)
                                    {
                                        myPage.Document.DrawContent(e3);
                                    }//if
                                }

                                e.Graphics.Restore(state2);

                                if (item.Enable == false)
                                {
                                    // 若区域无效则用白色半透明进行覆盖,以作标记
                                    using (System.Drawing.SolidBrush maskBrush
                                               = new SolidBrush(Color.FromArgb(140, this.PageBackColor)))
                                    {
                                        e.Graphics.FillRectangle(
                                            maskBrush,
                                            rect.Left,
                                            rect.Top,
                                            rect.Width + 2,
                                            rect.Height + 2);
                                    }
                                }
                            }                            //if
                            // ClipRect.Height -= 1;
                        }
                    }    //foreach
                }        //if
            }            //foreach
            DrawHeadShadow();
            //base.OnPaint( e );
            //e.Graphics.Flush(System.Drawing.Drawing2D.FlushIntention.Sync);
            //System.Threading.Thread.Sleep(100);
        }