public partial class Print : Form
{
// 定义dgSetPage托付进行打印时的选项设置
public delegate void dgSetPage();
//定义dgFiledelete 托付进行打印完毕后。删除填充后的模板文件
public delegate void dgFiledelete();
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
//定义SendMessage方法内使用的鼠标单击 常量
const int BM_CLICK = 0xF5;
private void btnPrint_Click(object sender, EventArgs e)
{
btnPrint.Enabled = false;
//由于是使用WebBrowser对象进行打印HTML文件,所以无法控制页面设置,须要使用注冊表改动一些内容
//方法 ChangePageSettingByRegist 。改动注冊表,取消页眉、页角项目
ChangePageSettingByRegist();
//读取数据填充HTML模板
string sFillDataResult=FillDataToNewFile();
if (sFillDataResult.IndexOf("NG") >= 0)
{
MessageBox.Show(sFillDataResult);
return;
}
pd_PrintPage();
btnPrint.Enabled = true;
}
private void pd_PrintPage()
{
//创建一个WebBrowser对象,然后用它在后台打开并解释HTML文件
WebBrowser webBrowserForPrinting = new WebBrowser();
// lblNewFile.Text 保存是填充后的模板文件名称
webBrowserForPrinting.Url = new Uri(Application.StartupPath.ToString() + "\\" + lblNewFile.Text);
//当载入文件完毕后激发手动追加的事件
webBrowserForPrinting.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PrintDocument);
webBrowserForPrinting.Focus();
}
private void PrintDocument(object sender,WebBrowserDocumentCompletedEventArgs e)
{
//创建一个新的线程,用于当弹出页面设置对话框时发送设置为横向的指令
Thread th = new Thread(new ThreadStart(new dgSetPage(SetPage)));
th.Start();
//弹出页面设置对话框-----即上方定义的新进程须要处理的窗体
((WebBrowser)sender).ShowPageSetupDialog();
//打印
((WebBrowser)sender).Print();
//释放资源
((WebBrowser)sender).Dispose();
//删除填充后的模板文件,防止反复打印
System.IO.File.delete(lblNewFile.Text);
lblNewFile.Text = "";
}
// 此处方式被凝视掉了,由于假设须要改动主窗体内控件的内容或者状况里须要使用托付调用改动方法
// 须要时,把SetPage与SetPage2名称互换就可以在变更后的名称为SetPage2内加入改动主窗体内容的代码
//private void SetPage2()
//{
// //新建托付对象
// MethodInvoker In = new MethodInvoker(SetPage2);
// this.BeginInvoke(In);
//}
private void SetPage()
{
int i = 0;
// 此处须要设置循环查找时间。眼下为超过10秒未找到结束查找
while (true)
{
IntPtr WindownHand = FindWindow("#32770", "页面设置");
if (WindownHand != IntPtr.Zero)
{
//找到窗体内子项目的名柄,然后模拟鼠标左键单击事件
IntPtr Wk = FindWindowEx(WindownHand, IntPtr.Zero, null, "横向(&A)");
SendMessage(Wk, BM_CLICK, 0, 0);
IntPtr Wk1 = FindWindowEx(WindownHand, IntPtr.Zero, null, "确定");
SendMessage(Wk1, BM_CLICK, 0, 0);
break;
}
else
{
if (i > 20)
break;
Thread.Sleep(500);
i++;
}
}
}
//改动注冊表,把页眉页脚拿掉。然后设置为缩放至一页
private void ChangePageSettingByRegist()
{
RegistryKey hklm = Registry.CurrentUser;
RegistryKey software = hklm.OpenSubKey(@"Software\Microsoft\Internet Explorer\PageSetup".ToUpper(), true);
object A = (object)"";
object B = (object)"0.5";
object C = (object)"0";
software.SetValue("header", A);
software.SetValue("footer", A);
software.SetValue("margin_bottom", B);
software.SetValue("margin_left", C);
software.SetValue("margin_right", C);
software.SetValue("margin_top", C);
software.SetValue("Shrink_To_Fit","yes");
}
HTML模板内容片段
<%A%> 占位符,用于替换
<table cellpadding="0" cellspacing="0" border="0px">
<tr><td colspan="8" class="EachPartSpace"></td></tr>
<tr><td class="LayOutFieldTitle" colspan="8">Machine Protocol</td></tr>
<tr>
<td class="FieldNameHorizontal Column1With">Purchase Order No:</td>
<td class="FieldValueHorizontal Column2With"><%OrderNo%></td>
<td class="FieldNameHorizontal Column1With">Unit No:</td>
<td class="FieldValueHorizontal Column2With"><%UnitNo%></td>
<td class="FieldNameHorizontal Column1With">Region:</td>
<td class="FieldValueHorizontal Column6With"><%Region%></td>
<td class="FieldNameHorizontal Column2With">Assembly date:</td>
<td class="FieldValueHorizontal Column2With"><%AssemblyDate%></td>
</tr>
</table>
该文章在 2023/8/11 17:49:26 编辑过