public partial class Print : Form
{
// 定義dgSetPage托付進(jìn)行打印時(shí)的選項(xiàng)設(shè)置
public delegate void dgSetPage();
//定義dgFiledelete 托付進(jìn)行打印完畢后。刪除填充后的模板文件
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方法內(nèi)使用的鼠標(biāo)單擊 常量
const int BM_CLICK = 0xF5;
private void btnPrint_Click(object sender, EventArgs e)
{
btnPrint.Enabled = false;
//由于是使用WebBrowser對(duì)象進(jìn)行打印HTML文件,所以無(wú)法控制頁(yè)面設(shè)置,須要使用注冊(cè)表改動(dòng)一些內(nèi)容
//方法 ChangePageSettingByRegist 。改動(dòng)注冊(cè)表,取消頁(yè)眉、頁(yè)角項(xiàng)目
ChangePageSettingByRegist();
//讀取數(shù)據(jù)填充HTML模板
string sFillDataResult=FillDataToNewFile();
if (sFillDataResult.IndexOf("NG") >= 0)
{
MessageBox.Show(sFillDataResult);
return;
}
pd_PrintPage();
btnPrint.Enabled = true;
}
private void pd_PrintPage()
{
//創(chuàng)建一個(gè)WebBrowser對(duì)象,然后用它在后臺(tái)打開(kāi)并解釋HTML文件
WebBrowser webBrowserForPrinting = new WebBrowser();
// lblNewFile.Text 保存是填充后的模板文件名稱(chēng)
webBrowserForPrinting.Url = new Uri(Application.StartupPath.ToString() + "\\" + lblNewFile.Text);
//當(dāng)載入文件完畢后激發(fā)手動(dòng)追加的事件
webBrowserForPrinting.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PrintDocument);
webBrowserForPrinting.Focus();
}
private void PrintDocument(object sender,WebBrowserDocumentCompletedEventArgs e)
{
//創(chuàng)建一個(gè)新的線(xiàn)程,用于當(dāng)彈出頁(yè)面設(shè)置對(duì)話(huà)框時(shí)發(fā)送設(shè)置為橫向的指令
Thread th = new Thread(new ThreadStart(new dgSetPage(SetPage)));
th.Start();
//彈出頁(yè)面設(shè)置對(duì)話(huà)框-----即上方定義的新進(jìn)程須要處理的窗體
((WebBrowser)sender).ShowPageSetupDialog();
//打印
((WebBrowser)sender).Print();
//釋放資源
((WebBrowser)sender).Dispose();
//刪除填充后的模板文件,防止反復(fù)打印
System.IO.File.delete(lblNewFile.Text);
lblNewFile.Text = "";
}
// 此處方式被凝視掉了,由于假設(shè)須要改動(dòng)主窗體內(nèi)控件的內(nèi)容或者狀況里須要使用托付調(diào)用改動(dòng)方法
// 須要時(shí),把SetPage與SetPage2名稱(chēng)互換就可以在變更后的名稱(chēng)為SetPage2內(nèi)加入改動(dòng)主窗體內(nèi)容的代碼
//private void SetPage2()
//{
// //新建托付對(duì)象
// MethodInvoker In = new MethodInvoker(SetPage2);
// this.BeginInvoke(In);
//}
private void SetPage()
{
int i = 0;
// 此處須要設(shè)置循環(huán)查找時(shí)間。眼下為超過(guò)10秒未找到結(jié)束查找
while (true)
{
IntPtr WindownHand = FindWindow("#32770", "頁(yè)面設(shè)置");
if (WindownHand != IntPtr.Zero)
{
//找到窗體內(nèi)子項(xiàng)目的名柄,然后模擬鼠標(biāo)左鍵單擊事件
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++;
}
}
}
//改動(dòng)注冊(cè)表,把頁(yè)眉頁(yè)腳拿掉。然后設(shè)置為縮放至一頁(yè)
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模板內(nèi)容片段
<%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 編輯過(guò)