using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace RyPrint
{
    public partial class frmPrintPreview : Form
    {
        public frmPrintPreview()
        {
            InitializeComponent();
        }
        private PrintDocument m_PrintDoc;
        [Description("打印文档")]
        public PrintDocument PrintDoc
        {
            get { return m_PrintDoc; }
            set { m_PrintDoc = value; }
        }
        private int m_TotalPageCount;
        [Description("总页数")]
        public int TotalPageCount
        {
            get { return m_TotalPageCount; }
            set { m_TotalPageCount = value; }
        }
        public SmartPrint _SmartPrint = null;
        private void frmPrintPreview_Load(object sender, EventArgs e)
        {
            try
            {
                m_PrintDoc.PrinterSettings.FromPage = 0;
                m_PrintDoc.EndPrint += Print_Doc_EndPrint;
                //PaperSize sPSize = new PaperSize("发货清单纸", Convert.ToInt32(clsPram.PageWidth * (decimal)33.375), Convert.ToInt32(clsPram.PageHeight * (decimal)33.375));//267为英寸转厘米为8厘米
                //printDocument1.DefaultPageSettings.PaperSize = sPSize;
                printPreviewControl1.Document = m_PrintDoc;
                printDialog1.Document = m_PrintDoc;
                pageSetupDialog1.Document = m_PrintDoc;
                printPreviewControl1.Zoom = 1;
                this.Text = "打印预览";
            }
            catch
            {
                MessageBox.Show("没有安装打印机。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public void Print_Doc_EndPrint(object sender, PrintEventArgs e)
        {
            TotalPageCount = _SmartPrint.curPageIndex + 1;
            this.Text = "打印预览(共" + (_SmartPrint.curPageIndex+1) + "页,当前第"+ (printPreviewControl1.StartPage+1) + "页)";
        }
        private void BtnPrint_Click(object sender, EventArgs e)
        {
            printPreviewControl1.Document.PrinterSettings.FromPage = 0;
            printDialog1.AllowSomePages = true;
            DialogResult dr = printDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                _SmartPrint.IsPreview = false;
                if (printDialog1.PrinterSettings.PrintRange == PrintRange.SomePages)
                {
                    _SmartPrint.curPageIndex = printDialog1.PrinterSettings.FromPage;
                    _SmartPrint.MaxPage = printDialog1.PrinterSettings.ToPage;
                }
                else
                {
                    _SmartPrint.curPageIndex = 0;
                    _SmartPrint.MaxPage = -1;
                }
                printPreviewControl1.Document.Print();
            }
        }

        private void BtnSetting_Click(object sender, EventArgs e)
        {
            //frmSettingPrint frm = new frmSettingPrint();
            //frm.ShowDialog();
            //if (frm.DialogResult == DialogResult.Yes)
            //{
                //clsPram.LoadPrintSetting(Application.StartupPath + "\\Db\\PrintSetting.dat");
                printPreviewControl1.Document.DefaultPageSettings.Landscape = true;    //横向打印
                //PaperSize sPSize = new PaperSize("发货清单纸", Convert.ToInt32(clsPram.PageWidth * (decimal)33.375), Convert.ToInt32(clsPram.PageHeight * (decimal)33.375));//267为英寸转厘米为8厘米
                //printPreviewControl1.Document.DefaultPageSettings.PaperSize = sPSize;                                                                                                                              //PaperSize sPSize = new PaperSize("快递单", (int)pixelsToInches(image.Width * 100), (int)pixelsToInches(image.Height * 100));
                printPreviewControl1.InvalidatePreview();
            //}
        }

        private void BtnPrev_Click(object sender, EventArgs e)
        {
            if (printPreviewControl1.StartPage > 0)
            {
                printPreviewControl1.StartPage -= 1;
            }
            this.Text = "打印预览(共" + (_SmartPrint.curPageIndex + 1) + "页,当前第" + (printPreviewControl1.StartPage + 1) + "页)";
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            if (printPreviewControl1.StartPage < TotalPageCount - 1)
            {
                printPreviewControl1.StartPage += 1;
            }
            this.Text = "打印预览(共" + (_SmartPrint.curPageIndex + 1) + "页,当前第" + (printPreviewControl1.StartPage + 1) + "页)";
        }

        private void frmPrintPreview_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_PrintDoc.EndPrint -= Print_Doc_EndPrint;
        }
    }
}
