• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

gisoracle

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

自定义打印预览

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

namespace ModifyPrintView
{
public partial class Form2 : PrintPreviewDialog
{
public Form2()
{
InitializeComponent();
}
//作为正式打印标志
private bool is_Print;
public bool isPrint
{
get { return is_Print; }
set { is_Print = value; }
}
public void Add()
{
foreach (Control c in Controls)
{
if (c.GetType() == typeof(ToolStrip))
{
ToolStrip ts = c as ToolStrip;

ToolStripButton page_setup = new ToolStripButton("页面设置");
page_setup.Size = new Size(23, 22);
page_setup.ImageTransparentColor = System.Drawing.Color.Magenta;
ts.Items.Insert(0, page_setup);
ts.Items[0].Click += new System.EventHandler(page_setup_Click);//添加对应按钮的单击是事件

ToolStripButton print_setup = new ToolStripButton("打印机设置");
Bitmap back_bmp = imageList1.Images[0] as Bitmap;//调用资源文件图片
print_setup.Image = back_bmp;
print_setup.DisplayStyle = ToolStripItemDisplayStyle.Image;
print_setup.Size = new Size(23, 22);
print_setup.ImageTransparentColor = System.Drawing.Color.Magenta;
ts.Items.Insert(1, print_setup);
ts.Items[1].Click += new System.EventHandler(print_setup_Click);//添加对应按钮的单击是事件

ToolStripButton upPage = new ToolStripButton("上一页");
back_bmp = imageList1.Images[1] as Bitmap;//调用资源文件图片
upPage.Image = back_bmp;
upPage.DisplayStyle = ToolStripItemDisplayStyle.Image;
upPage.Size = new Size(23, 22);
upPage.ImageTransparentColor = System.Drawing.Color.White;
ts.Items.Insert(2, upPage);
ts.Items[2].Click += new System.EventHandler(UpPage_Click);//添加对应按钮的单击是事件

ToolStripButton DownPage = new ToolStripButton("下一页");
back_bmp = imageList1.Images[2] as Bitmap;//调用资源文件图片
DownPage.Image = back_bmp;
DownPage.DisplayStyle = ToolStripItemDisplayStyle.Image;
DownPage.Size = new Size(23, 22);
DownPage.ImageTransparentColor = System.Drawing.Color.White;
ts.Items.Insert(3, DownPage);
ts.Items[3].Click += new System.EventHandler(my_setup_Click);//添加对应按钮的单击是事件

ToolStripButton my_setup = new ToolStripButton("打印");
my_setup.Size = new Size(23, 22);
my_setup.ImageTransparentColor = System.Drawing.Color.Magenta;
ts.Items.Insert(4, my_setup);
ts.Items[4].Click += new System.EventHandler(my_setup_Click);//添加对应按钮的单击是事件
}
}
}
private void my_setup_Click(object sender, EventArgs e)
{
is_Print = true;//说明是正式打印,用于通知printDocument1_PrintPage哪些打印,哪些只是预览
Document.Print();
}

//上一页
private void UpPage_Click(object sender, EventArgs e)
{
//base.PrintPreviewControl.DrawToBitmap(
base.PrintPreviewControl.StartPage++;
//Bitmap newbitmap = new Bitmap(Document.DefaultPageSettings.PaperSize.Width, Document.DefaultPageSettings.PaperSize.Height);
//base.PrintPreviewControl.DrawToBitmap(newbitmap, new Rectangle(0, 0, newbitmap.Width, newbitmap.Height));
//newbitmap.Save(@"F:\yl.jpg");


}
//上一页
private void DownPage_Click(object sender, EventArgs e)
{

}
private void page_setup_Click(object sender, EventArgs e)
{
pageSetupDialog1.Document = base.Document;
if (pageSetupDialog1.ShowDialog() == DialogResult.OK)
{
is_Print = false;
this.PrintPreviewControl.InvalidatePreview();//刷新预览内容
}
}

private void print_setup_Click(object sender, EventArgs e)
{

using (PrintDialog diag = new PrintDialog())
{
diag.Document = base.Document;//base 关键字用于从派生类中访问基类的成员:调用基类上已被其他方法重写的方法。
if (diag.ShowDialog() == DialogResult.OK)
{
is_Print = false;
this.PrintPreviewControl.InvalidatePreview();//刷新预览内容
}
}


}

public void Del()
{
int bnt = 0;
foreach (Control c in Controls)
{
if (c.GetType() == typeof(ToolStrip))
{
ToolStrip ts = c as ToolStrip;
ts.Items.RemoveAt(bnt);
}
}
}

private void Form2_Load(object sender, EventArgs e)
{
//以下两行,虽然输入是语法提示中不存在,但不要管它
this.Text = "自定义打印预览中的按钮";
// this.Icon = my_printPreviewDialog.Properties.Resources.print_driver;

}


}
}

=======

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

namespace ModifyPrintView
{
public partial class my_printPreviewDialog : Form
{
public my_printPreviewDialog()
{
InitializeComponent();
}

Form2 f2 = null;
private void button1_Click(object sender, EventArgs e)
{
f2 = new Form2();
f2.Del();//删除原来的打印按钮
f2.Add();//加入自定义的按钮
f2.isPrint = false;//给自定义预览类中的打印标志赋值,告诉printDocument1_PrintPage只是预览
f2.Document = printDocument1;
f2.ShowDialog();

}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("天津冶金集团中兴盛达钢业有限公司", new Font("Arial", 5), Brushes.Black, 10, 10);
e.Graphics.DrawString("信息网络中心", new Font("Arial", 5), Brushes.Black, 10, 20);

if (f2.isPrint != true)//判断自定义预览类中的打印标志,是否是输出至打印机
{
e.Graphics.DrawString("信息网络中心111111111111111", new Font("Arial", 5), Brushes.Black, 10, 20);
//e.Graphics.DrawImage(new Bitmap(my_printPreviewDialog.p.Resources.print_driver1), 25, 25);
}

}
}
}

posted on 2015-10-14 08:45  gisai  阅读(224)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3