列印|控制項
開啟:
private void openFileDialogBTN_Click(object sender, System.EventArgs e){
OpenFileDialog openFileDialog=new OpenFileDialog();
openFileDialog.InitialDirectory="c:\\";//注意這裡寫路徑時要用c:\\而不是c:openFileDialog.Filter="文字檔|*.*|C#檔案|*.cs|所有檔案|*.*";
openFileDialog.RestoreDirectory=true;
openFileDialog.FilterIndex=1;
if (openFileDialog.ShowDialog()==DialogResult.OK)
{ fName=openFileDialog.FileName;
File fileOpen=new File(fName);
isFileHaveName=true;
richTextBox1.Text=fileOpen.ReadFile();
richTextBox1.AppendText("");
} }
儲存:
private void saveAsDialogBTN_Click(object sender, System.EventArgs e)
{ SaveFileDialog saveFileDialog=new SaveFileDialog();
saveFileDialog.Filter="文字檔|*.*|C#檔案|*.cs|所有檔案|*.*";
saveFileDialog.FilterIndex=2;
saveFileDialog.RestoreDirectory=true;
if(saveFileDialog.ShowDialog()==DialogResult.OK)
{ if(saveFileDialog.ShowDialog()==DialogResult.OK)
{ fName=saveFileDialog.FileName;
File fSaveAs=new File(fName);
isFileHaveName=true; file://儲存的檔案有名字
fSaveAs.WriteFile(richTextBox1.Text);
} }
}
改變字型大小
private void fontDialogBTN_Click(object sender, System.EventArgs e)
{ FontDialog fontDialog=new FontDialog();
fontDialog.Color=richTextBox1.ForeColor;
fontDialog.AllowScriptChange=true;
fontDialog.ShowColor=true;
if(fontDialog.ShowDialog()!=DialogResult.Cancel)
{ richTextBox1.SelectionFont=fontDialog.Font;//將當前選定的文字改變字型
} }
改變字型顏色:
private void colorDialogBTN_Click(object sender, System.EventArgs e)
{ ColorDialog colorDialog=new ColorDialog();
colorDialog.AllowFullOpen=true;
colorDialog.FullOpen=true;
colorDialog.ShowHelp=true;
colorDialog.Color=Color.Black;//初始化當前文字框中的字型顏色,當使用者在ColorDialog對話方塊中點擊"取消"按鈕
file://恢複原來的值
colorDialog.ShowDialog();
richTextBox1.SelectionColor=colorDialog.Color;
}
列印:
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{ float linesPerPage=0;//頁面的行號
float yPos=0;//列印字串的縱向位置
int count=0;//行計數器
float leftMargin =e.MarginBounds.Left;//左邊距
float topMargin=e.MarginBounds.Top;//上邊距
string line=null;//行字串
Color clr=richTextBox1.SelectionColor;//當前的列印顏色,在我這個程式沒有實現不同顏色列印
SolidBrush b =new SolidBrush(clr);//刷子
fnt=richTextBox1.SelectionFont;//當前的列印字型
linesPerPage=e.MarginBounds.Height/fnt.GetHeight(e.Graphics);//每頁可列印的行數
file://逐行循行列印一頁
while(count {
yPos=topMargin+(count*fnt.GetHeight(e.Graphics));
e.Graphics.DrawString(line,fnt,b,leftMargin,yPos,new StringFormat());
count++;
} file://如果該頁列印完成而line不為空白說明還有沒完成的頁面,發出下一次的列印事件,
file://在下一次的列印中lineReader會自動讀取上次沒有列印完的內容。lineReader可以記錄當前讀取的位置
if(line!=null)
e.HasMorePages=true;
else
e.HasMorePages=false;
}
private void printPreviewBTN_Click(object sender, System.EventArgs e)
{ lineReader = new StringReader(richTextBox1.Text);
try
{ PrintPreviewDialog printPreviewDialog1=new PrintPreviewDialog();
printPreviewDialog1.Document=printDocument;
printPreviewDialog1.FormBorderStyle=FormBorderStyle.Fixed3D;
printPreviewDialog1.ShowDialog(this);
} catch(Exception excep)
{ MessageBox.Show(excep.Message, "列印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
} }
private void printDialogBTN_Click(object sender, System.EventArgs e)
{ PrintDialog printDialog=new PrintDialog();
printDialog.Document=printDocument;
if(printDialog.ShowDialog()!=DialogResult.Cancel)
{ try
{ printDocument.Print();
} catch(Exception ex)
{ MessageBox.Show(ex.Message);
} }
}