110,538
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
namespace njtnpgd
{
public partial class frm_print : Form
{
Form1 f1 = new Form1();
public frm_print(Form1 _f1)
{
f1 = _f1;
InitializeComponent();
}
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
Bitmap memoryImage = null;
private void frm_print_Load(object sender, EventArgs e)
{
//测试不成功,注视掉,只能打印一个工单
//if (f1.dataGridView1.SelectedRows.Count > 1)
//{
// for (int i = 0; i < f1.dataGridView1.SelectedRows.Count; i++)
// {
// this.panelc.Height = this.panel1.Height*(i+1);
// Panel paneli=new Panel();
// paneli.BackColor = System.Drawing.Color.White;
// paneli.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
// paneli.BackgroundImage = global::njtnpgd.Properties.Resources.pgd_table;
// paneli.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
// paneli.Width = panel1.Width;
// paneli.Height = panel1.Height;
// paneli.Left = panel1.Left;
// paneli.Top = panel1.Top + panel1.Height*(i+1);
// //绘制单号
// TextBox tb_uid = new TextBox();
// tb_uid.BorderStyle = BorderStyle.None;
// tb_uid.Text = f1.dataGridView1.SelectedRows[i].Cells["pgdid"].Value.ToString();
// tb_uid.Width = 242; tb_uid.Height = 14; tb_uid.Left = paneli.Left + 87; tb_uid.Top = paneli.Top + 69;
// //绘制申报日期
// TextBox tb_pgd_addtime = new TextBox();
// tb_pgd_addtime.BorderStyle = BorderStyle.None;
// tb_pgd_addtime.Text = f1.dataGridView1.SelectedRows[i].Cells["pgd_addtime"].Value.ToString();
// tb_pgd_addtime.Width = 235; tb_pgd_addtime.Height = 14; tb_pgd_addtime.Left = paneli.Left + 433; tb_pgd_addtime.Top = paneli.Top + 69;
// //绘制用户姓名控件
// TextBox tb_uname = new TextBox();
// tb_uname.BorderStyle = BorderStyle.None;
// tb_uname.Text = f1.dataGridView1.SelectedRows[i].Cells["uname"].Value.ToString();
// tb_uname.Width = 92; tb_uname.Height = 14; tb_uname.Left = paneli.Left + 93; tb_uname.Top = paneli.Top + 101;
// //绘制用户联系电话控件
// TextBox tb_umobile = new TextBox();
// tb_umobile.BorderStyle = BorderStyle.None;
// tb_umobile.Text = f1.dataGridView1.SelectedRows[i].Cells["umobile"].Value.ToString();
// tb_umobile.Width = 93; tb_umobile.Height = 14; tb_umobile.Left = paneli.Left + 267; tb_umobile.Top = paneli.Top + 101;
// //绘制施工内容控件
// TextBox tb_proctype = new TextBox();
// tb_proctype.BorderStyle = BorderStyle.None;
// tb_proctype.Text = f1.dataGridView1.SelectedRows[i].Cells["pgdtype"].Value.ToString();
// tb_proctype.Width = 211; tb_proctype.Height = 14; tb_proctype.Left = paneli.Left + 443; tb_proctype.Top = paneli.Top + 101;
// //绘制用户住址控件
// TextBox tb_uaddress = new TextBox();
// tb_uaddress.BorderStyle = BorderStyle.None;
// tb_uaddress.Text = f1.dataGridView1.SelectedRows[i].Cells["uaddress"].Value.ToString();
// tb_uaddress.Width = 576; tb_uaddress.Height = 14; tb_uaddress.Left = paneli.Left + 93; tb_uaddress.Top = paneli.Top + 140;
// panelc.Controls.Add(tb_uid);
// panelc.Controls.Add(tb_pgd_addtime);
// panelc.Controls.Add(tb_uname);
// panelc.Controls.Add(tb_umobile);
// panelc.Controls.Add(tb_proctype);
// panelc.Controls.Add(tb_uaddress);
// this.panelc.Controls.Add(paneli);
// }
//}
//为控件赋值
if (f1.dataGridView1.SelectedRows.Count > 1)
{
MessageBox.Show("出错了!每次只能打印一个工单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
else
{
try
{
tb_uid.Text = f1.dataGridView1.SelectedRows[0].Cells["pgdid"].Value.ToString();
tb_pgd_addtime.Text = f1.dataGridView1.SelectedRows[0].Cells["pgd_addtime"].Value.ToString();
tb_uname.Text = f1.dataGridView1.SelectedRows[0].Cells["uname"].Value.ToString();
tb_umobile.Text = f1.dataGridView1.SelectedRows[0].Cells["umobile"].Value.ToString();
tb_proctype.Text = f1.dataGridView1.SelectedRows[0].Cells["pgdtype"].Value.ToString();
tb_uaddress.Text = f1.dataGridView1.SelectedRows[0].Cells["uaddress"].Value.ToString();
tb_pdr.Text = f1.dataGridView1.SelectedRows[0].Cells["pdr"].Value.ToString();
tb_sgry.Text = f1.dataGridView1.SelectedRows[0].Cells["shigongrenyuan"].Value.ToString();
tb_paidanshijian.Text = f1.dataGridView1.SelectedRows[0].Cells["paidanshijian"].Value.ToString();
}
catch
{
MessageBox.Show("出错了!请联系技术部处理该错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 10, 30);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Graphics graphic = panelc.CreateGraphics();
Size s = panelc.Size;
Bitmap memImage = new Bitmap(s.Width, s.Height, graphic);
Graphics memGraphic = Graphics.FromImage(memImage);
IntPtr dc1 = graphic.GetHdc();
IntPtr dc2 = memGraphic.GetHdc();
BitBlt(dc2, 0, 0, panelc.ClientRectangle.Width, panelc.ClientRectangle.Height,dc1, 0, 0, 13369376);
memoryImage = (Bitmap)memImage.Clone();
graphic.ReleaseHdc(dc1);
memGraphic.ReleaseHdc(dc2);
graphic.Dispose();
memGraphic.Dispose();
memImage.Dispose();
PrintPreviewDialog dlg = new PrintPreviewDialog();
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
private void toolStrip1_MouseEnter(object sender, EventArgs e)
{
label1.Focus();
}
}
}