C#中代碼Dialog控制項實現(開啟,儲存,改變字型,顏色,列印功能)

來源:互聯網
上載者:User
列印|控制項

開啟:

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);
} }
}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.