The C # C/S program uses HTML files as print templates,

Source: Internet
Author: User

The C # C/S program uses HTML files as print templates,

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

<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>

 


A simple program of C language Bubble Sorting

Main ()
{
Int I, j, temp;
Int a [10];
For (I = 0; I <10; I ++)
Scanf ("% d,", & a [I]);
For (j = 0; j <= 9; j ++)
{For (I = 0; I <10-j; I ++)
If (a [I]> a [I + 1])
{Temp = a [I];
A [I] = a [I + 1];
A [I + 1] = temp ;}
}
For (I = 1; I <11; I ++)
Printf ("% 5d,", a [I]);
Printf ("\ n ");
}

--------------
Bubble Algorithm
Algorithm Analysis and Improvement of Bubble Sorting
The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.
The basic concepts of application exchange sorting include Bubble sorting and quick sorting.

Bubble Sorting

1. Sorting Method
Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.
(1) initial
R [1. n] is an unordered area.

(2) First scan
The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.

(3) second scan
Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
Finally, the sequential area R [1. n] can be obtained through n-1 scanning.
Note:
During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.

2. Bubble sorting process example
Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49

3. Sorting Algorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.

(2) specific algorithms
Void BubbleSort (SeqList R)
{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.
Int I, j;
Boolean exchange; // exchange flag
For (I = 1; I <G id = "1">

A simple program of C language Bubble Sorting

Main ()
{
Int I, j, temp;
Int a [10];
For (I = 0; I <10; I ++)
Scanf ("% d,", & a [I]);
For (j = 0; j <= 9; j ++)
{For (I = 0; I <10-j; I ++)
If (a [I]> a [I + 1])
{Temp = a [I];
A [I] = a [I + 1];
A [I + 1] = temp ;}
}
For (I = 1; I <11; I ++)
Printf ("% 5d,", a [I]);
Printf ("\ n ");
}

--------------
Bubble Algorithm
Algorithm Analysis and Improvement of Bubble Sorting
The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.
The basic concepts of application exchange sorting include Bubble sorting and quick sorting.

Bubble Sorting

1. Sorting Method
Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.
(1) initial
R [1. n] is an unordered area.

(2) First scan
The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.

(3) second scan
Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
Finally, the sequential area R [1. n] can be obtained through n-1 scanning.
Note:
During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.

2. Bubble sorting process example
Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49

3. Sorting Algorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.

(2) specific algorithms
Void BubbleSort (SeqList R)
{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.
Int I, j;
Boolean exchange; // exchange flag
For (I = 1; I <G id = "1">

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.