WPF 中的 WinForms.ColorDialog
在小型 WPF 项目 TextEditor 中使用 System.Windows.Forms.ColorDialog。
引言
本文演示了如何在 WPF 项目中使用 System.Windows.Forms.ColorDialog
。 为什么呢? 仅仅因为可以尝试,并且应该尝试...
背景
为了避免为我的解决方案设计 UserControl 或额外的项目,我开始寻找一个简单的解决方案,因此我创建了以下内容。 它还演示了如何在 WPF 解决方案中使用 WinForms 组件。
使用代码
为了演示,我创建了一个小型 TextEditor。 我将展示如何更改 RichTextBox
中选定文本的前景色/背景色。
- 创建一个名为 TextEditor 的 WPF 项目的新解决方案
- 添加引用 System.Windows.Forms.dll 和 System.Drawing.dll。 这两个文件都可以在以下目录中找到:C:\Windows\Microsoft.NET\Framwork\v2.0.50727\。
- 图像
- 文档管理器
- MainWindow.xml
- MainWindow.xaml.cs
[解决方案资源管理器]
在项目中添加一个名为 Images 的文件夹,然后添加工具栏所需的图像。
在项目中添加一个名为 DocumentManager.cs 的类,然后添加一些方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using Microsoft.Win32;
namespace TextEditor
{
class DocumentManager
{
private string _currentFile;
private RichTextBox _textBox;
public DocumentManager(RichTextBox textBox)
{
_textBox = textBox;
}
public void ApplyToSelection(DependencyProperty property, object value)
{
_textBox.Selection.ApplyPropertyValue(property, value);
}
public bool OpenDocument()
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == true)
{
_currentFile = dlg.FileName;
using (Stream stream = dlg.OpenFile())
{
TextRange range = new TextRange(
_textBox.Document.ContentStart,
_textBox.Document.ContentEnd);
range.Load(stream, DataFormats.Rtf);
}
return true;
}
return false;
}
public bool SaveDocument()
{
if (string.IsNullOrEmpty(_currentFile)) return SaveDocumentAs();
else
{
using (Stream stream = new FileStream(_currentFile, FileMode.Create))
{
TextRange range = new TextRange(
_textBox.Document.ContentStart,
_textBox.Document.ContentEnd);
range.Save(stream, DataFormats.Rtf);
}
return true;
}
}
public bool SaveDocumentAs()
{
SaveFileDialog dlg = new SaveFileDialog();
if (dlg.ShowDialog() == true)
{
_currentFile = dlg.FileName;
return SaveDocument();
}
return false;
}
public void NewDocument()
{
_currentFile = null;
_textBox.Document = new FlowDocument();
}
}
}
<Window x:Class="TextEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525">
<DockPanel>
<ToolBarTray DockPanel.Dock="top">
<ToolBar >
<Button x:Name="highlight" Click="Highlight_Click">
<Image Source="Images\text_highlight.png" />
</Button>
<Button x:Name="color" Click="Color_Click">
<Image Source="Images\text_Color.png" />
</Button>
</ToolBar>
</ToolBarTray>
<RichTextBox x:Name="body"
SelectionChanged="body_SelctionChanged"
SpellCheck.IsEnabled="True"
AcceptsReturn="True"
AcceptsTab="True"
BorderThickness="0 2 0 0" />
</DockPanel>
</Window>
usisng System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
namespace TextEditor
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private DocumentManager _documentManager;
public MainWindow()
{
InitializeComponent();
// Insert code required on object creation below this point.
_documentManager = new DocumentManager(body);
}
private void body_SelctionChanged(object seder, RoutedEventArgs e)
{
//update the tool bar
}
private void Highlight_Click(object sender, RoutedEventArgs e)
{
SolidColorBrush scb = new SolidColorBrush();
_documentManager.ApplyToSelection(
TextBlock.ForegroundProperty,
new SolidColorBrush(colorPicker())
);
}
private void Color_Click(object sender, RoutedEventArgs e)
{
_documentManager.ApplyToSelection(
TextBlock.BackgroundProperty,
new SolidColorBrush(colorPicker())
);
}
private System.Windows.Media.Color colorPicker()
{
System.Windows.Forms.ColorDialog colorDialog =
new System.Windows.Forms.ColorDialog();
colorDialog.AllowFullOpen = true;
colorDialog.ShowDialog();
System.Windows.Media.Color col = new System.Windows.Media.Color();
col.A = colorDialog.Color.A;
col.B = colorDialog.Color.B;
col.G = colorDialog.Color.G;
col.R = colorDialog.Color.R;
return col;
}
}
}
关注点
我了解到,不必丢弃旧的库。