C#-WinForm-Print Control

C#-WinForm-Print Control
        </h1>
        <div class="clear"></div>
        <div class="postBody">

Print control

Draw the following form

1. PrintDocument-the basis of printing First place the PrintDocument control, double-click the event PrintPage to set the style to be printed (Li Xiance lxc)

  

Copy code
//The first step is to set the printing properties 
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
        { 
            //Set the font style 
            Font f = new System.Drawing.Font("Microsoft Yahei ", 16); 
            f = richTextBox1.Font; 
            //Set the font color 
            Brush b = new SolidBrush(richTextBox1.ForeColor);
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">e.绘制.画字符串(要打印的文本,文本格式,画刷-颜色和纹理,位置坐标)</span>
        e.Graphics.DrawString(richTextBox1.Text, f, b, <span style="color: rgba(128, 0, 128, 1)">20</span>, <span style="color: rgba(128, 0, 128, 1)">10</span><span style="color: rgba(0, 0, 0, 1)">);
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">字体样式设置</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> button4_Click(<span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> sender, EventArgs e)
    {
        DialogResult dr </span>=<span style="color: rgba(0, 0, 0, 1)"> fontDialog1.ShowDialog();
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (dr ==<span style="color: rgba(0, 0, 0, 1)"> DialogResult.OK)
        {
            richTextBox1.Font </span>=<span style="color: rgba(0, 0, 0, 1)"> fontDialog1.Font;
            richTextBox1.ForeColor </span>=<span style="color: rgba(0, 0, 0, 1)"> fontDialog1.Color;
        }
    }</span></pre>
Copy code
Set up Document

Two, PageSetupDialog-print page settings

Copy code
//The second step is to set the print page settings 
        private void button1_Click(object sender, EventArgs e) 
        { 
            pageSetupDialog1.Document = printDocument1; 
            pageSetupDialog1.ShowDialog(); 
        }
Copy code
Print page setup

Three, 1. PrintPreviewContol-Print preview format one, set the preview area in the form

//The third step is to print and preview a 
        private void button2_Click(object sender, EventArgs e) 
        { 
            printPreviewControl1.Document = printDocument1; 
        }
Print preview one

2. PrintPreviewDialog-Print preview format two, preview in the pop-up window

Copy code
//The third step is to print preview two 
        private void button2_Click(object sender, EventArgs e) 
        { 
            printPreviewDialog1.Document = printDocument1; 
            printPreviewDialog1.ShowDialog(); 
        }
Copy code
Print preview two

Four, PrintDialog-start printing

Copy code
//The fourth step starts printing 
        private void button3_Click(object sender, EventArgs e) 
        { 
            printDialog1.Document = printDocument1; 
            DialogResult dr = printDialog1.ShowDialog(); 
            if (dr == DialogResult.OK) 
            { 
                printDocument1.Print(); 
            } 
        }
Copy code
Start printing

 

Guess you like

Origin blog.csdn.net/s_156/article/details/112966275