Esempio n. 1
0
 public static void MakePrintPreviewDialogMaximized(PrintPreviewDialog ppd)
 {
     System.Reflection.PropertyInfo[] pis = ppd.GetType().GetProperties();
     for (int i = 0; i < pis.Length; i++)
     {
         if (pis[i].Name == "WindowState")
         {
             pis[i].SetValue(ppd, FormWindowState.Maximized, null);
             break;
         }
     }
 }
Esempio n. 2
0
        private void SetPrintPreviewDialog(PrintPreviewDialog pPreviewDialog)
        {
            System.Reflection.PropertyInfo[] pis = pPreviewDialog.GetType().GetProperties();
            for (int i = 0; i < pis.Length; i++)
            {
                if (pis[i].CanWrite)
                {
                    switch (pis[i].Name)
                    {
                    case "Dock":
                        pis[i].SetValue(pPreviewDialog, DockStyle.Fill, null);
                        break;

                    case "FormBorderStyle":
                        pis[i].SetValue(pPreviewDialog, FormBorderStyle.None, null);
                        break;

                    case "WindowState":
                        pis[i].SetValue(pPreviewDialog, FormWindowState.Normal, null);
                        break;

                    default:
                        break;
                    }
                }
            }
            #region 屏蔽默认的打印按钮,添加自定义的打印和保存按钮
            foreach (Control c in pPreviewDialog.Controls)
            {
                if (c is ToolStrip)
                {
                    ToolStrip ts = (ToolStrip)c;
                    ts.Items[0].Visible = false;
                    //print
                    ToolStripButton toolStripBtn_Print = new ToolStripButton();
                    toolStripBtn_Print.Text        = "打印报表";
                    toolStripBtn_Print.ToolTipText = "打印当前报表";
                    toolStripBtn_Print.Image       = Properties.Resources.Printer.ToBitmap();
                    toolStripBtn_Print.Click      +=
                        delegate(object sender, EventArgs e)
                    {
                        PrintDialog pd = new PrintDialog();
                        pd.Document    = pPreviewDialog.Document;
                        pd.UseEXDialog = true;
                        if (pd.ShowDialog() == DialogResult.OK)
                        {
                            pPreviewDialog.Document.Print();
                        }
                    };
                    ToolStripButton toolStripBtn_SaveAsExcel = new ToolStripButton();
                    toolStripBtn_SaveAsExcel.Text        = "导出到Excel";
                    toolStripBtn_SaveAsExcel.ToolTipText = "导出报表到Excel";
                    toolStripBtn_SaveAsExcel.Image       = Properties.Resources.Excel.ToBitmap();
                    toolStripBtn_SaveAsExcel.Click      +=
                        delegate(object sender, EventArgs e)
                    {
                        SaveFileDialog f = new SaveFileDialog();
                        f.Title  = "数据保存到Excel...";
                        f.Filter = "Excel文档(*.xls)|*.xls";
                        if (f.ShowDialog() == DialogResult.OK)
                        {
                            Exception ex = SaveAsExcel(f.FileName);
                            if (ex == null)
                            {
                                MessageBox.Show("导出成功!");
                            }
                            else
                            {
                                MessageBox.Show("导出失败:" + ex.Message);
                            }
                        }
                        f.Dispose();
                    };
                    ToolStripSeparator tss = new ToolStripSeparator();
                    ts.Items.Insert(0, toolStripBtn_Print);
                    ts.Items.Insert(1, toolStripBtn_SaveAsExcel);
                    ts.Items.Insert(2, tss);
                }
            }
            #endregion
        }