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

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

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

添加 Windows Forms的引用

  1. 项目工程:右键“引用”=》“添加引用”=》勾选System.Windows.Forms
    在这里插入图片描述
  2. 浏览选择System.Windows.Forms.dll动态库文件
    默认路径:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework
    在这里插入图片描述
    3.添加成功
    在这里插入图片描述
  • 在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.lryc.cn/news/482142.html

相关文章:

  • 在unity中实现把普通的照片,图片 变成油画风格的shader实现
  • 使用elementUI实现表格行拖拽改变顺序,无需引入外部库
  • PySpark 数据处理实战:从基础操作到案例分析
  • 恒源云使用手册记录:从服务器下载数据到本地
  • 【大咖云集 | IEEE计算智能学会广州分会支持】第四届信息技术与当代体育国际学术会议(TCS 2024,12月13-15日)
  • 【AI声音克隆整合包及教程】第二代GPT-SoVITS V2:技术、应用与伦理思考
  • 利用AI制作《职业生涯规划PPT》,10分钟完成
  • 【Java多线程】线程安全及解决方案(详解)
  • 【前端基础】Javascript取整函数以及向零取整方式
  • 禅道与Jira与Ones对比:哪个更适合你的项目管理需求?
  • Linux I/O编程:I/O多路复用与异步 I/O对比
  • Spark Plan 之 SQLMetric
  • 基于YOLOv5模型的火焰识别系统
  • 多模态AI:开启人工智能的新纪元
  • 麒麟信安支撑2024年电力监控系统网络安全加固培训护航电力网络安全!
  • 横表和纵表 中的横表
  • 7个常用的JavaScript数组操作进阶用法
  • Spark的Standalone集群环境安装
  • Android Glide动态apply centerCropTransform(),transition withCrossFade动画,Kotlin
  • shukla方差和相对平均偏差
  • 双指针(二)双指针到底是怎么个事
  • vscode通过remote-ssh连接远程开发机
  • uniapp实现H5和微信小程序获取当前位置(腾讯地图)
  • SQL HAVING子句
  • 计算机视觉基础:OpenCV库详解
  • UI自动化测试工具(超详细总结)
  • AJAX 全面教程:从基础到高级
  • ONLYOFFICE 8.2测评:功能增强与体验优化,打造高效办公新体验
  • Science Robotics 综述揭示演化研究新范式,从机器人复活远古生物!
  • uni-app表格带分页,后端处理过每页显示多少条