首页 > 开发 > 综合 > 正文

C#中代码Dialog控件实现(打开,保存,改变字体,颜色,打印功能)

2024-07-21 02:26:01
字体:
来源:转载
供稿:网友

打开:

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



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表