The C # C/S program uses HTML files as the print template.

Source: Internet
Author: User

The C # C/S program uses HTML files as the print template.

The C # C/S program uses HTML files as the print template.

I found a bunch of information on the Internet, sorted it out, and tried to change it slowly. Ah, I finally succeeded. Ha, the cainiao cannot afford to hurt.

Public partial class Print: Form
{

// Define the option settings when the dgSetPage delegate is printed
Public delegate void dgSetPage ();

// Define the dgFileDelete delegate. After printing is complete, delete the filled template file.
Public delegate void dgFileDelete ();

[DllImport ("User32.dll", EntryPoint = "FindWindow")]
Private static extern IntPtr FindWindow (string lpClassName, string lpWindowName );
[DllImport ("User32.dll", EntryPoint = "find1_wex")]
Private static extern IntPtr find1_wex (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 );

// Defines the constant used by the mouse in the SendMessage Method

Const int BM_CLICK = 0xF5;

Private void btnPrint_Click (object sender, EventArgs e)
{
BtnPrint. Enabled = false;

// Because the WebBrowser object is used to print HTML files, you cannot control page settings. You need to use the registry to modify some content.

// Method ChangePageSettingByRegist, modify the registry, and cancel the header and page view items
ChangePageSettingByRegist ();

// Read data to fill in the HTML Template
String sFillDataResult = FillDataToNewFile ();


If (sFillDataResult. IndexOf ("NG")> = 0)
{
MessageBox. Show (sFillDataResult );
Return;
}

Pd_PrintPage ();
BtnPrint. Enabled = true;
}

Private void pd_PrintPage ()
{
// Create a WebBrowser object and use it to open and interpret HTML files in the background
WebBrowser webBrowserForPrinting = new WebBrowser ();

// LblNewFile. Text is the filled template file name.
WebBrowserForPrinting. Url = new Uri (Application. StartupPath. ToString () + "\" + lblNewFile. Text );

// Trigger the manual append event after the file is loaded
WebBrowserForPrinting. DocumentCompleted + = new WebBrowserDocumentCompletedEventHandler (PrintDocument );
WebBrowserForPrinting. Focus ();
}


Private void PrintDocument (object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Create a new thread for sending commands set to horizontal when the page Setting dialog box is displayed
Thread th = new Thread (new ThreadStart (new dgSetPage (SetPage )));
Th. Start ();

// The page Setting dialog box is displayed. That is, the window for processing the new process defined above.
(WebBrowser) sender). ShowPageSetupDialog ();

// Print
(WebBrowser) sender). Print ();

// Release resources
(WebBrowser) sender). Dispose ();

// Delete the filled template file to prevent repeated Printing
System. IO. File. Delete (lblNewFile. Text );
LblNewFile. Text = "";
}

// The method is commented out here, because if you need to modify the content or status of the control in the main window, you need to use the delegate to call the modification method.

// If necessary, you can change the SetPage and SetPage2 names to add the code for modifying the content of the main window in the changed name SetPage2.
// Private void SetPage2 ()
//{
/// Create a delegate object
// MethodInvoker In = new MethodInvoker (SetPage2 );
// This. BeginInvoke (In );
//}

Private void SetPage ()
{
Int I = 0;

// You need to set the cyclic search time here, which is not found in more than 10 seconds.
While (true)
{
IntPtr WindownHand = FindWindow ("#32770", "page settings ");
If (WindownHand! = IntPtr. Zero)
{

// Locate the Sub-item name handle in the window, and then simulate the left-click event
IntPtr Wk = find1_wex (WindownHand, IntPtr. Zero, null, "horizontal (& )");
SendMessage (Wk, BM_CLICK, 0, 0 );

IntPtr Wk1 = find1_wex (WindownHand, IntPtr. Zero, null, "OK ");
SendMessage (Wk1, BM_CLICK, 0, 0 );


Break;
}
Else
{
If (I> 20)
Break;
Thread. Sleep (500 );
I ++;
}
}

}

// Modify the registry, remove the header and footer, and set it to zoom to one page.
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", );
Software. SetValue ("footer", );
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 template content snippets

<% A %> placeholder for replacement













Machine Protocol
Purchase Order No: <% OrderNo %> Unit No: <% UnitNo %> Region: <% Region %> Assembly date: <% AssemblyDate %>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.