主页 > 编程资料 > C# >
发布时间:2015-09-26 作者:网络 阅读:265次

打开:

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

<> <>
关键字词: