Form3.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Text;
  4. using System.Collections;
  5. using System.ComponentModel;
  6. using System.Windows.Forms;
  7. namespace GDIPlusDemo
  8. {
  9. /// <summary>
  10. /// Form3 的摘要说明。
  11. /// </summary>
  12. public class Form3 : System.Windows.Forms.Form
  13. {
  14. private System.Windows.Forms.GroupBox groupBox1;
  15. private System.Windows.Forms.GroupBox groupBox2;
  16. private System.Windows.Forms.GroupBox groupBox3;
  17. private System.Windows.Forms.GroupBox groupBox4;
  18. private System.Windows.Forms.GroupBox groupBox5;
  19. private System.Windows.Forms.GroupBox groupBox6;
  20. private System.Windows.Forms.Label label1;
  21. //字体系列名称列表
  22. private System.Windows.Forms.ListBox FontlistBox;
  23. //五种字体风格的复选框
  24. private System.Windows.Forms.CheckBox FontStyle_Strikeout;
  25. private System.Windows.Forms.CheckBox FontStyle_Underline;
  26. private System.Windows.Forms.CheckBox FontStyle_Italic;
  27. private System.Windows.Forms.CheckBox FontStyle_Bold;
  28. private System.Windows.Forms.CheckBox FontStyle_Regular;
  29. //字体色彩ARGB对应的微调控件
  30. private System.Windows.Forms.NumericUpDown FontColor_A;
  31. private System.Windows.Forms.NumericUpDown FontColor_R;
  32. private System.Windows.Forms.NumericUpDown FontColor_G;
  33. private System.Windows.Forms.NumericUpDown FontColor_B;
  34. //字体大小单位的七个单选按钮
  35. private System.Windows.Forms.RadioButton FontUnit_Dot;
  36. private System.Windows.Forms.RadioButton FontUnit_Pixel;
  37. private System.Windows.Forms.RadioButton FontUnit_Inch;
  38. private System.Windows.Forms.RadioButton FontUnit_Millimeter;
  39. private System.Windows.Forms.RadioButton FontUnit_Display;
  40. private System.Windows.Forms.RadioButton FontUnit_World;
  41. private System.Windows.Forms.RadioButton FontUnit_Document;
  42. //字体大小对应的微调控件
  43. private System.Windows.Forms.NumericUpDown FontSize;
  44. //字体预览区域
  45. private System.Windows.Forms.Label FontPreview;
  46. /// <summary>
  47. /// 必需的设计器变量。
  48. /// </summary>
  49. private System.ComponentModel.Container components = null;
  50. public Form3()
  51. {
  52. //
  53. // Windows 窗体设计器支持所必需的
  54. //
  55. InitializeComponent();
  56. //
  57. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  58. //
  59. }
  60. /// <summary>
  61. /// 清理所有正在使用的资源。
  62. /// </summary>
  63. protected override void Dispose( bool disposing )
  64. {
  65. if( disposing )
  66. {
  67. if(components != null)
  68. {
  69. components.Dispose();
  70. }
  71. }
  72. base.Dispose( disposing );
  73. }
  74. #region Windows 窗体设计器生成的代码
  75. /// <summary>
  76. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  77. /// 此方法的内容。
  78. /// </summary>
  79. private void InitializeComponent()
  80. {
  81. this.groupBox1 = new System.Windows.Forms.GroupBox();
  82. this.FontStyle_Strikeout = new System.Windows.Forms.CheckBox();
  83. this.FontStyle_Underline = new System.Windows.Forms.CheckBox();
  84. this.FontStyle_Italic = new System.Windows.Forms.CheckBox();
  85. this.FontStyle_Bold = new System.Windows.Forms.CheckBox();
  86. this.FontStyle_Regular = new System.Windows.Forms.CheckBox();
  87. this.FontColor_R = new System.Windows.Forms.NumericUpDown();
  88. this.FontUnit_Dot = new System.Windows.Forms.RadioButton();
  89. this.FontColor_A = new System.Windows.Forms.NumericUpDown();
  90. this.FontlistBox = new System.Windows.Forms.ListBox();
  91. this.FontColor_G = new System.Windows.Forms.NumericUpDown();
  92. this.FontColor_B = new System.Windows.Forms.NumericUpDown();
  93. this.groupBox2 = new System.Windows.Forms.GroupBox();
  94. this.FontUnit_Pixel = new System.Windows.Forms.RadioButton();
  95. this.FontUnit_Inch = new System.Windows.Forms.RadioButton();
  96. this.FontUnit_Millimeter = new System.Windows.Forms.RadioButton();
  97. this.FontUnit_Display = new System.Windows.Forms.RadioButton();
  98. this.FontUnit_World = new System.Windows.Forms.RadioButton();
  99. this.FontUnit_Document = new System.Windows.Forms.RadioButton();
  100. this.groupBox3 = new System.Windows.Forms.GroupBox();
  101. this.groupBox4 = new System.Windows.Forms.GroupBox();
  102. this.groupBox5 = new System.Windows.Forms.GroupBox();
  103. this.label1 = new System.Windows.Forms.Label();
  104. this.groupBox6 = new System.Windows.Forms.GroupBox();
  105. this.FontSize = new System.Windows.Forms.NumericUpDown();
  106. this.FontPreview = new System.Windows.Forms.Label();
  107. this.groupBox1.SuspendLayout();
  108. ((System.ComponentModel.ISupportInitialize)(this.FontColor_R)).BeginInit();
  109. ((System.ComponentModel.ISupportInitialize)(this.FontColor_A)).BeginInit();
  110. ((System.ComponentModel.ISupportInitialize)(this.FontColor_G)).BeginInit();
  111. ((System.ComponentModel.ISupportInitialize)(this.FontColor_B)).BeginInit();
  112. this.groupBox3.SuspendLayout();
  113. this.groupBox5.SuspendLayout();
  114. ((System.ComponentModel.ISupportInitialize)(this.FontSize)).BeginInit();
  115. this.SuspendLayout();
  116. //
  117. // groupBox1
  118. //
  119. this.groupBox1.Controls.Add(this.FontStyle_Strikeout);
  120. this.groupBox1.Controls.Add(this.FontStyle_Underline);
  121. this.groupBox1.Controls.Add(this.FontStyle_Italic);
  122. this.groupBox1.Controls.Add(this.FontStyle_Bold);
  123. this.groupBox1.Controls.Add(this.FontStyle_Regular);
  124. this.groupBox1.Location = new System.Drawing.Point(160, 72);
  125. this.groupBox1.Name = "groupBox1";
  126. this.groupBox1.Size = new System.Drawing.Size(328, 48);
  127. this.groupBox1.TabIndex = 19;
  128. this.groupBox1.TabStop = false;
  129. this.groupBox1.Text = "设置字体风格";
  130. //
  131. // FontStyle_Strikeout
  132. //
  133. this.FontStyle_Strikeout.Location = new System.Drawing.Point(248, 16);
  134. this.FontStyle_Strikeout.Name = "FontStyle_Strikeout";
  135. this.FontStyle_Strikeout.Size = new System.Drawing.Size(64, 24);
  136. this.FontStyle_Strikeout.TabIndex = 4;
  137. this.FontStyle_Strikeout.Text = "强调线";
  138. this.FontStyle_Strikeout.CheckedChanged += new System.EventHandler(this.FontStyle_Strikeout_CheckedChanged);
  139. //
  140. // FontStyle_Underline
  141. //
  142. this.FontStyle_Underline.Location = new System.Drawing.Point(176, 16);
  143. this.FontStyle_Underline.Name = "FontStyle_Underline";
  144. this.FontStyle_Underline.Size = new System.Drawing.Size(64, 24);
  145. this.FontStyle_Underline.TabIndex = 3;
  146. this.FontStyle_Underline.Text = "下划线";
  147. this.FontStyle_Underline.CheckedChanged += new System.EventHandler(this.FontStyle_Underline_CheckedChanged);
  148. //
  149. // FontStyle_Italic
  150. //
  151. this.FontStyle_Italic.Location = new System.Drawing.Point(112, 16);
  152. this.FontStyle_Italic.Name = "FontStyle_Italic";
  153. this.FontStyle_Italic.Size = new System.Drawing.Size(48, 24);
  154. this.FontStyle_Italic.TabIndex = 2;
  155. this.FontStyle_Italic.Text = "倾斜";
  156. this.FontStyle_Italic.CheckedChanged += new System.EventHandler(this.FontStyle_Italic_CheckedChanged);
  157. //
  158. // FontStyle_Bold
  159. //
  160. this.FontStyle_Bold.Location = new System.Drawing.Point(56, 16);
  161. this.FontStyle_Bold.Name = "FontStyle_Bold";
  162. this.FontStyle_Bold.Size = new System.Drawing.Size(48, 24);
  163. this.FontStyle_Bold.TabIndex = 1;
  164. this.FontStyle_Bold.Text = "加粗";
  165. this.FontStyle_Bold.CheckedChanged += new System.EventHandler(this.FontStyle_Bold_CheckedChanged);
  166. //
  167. // FontStyle_Regular
  168. //
  169. this.FontStyle_Regular.Location = new System.Drawing.Point(8, 16);
  170. this.FontStyle_Regular.Name = "FontStyle_Regular";
  171. this.FontStyle_Regular.Size = new System.Drawing.Size(48, 24);
  172. this.FontStyle_Regular.TabIndex = 0;
  173. this.FontStyle_Regular.Text = "常规";
  174. this.FontStyle_Regular.CheckedChanged += new System.EventHandler(this.FontStyle_Regular_CheckedChanged);
  175. //
  176. // FontColor_R
  177. //
  178. this.FontColor_R.Location = new System.Drawing.Point(256, 40);
  179. this.FontColor_R.Name = "FontColor_R";
  180. this.FontColor_R.Size = new System.Drawing.Size(56, 21);
  181. this.FontColor_R.TabIndex = 15;
  182. this.FontColor_R.ValueChanged += new System.EventHandler(this.FontColor_R_ValueChanged);
  183. //
  184. // FontUnit_Dot
  185. //
  186. this.FontUnit_Dot.Location = new System.Drawing.Point(168, 144);
  187. this.FontUnit_Dot.Name = "FontUnit_Dot";
  188. this.FontUnit_Dot.Size = new System.Drawing.Size(52, 24);
  189. this.FontUnit_Dot.TabIndex = 12;
  190. this.FontUnit_Dot.Text = "点";
  191. this.FontUnit_Dot.CheckedChanged += new System.EventHandler(this.FontUnit_Dot_CheckedChanged);
  192. //
  193. // FontColor_A
  194. //
  195. this.FontColor_A.Location = new System.Drawing.Point(168, 40);
  196. this.FontColor_A.Name = "FontColor_A";
  197. this.FontColor_A.Size = new System.Drawing.Size(56, 21);
  198. this.FontColor_A.TabIndex = 13;
  199. this.FontColor_A.ValueChanged += new System.EventHandler(this.FontColor_A_ValueChanged);
  200. //
  201. // FontlistBox
  202. //
  203. this.FontlistBox.ItemHeight = 12;
  204. this.FontlistBox.Location = new System.Drawing.Point(8, 16);
  205. this.FontlistBox.Name = "FontlistBox";
  206. this.FontlistBox.Size = new System.Drawing.Size(136, 244);
  207. this.FontlistBox.TabIndex = 18;
  208. this.FontlistBox.SelectedIndexChanged += new System.EventHandler(this.FontlistBox_SelectedIndexChanged);
  209. //
  210. // FontColor_G
  211. //
  212. this.FontColor_G.Location = new System.Drawing.Point(336, 40);
  213. this.FontColor_G.Name = "FontColor_G";
  214. this.FontColor_G.Size = new System.Drawing.Size(56, 21);
  215. this.FontColor_G.TabIndex = 16;
  216. this.FontColor_G.ValueChanged += new System.EventHandler(this.FontColor_G_ValueChanged);
  217. //
  218. // FontColor_B
  219. //
  220. this.FontColor_B.Location = new System.Drawing.Point(400, 40);
  221. this.FontColor_B.Name = "FontColor_B";
  222. this.FontColor_B.Size = new System.Drawing.Size(56, 21);
  223. this.FontColor_B.TabIndex = 20;
  224. this.FontColor_B.ValueChanged += new System.EventHandler(this.FontColor_B_ValueChanged);
  225. //
  226. // groupBox2
  227. //
  228. this.groupBox2.Location = new System.Drawing.Point(160, 16);
  229. this.groupBox2.Name = "groupBox2";
  230. this.groupBox2.Size = new System.Drawing.Size(328, 56);
  231. this.groupBox2.TabIndex = 21;
  232. this.groupBox2.TabStop = false;
  233. this.groupBox2.Text = "设置字体色彩(ARGB)";
  234. //
  235. // FontUnit_Pixel
  236. //
  237. this.FontUnit_Pixel.Location = new System.Drawing.Point(320, 144);
  238. this.FontUnit_Pixel.Name = "FontUnit_Pixel";
  239. this.FontUnit_Pixel.Size = new System.Drawing.Size(52, 24);
  240. this.FontUnit_Pixel.TabIndex = 22;
  241. this.FontUnit_Pixel.Text = "像素";
  242. this.FontUnit_Pixel.CheckedChanged += new System.EventHandler(this.FontUnit_Pixel_CheckedChanged);
  243. //
  244. // FontUnit_Inch
  245. //
  246. this.FontUnit_Inch.Location = new System.Drawing.Point(240, 144);
  247. this.FontUnit_Inch.Name = "FontUnit_Inch";
  248. this.FontUnit_Inch.Size = new System.Drawing.Size(52, 24);
  249. this.FontUnit_Inch.TabIndex = 23;
  250. this.FontUnit_Inch.Text = "英寸";
  251. this.FontUnit_Inch.CheckedChanged += new System.EventHandler(this.FontUnit_Inch_CheckedChanged);
  252. //
  253. // FontUnit_Millimeter
  254. //
  255. this.FontUnit_Millimeter.Location = new System.Drawing.Point(392, 144);
  256. this.FontUnit_Millimeter.Name = "FontUnit_Millimeter";
  257. this.FontUnit_Millimeter.Size = new System.Drawing.Size(52, 24);
  258. this.FontUnit_Millimeter.TabIndex = 24;
  259. this.FontUnit_Millimeter.Text = "毫米";
  260. this.FontUnit_Millimeter.CheckedChanged += new System.EventHandler(this.FontUnit_Millimeter_CheckedChanged);
  261. //
  262. // FontUnit_Display
  263. //
  264. this.FontUnit_Display.Location = new System.Drawing.Point(168, 168);
  265. this.FontUnit_Display.Name = "FontUnit_Display";
  266. this.FontUnit_Display.Size = new System.Drawing.Size(112, 24);
  267. this.FontUnit_Display.TabIndex = 25;
  268. this.FontUnit_Display.Text = "与输出设备相同";
  269. this.FontUnit_Display.CheckedChanged += new System.EventHandler(this.FontUnit_Display_CheckedChanged);
  270. //
  271. // FontUnit_World
  272. //
  273. this.FontUnit_World.Location = new System.Drawing.Point(392, 168);
  274. this.FontUnit_World.Name = "FontUnit_World";
  275. this.FontUnit_World.Size = new System.Drawing.Size(80, 24);
  276. this.FontUnit_World.TabIndex = 26;
  277. this.FontUnit_World.Text = "世界坐标";
  278. this.FontUnit_World.CheckedChanged += new System.EventHandler(this.FontUnit_World_CheckedChanged);
  279. //
  280. // FontUnit_Document
  281. //
  282. this.FontUnit_Document.Location = new System.Drawing.Point(304, 168);
  283. this.FontUnit_Document.Name = "FontUnit_Document";
  284. this.FontUnit_Document.Size = new System.Drawing.Size(80, 24);
  285. this.FontUnit_Document.TabIndex = 27;
  286. this.FontUnit_Document.Text = "文档单位";
  287. this.FontUnit_Document.CheckedChanged += new System.EventHandler(this.FontUnit_Document_CheckedChanged);
  288. //
  289. // groupBox3
  290. //
  291. this.groupBox3.Controls.Add(this.groupBox4);
  292. this.groupBox3.Location = new System.Drawing.Point(160, 128);
  293. this.groupBox3.Name = "groupBox3";
  294. this.groupBox3.Size = new System.Drawing.Size(328, 72);
  295. this.groupBox3.TabIndex = 28;
  296. this.groupBox3.TabStop = false;
  297. this.groupBox3.Text = "选择大小单位";
  298. //
  299. // groupBox4
  300. //
  301. this.groupBox4.Location = new System.Drawing.Point(8, 160);
  302. this.groupBox4.Name = "groupBox4";
  303. this.groupBox4.Size = new System.Drawing.Size(328, 72);
  304. this.groupBox4.TabIndex = 29;
  305. this.groupBox4.TabStop = false;
  306. this.groupBox4.Text = "选择大小单位";
  307. //
  308. // groupBox5
  309. //
  310. this.groupBox5.Controls.Add(this.label1);
  311. this.groupBox5.Controls.Add(this.groupBox6);
  312. this.groupBox5.Controls.Add(this.FontSize);
  313. this.groupBox5.Location = new System.Drawing.Point(160, 208);
  314. this.groupBox5.Name = "groupBox5";
  315. this.groupBox5.Size = new System.Drawing.Size(328, 56);
  316. this.groupBox5.TabIndex = 30;
  317. this.groupBox5.TabStop = false;
  318. this.groupBox5.Text = "设置字体大小";
  319. //
  320. // label1
  321. //
  322. this.label1.Location = new System.Drawing.Point(24, 24);
  323. this.label1.Name = "label1";
  324. this.label1.Size = new System.Drawing.Size(80, 16);
  325. this.label1.TabIndex = 32;
  326. this.label1.Text = "字体大小";
  327. //
  328. // groupBox6
  329. //
  330. this.groupBox6.Location = new System.Drawing.Point(8, 160);
  331. this.groupBox6.Name = "groupBox6";
  332. this.groupBox6.Size = new System.Drawing.Size(328, 72);
  333. this.groupBox6.TabIndex = 29;
  334. this.groupBox6.TabStop = false;
  335. this.groupBox6.Text = "选择大小单位";
  336. //
  337. // FontSize
  338. //
  339. this.FontSize.Location = new System.Drawing.Point(120, 24);
  340. this.FontSize.Name = "FontSize";
  341. this.FontSize.Size = new System.Drawing.Size(56, 21);
  342. this.FontSize.TabIndex = 31;
  343. this.FontSize.Value = new System.Decimal(new int[] {
  344. 1,
  345. 0,
  346. 0,
  347. 0});
  348. this.FontSize.ValueChanged += new System.EventHandler(this.FontSize_ValueChanged);
  349. //
  350. // FontPreview
  351. //
  352. this.FontPreview.Location = new System.Drawing.Point(8, 272);
  353. this.FontPreview.Name = "FontPreview";
  354. this.FontPreview.Size = new System.Drawing.Size(480, 104);
  355. this.FontPreview.TabIndex = 31;
  356. this.FontPreview.Text = "字体示例";
  357. //
  358. // Form3
  359. //
  360. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  361. this.ClientSize = new System.Drawing.Size(496, 382);
  362. this.Controls.Add(this.FontPreview);
  363. this.Controls.Add(this.FontUnit_Document);
  364. this.Controls.Add(this.FontUnit_World);
  365. this.Controls.Add(this.FontUnit_Display);
  366. this.Controls.Add(this.FontUnit_Millimeter);
  367. this.Controls.Add(this.FontUnit_Inch);
  368. this.Controls.Add(this.FontUnit_Pixel);
  369. this.Controls.Add(this.FontColor_B);
  370. this.Controls.Add(this.groupBox1);
  371. this.Controls.Add(this.FontColor_R);
  372. this.Controls.Add(this.FontUnit_Dot);
  373. this.Controls.Add(this.FontColor_A);
  374. this.Controls.Add(this.FontlistBox);
  375. this.Controls.Add(this.FontColor_G);
  376. this.Controls.Add(this.groupBox2);
  377. this.Controls.Add(this.groupBox3);
  378. this.Controls.Add(this.groupBox5);
  379. this.Name = "Form3";
  380. this.Text = "增强型字体选择对话框";
  381. this.Load += new System.EventHandler(this.Form3_Load);
  382. this.groupBox1.ResumeLayout(false);
  383. ((System.ComponentModel.ISupportInitialize)(this.FontColor_R)).EndInit();
  384. ((System.ComponentModel.ISupportInitialize)(this.FontColor_A)).EndInit();
  385. ((System.ComponentModel.ISupportInitialize)(this.FontColor_G)).EndInit();
  386. ((System.ComponentModel.ISupportInitialize)(this.FontColor_B)).EndInit();
  387. this.groupBox3.ResumeLayout(false);
  388. this.groupBox5.ResumeLayout(false);
  389. ((System.ComponentModel.ISupportInitialize)(this.FontSize)).EndInit();
  390. this.ResumeLayout(false);
  391. }
  392. #endregion
  393. //字体对话框加载时的初始化处理
  394. private void Form3_Load(object sender, System.EventArgs e)
  395. {
  396. string tmp=string.Empty;
  397. //获取所有已经安装的字体系列
  398. InstalledFontCollection installedFontCollection=new InstalledFontCollection();
  399. FontFamily[] fontfamily=installedFontCollection.Families;
  400. int index=0;
  401. //访问fontfamily数组的每一个成员
  402. foreach (FontFamily i in fontfamily)
  403. {
  404. //在字体列表框中添加字体系列名
  405. this.FontlistBox.Items.Add(i.Name);
  406. index++;
  407. }
  408. //色彩色彩微调控件的取值范围:最大值
  409. FontColor_A.Maximum=255;
  410. FontColor_R.Maximum=255;
  411. FontColor_G.Maximum=255;
  412. FontColor_B.Maximum=255;
  413. //最小值
  414. FontColor_A.Minimum=0;
  415. FontColor_R.Minimum=0;
  416. FontColor_G.Minimum=0;
  417. FontColor_B.Minimum=0;
  418. //设置色彩默认值 
  419. this.FontColor_A.Value=255;
  420. this.FontColor_R.Value=0;
  421. this.FontColor_G.Value=0;
  422. this.FontColor_B.Value=0;
  423. //字体大小微调控件取值范围
  424. this.FontSize.Minimum=1;
  425. this.FontSize.Maximum=100;
  426. //默认的字体风格为常规
  427. this.FontStyle_Regular.Checked=true;
  428. //默认的字体单位为点
  429. this.FontUnit_Dot.Checked=true;
  430. //默认的字体大小为12
  431. this.FontSize.Value=12;
  432. //默认的字体系列为列表框中的第一个列表项
  433. this.FontlistBox.SelectedIndex=0;
  434. this.RedrawFontPreviewWindow();
  435. }
  436. private void FontlistBox_SelectedIndexChanged(object sender, System.EventArgs e)
  437. {
  438. this.RedrawFontPreviewWindow();
  439. }
  440. //根据用户对字体设置的信息,在预览框中进行字体显示
  441. public void RedrawFontPreviewWindow()
  442. {
  443. //如果未选择字体系列名称
  444. if(this.FontlistBox.SelectedIndex==-1)
  445. return;
  446. //示例字体输出效果的显示区域 
  447. Rectangle textOut=new Rectangle(0,0,
  448. this.FontPreview.Width,this.FontPreview.Height);
  449. Graphics graphics=this.FontPreview.CreateGraphics();
  450. graphics.Clear(this.FontPreview.BackColor);
  451. //获取当前已经选择的字体系列名称及字体大小
  452. string font_name=this.FontlistBox.Text;
  453. decimal font_size=this.FontSize.Value;
  454. //使用灰色线条绘制基准线,以10*10像素为单位
  455. Pen pen=new Pen(Color.Gray);
  456. //水平线
  457. for(int i=0;i<textOut.Height;i+=10)
  458. graphics.DrawLine(pen,0,i,textOut.Width,i);
  459. //垂直线
  460. for(int i=0;i<textOut.Width;i+=10)
  461. graphics.DrawLine(pen,i,0,i,textOut.Height);
  462. //获取当前已经选择的字体大小单位
  463. GraphicsUnit font_uint=GraphicsUnit.Point;
  464. if(this.FontUnit_Document.Checked)
  465. font_uint=GraphicsUnit.Document;
  466. if(this.FontUnit_Inch.Checked)
  467. font_uint=GraphicsUnit.Inch;
  468. if(this.FontUnit_Millimeter.Checked)
  469. font_uint=GraphicsUnit.Millimeter;
  470. if(this.FontUnit_Pixel.Checked)
  471. font_uint=GraphicsUnit.Pixel;
  472. if(this.FontUnit_Dot.Checked)
  473. font_uint=GraphicsUnit.Point;
  474. if(this.FontUnit_World.Checked)
  475. font_uint=GraphicsUnit.World;
  476. //获取当前已经选择的字体风格
  477. FontStyle font_style=FontStyle.Regular;
  478. if(this.FontStyle_Regular.Checked)
  479. font_style|=FontStyle.Regular;
  480. if(this.FontStyle_Bold.Checked)
  481. font_style|=FontStyle.Bold;
  482. if(this.FontStyle_Italic.Checked)
  483. font_style|=FontStyle.Italic;
  484. if(this.FontStyle_Strikeout.Checked)
  485. font_style|=FontStyle.Strikeout;
  486. if(this.FontStyle_Underline.Checked)
  487. font_style|=FontStyle.Underline;
  488. //获取选择的字体色彩
  489. Color basecolor=Color.FromArgb((int)this.FontColor_R.Value,
  490. (int)this.FontColor_G.Value,(int)this.FontColor_B.Value);
  491. //根据选择的色彩构造输出文本时使用的画刷
  492. SolidBrush solidBrush=new SolidBrush(Color.FromArgb((int)this.FontColor_A.Value,basecolor));
  493. //根据选择的字体信息构造字体
  494. FontFamily fontFamily=new FontFamily(font_name);
  495. Font font=new Font(fontFamily,
  496. (int)font_size, (FontStyle)font_style, font_uint);
  497. //设置文本输出格式:居中
  498. StringFormat fmt=new StringFormat();
  499. fmt.Alignment=StringAlignment.Center;
  500. fmt.LineAlignment=StringAlignment.Center;
  501. //在字体示意区域输出文本
  502. graphics.DrawString("GDI+程序设计",font,solidBrush,textOut,fmt);
  503. }
  504. private void FontSize_ValueChanged(object sender, System.EventArgs e)
  505. {
  506. this.RedrawFontPreviewWindow();
  507. }
  508. private void FontColor_A_ValueChanged(object sender, System.EventArgs e)
  509. {
  510. this.RedrawFontPreviewWindow();
  511. }
  512. private void FontColor_R_ValueChanged(object sender, System.EventArgs e)
  513. {
  514. this.RedrawFontPreviewWindow();
  515. }
  516. private void FontColor_G_ValueChanged(object sender, System.EventArgs e)
  517. {
  518. this.RedrawFontPreviewWindow();
  519. }
  520. private void FontColor_B_ValueChanged(object sender, System.EventArgs e)
  521. {
  522. this.RedrawFontPreviewWindow();
  523. }
  524. private void FontStyle_Regular_CheckedChanged(object sender, System.EventArgs e)
  525. {
  526. this.RedrawFontPreviewWindow();
  527. }
  528. private void FontStyle_Bold_CheckedChanged(object sender, System.EventArgs e)
  529. {
  530. this.RedrawFontPreviewWindow();
  531. }
  532. private void FontStyle_Italic_CheckedChanged(object sender, System.EventArgs e)
  533. {
  534. this.RedrawFontPreviewWindow();
  535. }
  536. private void FontStyle_Underline_CheckedChanged(object sender, System.EventArgs e)
  537. {
  538. this.RedrawFontPreviewWindow();
  539. }
  540. private void FontStyle_Strikeout_CheckedChanged(object sender, System.EventArgs e)
  541. {
  542. this.RedrawFontPreviewWindow();
  543. }
  544. private void FontUnit_Dot_CheckedChanged(object sender, System.EventArgs e)
  545. {
  546. this.RedrawFontPreviewWindow();
  547. }
  548. private void FontUnit_Inch_CheckedChanged(object sender, System.EventArgs e)
  549. {
  550. this.RedrawFontPreviewWindow();
  551. }
  552. private void FontUnit_Pixel_CheckedChanged(object sender, System.EventArgs e)
  553. {
  554. this.RedrawFontPreviewWindow();
  555. }
  556. private void FontUnit_Millimeter_CheckedChanged(object sender, System.EventArgs e)
  557. {
  558. this.RedrawFontPreviewWindow();
  559. }
  560. private void FontUnit_Display_CheckedChanged(object sender, System.EventArgs e)
  561. {
  562. this.RedrawFontPreviewWindow();
  563. }
  564. private void FontUnit_Document_CheckedChanged(object sender, System.EventArgs e)
  565. {
  566. this.RedrawFontPreviewWindow();
  567. }
  568. private void FontUnit_World_CheckedChanged(object sender, System.EventArgs e)
  569. {
  570. this.RedrawFontPreviewWindow();
  571. }
  572. }
  573. }