国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

C#中代碼Dialog控件實(shí)現(xiàn)(打開(kāi),保存,改變字體,顏色,打印功能)

2024-07-21 02:26:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

打開(kāi):

private void openfiledialogbtn_click(object sender, system.eventargs e){
openfiledialog openfiledialog=new openfiledialog();
openfiledialog.initialdirectory="c://";//注意這里寫路徑時(shí)要用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;//將當(dāng)前選定的文字改變字體
} }

改變字體顏色:

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;//初始化當(dāng)前文本框中的字體顏色,當(dāng)用戶在colordialog對(duì)話框中點(diǎn)擊"取消"按鈕
file://恢復(fù)原來(lái)的值
colordialog.showdialog();
richtextbox1.selectioncolor=colordialog.color;
}

打印:

private void printdocument_printpage(object sender, system.drawing.printing.printpageeventargs e)
{ float linesperpage=0;//頁(yè)面的行號(hào)
float ypos=0;//打印字符串的縱向位置
int count=0;//行計(jì)數(shù)器
float leftmargin =e.marginbounds.left;//左邊距
float topmargin=e.marginbounds.top;//上邊距
string line=null;//行字符串
color clr=richtextbox1.selectioncolor;//當(dāng)前的打印顏色,在我這個(gè)程序沒(méi)有實(shí)現(xiàn)不同顏色打印
solidbrush b =new solidbrush(clr);//刷子
fnt=richtextbox1.selectionfont;//當(dāng)前的打印字體
linesperpage=e.marginbounds.height/fnt.getheight(e.graphics);//每頁(yè)可打印的行數(shù)
file://逐行循行打印一頁(yè)
while(count {
ypos=topmargin+(count*fnt.getheight(e.graphics));
e.graphics.drawstring(line,fnt,b,leftmargin,ypos,new stringformat());
count++;
} file://如果該頁(yè)打印完成而line不為空說(shuō)明還有沒(méi)完成的頁(yè)面,發(fā)出下一次的打印事件,
file://在下一次的打印中l(wèi)inereader會(huì)自動(dòng)讀取上次沒(méi)有打印完的內(nèi)容。linereader可以記錄當(dāng)前讀取的位置
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, "打印出錯(cuò)", 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);
} }
}



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南召县| 贺州市| 汉寿县| 依兰县| 察哈| 安陆市| 广宗县| 萨迦县| 新平| 长岛县| 德化县| 横峰县| 万盛区| 云浮市| 绥阳县| 沂水县| 宝应县| 昂仁县| 锡林浩特市| 墨江| 昌图县| 凤城市| 全南县| 梧州市| 林西县| 芜湖县| 镇江市| 封丘县| 施秉县| 黄梅县| 奉化市| 恭城| 宝丰县| 噶尔县| 磴口县| 诸暨市| 东乡族自治县| 漳浦县| 门源| 遵化市| 新和县|