인쇄 기능 을 지원 하 는 ListView 클래스

23292 단어 F#
최근 에 C \ # 로 작은 유 료 시스템 을 만 들 었 습 니 다. 인쇄 기능 을 지원 하 는 ListView 류 를 사용 해 야 합 니 다. 인터넷 에서 다른 사람의 다음 과 같은 것 을 찾 았 습 니 다.
http://www.chinaaspx.com/comm/dotnetbbs/Showtopic.aspx?Forum_ID=6&Id=156821
그러나 사용 할 때 인쇄 할 때 첫 페이지 와 꼬 릿 말 이 나 왔 지만 표 자 체 는 나 오지 않 았 다.
추적 을 통 해 수정 코드 는 다음 과 같 습 니 다.

    /// <summary> 
    /// ListView             。 
    /// </summary> 
    public class PrintListView : ListView
    {
        /// <summary> 
        ///           ,     true 
        /// </summary> 
        private bool m_bIsPreview;

        /// <summary> 
        ///           ,     true 
        /// </summary> 
        private bool m_bIsAlwaysPrintHeader;

        /// <summary> 
        ///            
        /// </summary> 
        private string m_sPrintHeaderString;

        /// <summary> 
        ///      
        /// </summary> 
        private Font m_oHeaderFont;

        /// <summary> 
        ///      
        /// </summary> 
        private Font m_oBodyFont;

        /// <summary> 
        ///      
        /// </summary> 
        private Font m_oTailFont;

        /// <summary> 
        ///       ColumnHeader   ,              
        /// </summary> 
        private Font m_oColumnHeaderFont;

        /// <summary> 
        ///      
        /// </summary> 
        private PrintDocument m_oPrintDoc;


        /// <summary> 
        ///     
        /// </summary> 
        private int m_nLineSpace;


        private int m_nPrintWidth;            //        
        private int m_nPrintHeight;            //        
        private int m_nPageCount;            //      
        private int m_nCurPrintPage;         //           
        private int m_nTotalPage;            //       
        private int m_nFromPage;            //            
        private int m_nCurPrintItem;         //           

        private Rectangle m_oHeaderRect;      //         
        private Rectangle m_oBodyRect;         //         
        private Rectangle m_oTailRect;         //         

        private Brush defaultBrush;
        private Pen defaultPen;

        /// <summary> 
        ///               
        /// </summary> 
        public bool IsPreview
        {
            get { return m_bIsPreview; }
            set { m_bIsPreview = value; }
        }


        /// <summary> 
        ///               
        /// </summary> 
        public bool IsAlwaysPrintHeader
        {
            get { return m_bIsAlwaysPrintHeader; }
            set { m_bIsAlwaysPrintHeader = value; }
        }


        /// <summary> 
        ///               
        /// </summary> 
        public string PrintHeaderString
        {
            get { return m_sPrintHeaderString; }
            set { if (value != null) m_sPrintHeaderString = value; }
        }


        /// <summary> 
        ///           
        /// </summary> 
        public Font HeaderFont
        {
            set { m_oHeaderFont = value; }
            get { return m_oHeaderFont; }
        }


        /// <summary> 
        ///           
        ///          Bold       true ,    Exception       
        /// </summary> 
        public Font BodyFont
        {
            set
            {
                if (value == null)
                    return;
                if (value.Bold == true)
                {
                    throw new Exception("         [  ]   .");
                }
                else
                {
                    m_oBodyFont = value;
                }
            }
            get { return m_oBodyFont; }
        }


        /// <summary> 
        ///           
        /// </summary> 
        public Font TailFont
        {
            set { m_oTailFont = value; }
            get { return m_oTailFont; }
        }


        /// <summary> 
        ///          
        /// </summary> 
        public int LineSpace
        {
            get { return m_nLineSpace; }
            set
            {
                if (value < 0)
                {
                    m_nLineSpace = 0;
                }
                m_nLineSpace = value;
            }
        }


        /// <summary> 
        ///     ,             
        /// </summary> 
        public PrintListView()
        {
            m_bIsPreview = true;
            m_bIsAlwaysPrintHeader = true;
            m_sPrintHeaderString = "";
            m_oHeaderFont = null;
            m_oTailFont = null;
            m_oBodyFont = null;
            m_oColumnHeaderFont = null;
            m_oPrintDoc = null;

            m_nPrintWidth = 0;
            m_nPrintHeight = 0;
            m_nPageCount = 0;
            m_nCurPrintPage = 1;
            m_nFromPage = 1;
            m_nLineSpace = 0;
            m_nCurPrintItem = 0;

            defaultBrush = Brushes.Black;
            defaultPen = new Pen(defaultBrush, 1);
        }


        /// <summary> 
        ///            
        /// </summary> 
        /// <returns></returns> 
        private void InitPrintDocument()
        {
            m_oPrintDoc = new PrintDocument();
            m_oPrintDoc.DocumentName = m_sPrintHeaderString;
            if (m_oPrintDoc.PrinterSettings.PrinterName == "<no default printer>")
            {
                throw new Exception("        .");
            }
            else
            {
                m_oPrintDoc.PrintPage += new PrintPageEventHandler(PrintPage);
                m_oPrintDoc.BeginPrint += new PrintEventHandler(BeginPrint);
                m_oPrintDoc.EndPrint += new PrintEventHandler(EndPrint);
            }
            /*        */
            if (m_oHeaderFont == null)
            {
                m_oHeaderFont = new Font("  _GB2312", 16, FontStyle.Bold, GraphicsUnit.World);
            }

            if (m_oBodyFont == null)
            {
                m_oBodyFont = new Font("  _GB2312", 14, FontStyle.Regular, GraphicsUnit.World);
            }

            m_oColumnHeaderFont = new Font(m_oBodyFont, FontStyle.Bold);

            if (m_oTailFont == null)
            {
                m_oTailFont = new Font("  _GB2312", 12, FontStyle.Regular, GraphicsUnit.World);
            }
        }


        /// <summary> 
        ///           
        /// </summary> 
        private bool InitPrintPage(out Margins margins)
        {
            margins = null;

            /*      listview      */
            Graphics g = this.CreateGraphics();
            float x = g.DpiX;
            g.Dispose();

            /*           */
            PageSetupDialog ps = new PageSetupDialog();
            ps.Document = m_oPrintDoc;
            if (ps.ShowDialog() == DialogResult.Cancel)
            {
                return false;
            }
            else
            {               //                  ,         1/100 Inch 
                m_nPrintWidth = ps.PageSettings.Bounds.Width - ps.PageSettings.Margins.Left - ps.PageSettings.Margins.Right;
                m_nPrintHeight = ps.PageSettings.Bounds.Height - ps.PageSettings.Margins.Top - ps.PageSettings.Margins.Bottom;
                margins = ps.PageSettings.Margins;
            }
            if (m_nPrintWidth <= 0 || m_nPrintHeight <= 0)
            {
                throw new Exception("      .");
            }

            /*     listview   column         ,           */
            int listViewWidth = 0;
            for (int i = 0; i < this.Columns.Count; i++)
            {
                listViewWidth += this.Columns[i].Width;
            }
            if (Convert.ToInt32(listViewWidth / x * 100) > m_nPrintWidth)
            {
                //throw new Exception("           !\r
;\r
。"); } m_oPrintDoc.DefaultPageSettings = ps.PageSettings; return true; } /// <summary> /// /// </summary> private bool InitPrintPageNumber(Margins margins) { /* */ int headerFontHeight = m_oHeaderFont.Height; int columnHeaderFontHeight = m_oColumnHeaderFont.Height; int bodyFontHeight = m_oBodyFont.Height; int tailFontHeight = m_oTailFont.Height; // m_oHeaderRect = new Rectangle(margins.Left, margins.Top, m_nPrintWidth, headerFontHeight + m_nLineSpace + 3); int tailHeight = tailFontHeight + m_nLineSpace + 3; // , ( ) m_oTailRect = new Rectangle(margins.Left, margins.Top + m_nPrintHeight - tailHeight, m_nPrintWidth, tailHeight); // m_oBodyRect = new Rectangle(margins.Left, m_oHeaderRect.Bottom + 2, m_nPrintWidth, m_oTailRect.Top - m_oHeaderRect.Bottom - 4); /* */ int printItemPerPage = 0; int firstPageItem = Convert.ToInt32((m_oBodyRect.Height - columnHeaderFontHeight - m_nLineSpace) / (bodyFontHeight + m_nLineSpace)); if (firstPageItem >= this.Items.Count) { // m_nPageCount = 1; } else { // printItemPerPage = firstPageItem; int leftItems = this.Items.Count - firstPageItem; if (m_bIsAlwaysPrintHeader == false) { printItemPerPage = (m_oBodyRect.Height + m_oHeaderRect.Height + 2 - columnHeaderFontHeight - m_nLineSpace) / (bodyFontHeight + m_nLineSpace); } if (leftItems % printItemPerPage == 0) { m_nPageCount = leftItems / printItemPerPage + 1; } else { m_nPageCount = leftItems / printItemPerPage + 2; } } m_nTotalPage = m_nPageCount; /* */ PrintDialog pd = new PrintDialog(); pd.Document = m_oPrintDoc; pd.PrinterSettings.MinimumPage = 1; pd.PrinterSettings.MaximumPage = m_nPageCount; pd.PrinterSettings.FromPage = 1; pd.PrinterSettings.ToPage = m_nPageCount; pd.AllowPrintToFile = false; // if (m_nPageCount > 1) { pd.AllowSelection = true; } else { pd.AllowSelection = false; } pd.AllowSomePages = true; if (pd.ShowDialog() == DialogResult.OK) { m_nPageCount = pd.PrinterSettings.ToPage - pd.PrinterSettings.FromPage + 1; if (pd.PrinterSettings.FromPage > 1) { m_nCurPrintItem = firstPageItem + (pd.PrinterSettings.FromPage - 2) * printItemPerPage; } else { m_nCurPrintItem = 0; } m_nFromPage = pd.PrinterSettings.FromPage; m_oPrintDoc.DocumentName = m_nPageCount.ToString(); m_oPrintDoc.PrinterSettings = pd.PrinterSettings; return true; } else { return false; } } /// <summary> /// ListView /// </summary> public virtual void DoPrint() { if (this.Items.Count <= 0) { throw new Exception(" ."); } InitPrintDocument(); Margins margins = null; if (InitPrintPage(out margins) == false) return; if (InitPrintPageNumber(margins) == false) return; if (m_bIsPreview == false) { m_oPrintDoc.Print(); } else { PrintPreviewDialog pd = new PrintPreviewDialog(); pd.Document = m_oPrintDoc; pd.PrintPreviewControl.Zoom = 1.0; pd.WindowState = FormWindowState.Maximized; pd.ShowInTaskbar = true; pd.ShowDialog(); } } /// <summary> /// /// </summary> /// <param name="g"></param> protected virtual void PrintPageHeader(Graphics g) { RectangleF textRect = new RectangleF(m_oHeaderRect.X, m_oHeaderRect.Y, m_oHeaderRect.Width, m_oHeaderRect.Height - 3); CenterText(g, m_sPrintHeaderString, m_oHeaderFont, defaultBrush, textRect); g.DrawLine(defaultPen, m_oHeaderRect.X, m_oHeaderRect.Bottom - 2, m_oHeaderRect.Right, m_oHeaderRect.Bottom - 2); } /// <summary> /// /// </summary> /// <param name="g"></param> protected virtual void PrintPageBody(Graphics g) { Rectangle textRect = m_oBodyRect; if (m_bIsAlwaysPrintHeader == false && m_nCurPrintPage != 1) { textRect = new Rectangle(m_oHeaderRect.X, m_oHeaderRect.Y, m_oHeaderRect.Width, m_oHeaderRect.Height + 2 + m_oBodyRect.Height); } /* , columnHeader */ int columnHeaderFontHeight = m_oColumnHeaderFont.Height; int bodyFontHeight = m_oBodyFont.Height; Rectangle columnHeaderRect = new Rectangle(textRect.X, textRect.Y, textRect.Width, columnHeaderFontHeight + m_nLineSpace); DrawColumnHeader(g, m_oColumnHeaderFont, defaultBrush, columnHeaderRect); /* */ int itemHeight = bodyFontHeight + m_nLineSpace; Rectangle itemRect; int yPos = columnHeaderRect.Bottom; int printItemPerPage = (textRect.Height - columnHeaderRect.Height) / (bodyFontHeight + m_nLineSpace); for (int i = 0; (i < printItemPerPage) && (m_nCurPrintItem < this.Items.Count); i++) { itemRect = new Rectangle(textRect.X, yPos, textRect.Width, itemHeight); DrawItem(g, m_oBodyFont, defaultBrush, itemRect); m_nCurPrintItem++; yPos += itemHeight; } m_nCurPrintItem = 0; // added by Wooce } /// <summary> /// /// </summary> /// <param name="g"></param> protected virtual void PrintPageTail(Graphics g) { g.DrawLine(defaultPen, m_oTailRect.X, m_oTailRect.Top + 1, m_oTailRect.Right, m_oTailRect.Top + 1); RectangleF textRect = new RectangleF(m_oTailRect.X, m_oTailRect.Y + 3, m_oTailRect.Width, m_oTailRect.Height - 3); string text = " " + m_nFromPage.ToString() + " " + m_nTotalPage.ToString() + " "; m_nFromPage++; CenterText(g, text, m_oTailFont, defaultBrush, textRect); string time = " :" + DateTime.Now.ToLongDateString() + " "; RightText(g, time, m_oTailFont, defaultBrush, textRect); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PrintPage(object sender, PrintPageEventArgs e) { e.Graphics.PageUnit = GraphicsUnit.Inch; e.Graphics.PageScale = .01f; if (m_bIsAlwaysPrintHeader == true) { PrintPageHeader(e.Graphics); } else { if (m_nCurPrintPage == 1) { PrintPageHeader(e.Graphics); } } PrintPageBody(e.Graphics); PrintPageTail(e.Graphics); if (m_nCurPrintPage == m_nPageCount) { e.HasMorePages = false; } else { e.HasMorePages = true; } m_nCurPrintPage++; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BeginPrint(object sender, PrintEventArgs e) { m_nCurPrintPage = 1; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EndPrint(object sender, PrintEventArgs e) { } /// <summary> /// /// </summary> private void CenterText(Graphics g, string t, Font f, Brush b, RectangleF rect) { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; g.DrawString(t, f, b, rect, sf); } /// <summary> /// /// </summary> private void RightText(Graphics g, string t, Font f, Brush b, RectangleF rect) { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Far; sf.LineAlignment = StringAlignment.Center; g.DrawString(t, f, b, rect, sf); } /// <summary> /// /// </summary> private void LeftText(Graphics g, string t, Font f, Brush b, RectangleF rect) { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Center; g.DrawString(t, f, b, rect, sf); } /// <summary> /// listview columnheader /// </summary> private void DrawColumnHeader(Graphics g, Font f, Brush b, Rectangle rect) { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; Rectangle itemRect; Point location = new Point(rect.Location.X, rect.Location.Y); Size itemSize; for (int i = 0; i < this.Columns.Count; i++) { itemSize = new Size(this.Columns[i].Width, rect.Height); itemRect = new Rectangle(location, itemSize); g.DrawRectangle(defaultPen, itemRect); g.DrawString(this.Columns[i].Text, f, b, itemRect, sf); location.X += this.Columns[i].Width; } } /// <summary> /// listview item /// </summary> private void DrawItem(Graphics g, Font f, Brush b, Rectangle rect) { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Center; Rectangle itemRect; Point location = new Point(rect.Location.X, rect.Location.Y); Size itemSize; for (int i = 0; i < this.Columns.Count; i++) { itemSize = new Size(this.Columns[i].Width, rect.Height); itemRect = new Rectangle(location, itemSize); g.DrawRectangle(defaultPen, itemRect); g.DrawString(this.Items[m_nCurPrintItem].SubItems[i].Text, f, b, itemRect, sf); location.X += this.Columns[i].Width; } } }

바로... 이다        protected virtual void PrintPageBody (Graphics g) 함수 의 끝 을 채 워 야 합 니 다.
            m_nCurPrintItem = 0; // added by Wooce

좋은 웹페이지 즐겨찾기