C #/S programs use HTML files as print templates

Source: Internet
Author: User

C #/S programs use HTML files as print templates

On the internet to find a bunch of information, sorting to depressed ah, slowly try to change slowly. Ah, the final success, HA, rookie hurt Ah

public partial class Print:form
{

Define option settings when Dgsetpage is entrusted for printing
public delegate void Dgsetpage ();

//define Dgfiledelete entrusted to print after completion. Delete a populated template file
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 Lpwindowna ME);

[DllImport ("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage (IntPtr hWnd, int msg, int wParam, int lParam);

Define the mouse click constants used within the SendMessage method

const int bm_click = 0XF5;

private void Btnprint_click (object sender, EventArgs e)
{
btnprint.enabled = false;

Because the HTML file is printed using the WebBrowser object, you cannot control the page setup and need to use the registration form to change some content

Method Changepagesettingbyregist. Change the list, cancel the header, page corner items
Changepagesettingbyregist ();

//Read data populate 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 the HTML file in the background
WebBrowser webbrowserforprinting = new WebBrowser ();

Lblnewfile.text Save is the template file name after the fill
Webbrowserforprinting.url = new Uri (Application.StartupPath.ToString () + "\ \" + Lblnewfile.text);

            // Fires a manually appended event when the file is loaded
            webbrowserforprinting.documentcompleted +=   New Webbrowserdocumentcompletedeventhandler (PrintDocument) ;
            Webbrowserforprinting.focus ();
       }


private void PrintDocument (Object Sender,webbrowserdocumentcompletedeventargs E)
{
//Create a new thread to send instructions that are set to landscape when the Page Setup dialog box pops up
Thread th = new Thread (new ThreadStart (New Dgsetpage (Setpage)));
Th. Start ();

//Popup Page Setup dialog box-----The form that the new process defined above needs to process
((WebBrowser) sender). Showpagesetupdialog ();

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

Freeing resources
((WebBrowser) sender). Dispose ();

//Delete the populated template file to prevent repeated printing
System.IO.File.Delete (Lblnewfile.text);
Lblnewfile.text = "";
}

This way is stared out, due to the assumption that you need to change the contents of the main form internal control or the situation need to use the commit call change method

When required, the Setpage and SetPage2 names can be exchanged to change the name of the SetPage2 to add the main form of the content of the code
private void SetPage2 ()
//{
//// new entrusted Object
MethodInvoker in = new MethodInvoker (SetPage2);
This. BeginInvoke (in);
//}

private void Setpage ()
{
int i = 0;

//You need to set the cycle lookup time here. Now for more than 10 seconds no end lookup found
while (true)
{
IntPtr Windownhand = FindWindow ("#32770", "Page Setup");
if (windownhand! = IntPtr.Zero)
{

//Find the name of the child item in the window, and then simulate the left mouse button click event
IntPtr Wk = FindWindowEx (Windownhand, IntPtr.Zero, NULL, "Landscape (&a)");
SendMessage (Wk, Bm_click, 0, 0);

IntPtr Wk1 = FindWindowEx (Windownhand, IntPtr.Zero, null, "OK");
SendMessage (WK1, Bm_click, 0, 0);


Break
}
Else
{
if (i > 20)
Break
Thread.Sleep (500);
i++;
}
}

}

//Change the registration form and remove the header and footer. Then set 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", 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 Template content Fragment

<%A%> placeholders for replacing

<table cellpadding= "0" cellspacing= "0" border= "0px" >
&LT;TR&GT;&LT;TD colspan= "8" class= "Eachpartspace" ></td></tr>
&LT;TR&GT;&LT;TD class= "Layoutfieldtitle" colspan= "8" >machine protocol</td></tr>
<tr>
&LT;TD class= "Fieldnamehorizontal column1with" >purchase Order no:</td>
&LT;TD class= "Fieldvaluehorizontal column2with"><%orderno%></Td>
&LT;TD class= "Fieldnamehorizontal column1with" >unit no:</td>
&LT;TD class= "Fieldvaluehorizontal column2with"><%unitno%></Td>
&LT;TD class= "Fieldnamehorizontal column1with" >Region:</td>
&LT;TD class= "Fieldvaluehorizontal column6with"><%region%></Td>
&LT;TD class= "Fieldnamehorizontal column2with" >assembly date:</td>
&LT;TD class= "Fieldvaluehorizontal column2with"><%assemblydate%></Td>
</tr>

C #/S programs use HTML files as print templates

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.