当前位置: 首页 > article >正文

C# WPF FontDialog字体对话框,ColorDialog颜色对话框 引用

WPF 并没有内置FontDialog和ColorDialog,但可以通过引用 Windows Forms 的控件来实现字体和颜色选择对话框功能。FontDialog 允许用户选择字体、样式、大小等设置。

添加 Windows Forms的引用

  • 项目工程:右键“引用”=》“添加引用”=》勾选System.Windows.Forms

  • 点击“确认”即可添加成功;

  • 点击“浏览”可以手动选择添加指定程序集文件;
    在这里插入图片描述

  • 浏览选择System.Windows.Forms.dll动态库文件

  • 默认路径:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework
    在这里插入图片描述

  • 添加成功
    在这里插入图片描述

  • 在C# WPF应用程序中,不能直接使用System.Drawing.Font,因为System.Drawing.Font是Windows Forms的一部分,而不是WPF。WPF使用System.Windows.Media.FontFamily来表示字体。需要做相应的转换才可以使用;

  • WinForm中的Color( System.Drawing.Color)与Wpf中Color(System.Windows.Media.Color)也需要要相互转换才能使用;

代码示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace WpfColorFontDialog
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnSetFont_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FontDialog fontDialog = new System.Windows.Forms.FontDialog();
            if (fontDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // System.Drawing.Font DFont = new System.Drawing.Font("宋体", 10, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic);
                
                System.Drawing.Font drawingFont = fontDialog.Font;
                FontFamily fontFamily = new FontFamily(drawingFont.Name);
                double fontSize = drawingFont.Size;
                FontWeight fontWeight = FontWeights.Bold;
                if (drawingFont.Bold)
                {
                    fontWeight = FontWeights.Bold;
                }
                else {
                    fontWeight = FontWeights.Normal;
                }
                FontStyle fontStyle = (drawingFont.Style & System.Drawing.FontStyle.Italic) != 0 ? FontStyles.Italic : FontStyles.Normal;

                txtBlock.FontFamily = fontFamily;
                txtBlock.FontSize = fontSize;
                txtBlock.FontStyle = fontStyle;
                txtBlock.FontWeight = fontWeight;
            }
        }



        private void btnSetColor_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.ColorDialog colorDialog = new System.Windows.Forms.ColorDialog();
            if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                System.Windows.Media.Color MColor = new System.Windows.Media.Color();
                MColor = System.Windows.Media.Color.FromArgb(colorDialog.Color.A, colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B);
                System.Windows.Media.Brush BColor = new SolidColorBrush(MColor);
                
                txtBlock.Background = BColor;
            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述


http://www.kler.cn/a/394542.html

相关文章:

  • 批量重命名Excel文件并排序
  • 亮数据——助力全球数据抓取的高效代理平台
  • 力扣最热一百题——完全平方数(中等难度,详细分析)
  • 【Excel】ToRow超级查找函数
  • 随机数
  • Spring Boot中的自动装配机制
  • 【竞技宝】CS2-上海majorRMR:美洲区最后门票争夺战
  • Spark 共享变量:广播变量与累加器解析
  • spring-webmvc根据请求路径找到对应的 HandlerMethod
  • [代码随想录Day11打卡] 150. 逆波兰表达式求值 239. 滑动窗口最大值 (有点难度) 347.前 K 个高频元素 (有点难度) 总结
  • 28、dawn
  • .NET 中的虚拟内存
  • 浅谈C#之多线程流式适配器
  • kafka如何知道哪个消费者消费哪个分区?
  • 单片机设计智能翻译手势识别系统
  • 「QT」窗口类 之 QWidget 窗口基类
  • 进入未来城:第五周游戏指南
  • 机器学习day4-朴素贝叶斯分类和决策树
  • ssm121开放式教学评价管理系统+vue(论文+源码)_kaic
  • java -jar`命令详解:运行JAR文件、传递参数与性能调优