C# C/S程序使用HTML文件作為打印模板
在網上找了一堆的資料,整理到郁悶呀,慢慢試慢慢改,哎,終於成功了,哈,菜鳥傷不起呀
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%> 占位符,用於替換
Machine Protocol
Purchase Order No:
<%OrderNo%>
Unit No:
<%UnitNo%>
Region:
<%Region%>
Assembly date:
<%AssemblyDate%>