• C#Winform中打印预览时设置横向打印


    因项目需要,在做一个Screen的打印时,因为Screen中的DataGridView的表格很长,需要横向排列才能完整的打印,因此设置PrintDocument.PrinterSettings.DefaultPageSettings.Landscape = true。但是在预览的时候仍然是纵向排列。伤脑筋。。。
    花了一些时间做其他的设置均未有效。用Reflector查看PrintController的PrintLoop方法,PrintDocument的打印相关的方法被调用的顺序是这样的:
    QueryPageSettings
    StartPage
    PrintPage
    EndPage
    目前只处理了StartPage和PrintPage。
    于是加上了QueryPageSettings事件的处理,发现每次执行打印时QueryPageSettingsEventArgs.PageSettings.Landscape仍然为false,虽然前面设置了DefaultpageSettings.Landscape=true。
    于是在此事件中将其指定为true,再次预览时,终于可以横向看到排列整齐的报表了。
    private void button2_Click(object sender, EventArgs e)
    {
        PrintPreviewDialog pd = new PrintPreviewDialog();
        pd.Document = new PrintDocument();
        pd.Document.PrintPage += new PrintPageEventHandler(Document_PrintPage);
        pd.Document.QueryPageSettings += new QueryPageSettingsEventHandler(Document_QueryPageSettings);
        pd.Document.BeginPrint += new PrintEventHandler(Document_BeginPrint);
        if (pd.ShowDialog(this) == DialogResult.OK)
        {
        }
    }
      
    void Document_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
    {
        e.PageSettings.Landscape = true;
        int index = -1;
        for (int i=0;i<e.PageSettings.PrinterSettings.PaperSizes.Count;i++)
        {
            if (e.PageSettings.PrinterSettings.PaperSizes[i].PaperName== "A4")
            {
                index=i;
                break;
            }
        }
        if (index != -1)
        {
            e.PageSettings.PaperSize = e.PageSettings.PrinterSettings.PaperSizes[index];
        }
    }
      
    void Document_BeginPrint(object sender, PrintEventArgs e)
    {
    }
      
    void Document_PrintPage(object sender, PrintPageEventArgs e)
    {
        using (Bitmap bit = new Bitmap(this.listBox.Width, this.listBox.Height))
        {
            this.listBox.DrawToBitmap(bit, this.listBox.ClientRectangle);
            e.Graphics.DrawImage(bit, new Point(0, 0));
        }
    }
  • 相关阅读:
    QK对中断的特殊处理
    程序控制的软件复位方法
    软件的按契约设计(DbC---Design by Contract)
    Arduino平台基于DbC的软件调试
    软件测试中的测不准原理
    关于嵌入式软件
    程序设计的SOLID原则
    CPS---(Cyber-Physical Sytem,信息物理融合系统)
    QP之QF原理
    QP之QEP原理
  • 原文地址:https://www.cnblogs.com/chenbg2001/p/3003892.html
Copyright © 2020-2023  润新知