文字格式设置:
字体颜色设置:
测试:
Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace ColorDialog { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
Form1.cs:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ColorDialog { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { DialogResult dr = colorDialog1.ShowDialog(); //显示选择文字颜色对话框 if(dr==DialogResult.OK) { richTextBox1.SelectionColor = colorDialog1.Color; //设置 richTextbox1 中的文字颜色 } } private void button2_Click(object sender, EventArgs e) { DialogResult dr = fontDialog1.ShowDialog(); if(dr==DialogResult.OK) { richTextBox1.SelectionFont = fontDialog1.Font; } } } }
Form1设计:
namespace ColorDialog { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.colorDialog1 = new System.Windows.Forms.ColorDialog(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.fontDialog1 = new System.Windows.Forms.FontDialog(); this.SuspendLayout(); // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(1, 69); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(292, 153); this.richTextBox1.TabIndex = 0; this.richTextBox1.Text = " 哈哈哈"; // // button1 // this.button1.Location = new System.Drawing.Point(162, 25); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "选择颜色"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(55, 25); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 2; this.button2.Text = "选择字体"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(290, 218); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.richTextBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion private System.Windows.Forms.ColorDialog colorDialog1; private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.FontDialog fontDialog1; } }
C#-ColorDialog,FontDialog-文字格式的设置---ShinePans,码迷,mamicode.com
时间: 2024-10-20 06:29:26