Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF RichTextBox增量字体大小_C#_Wpf_Fonts_Richtextbox_Textrange - Fatal编程技术网

C# WPF RichTextBox增量字体大小

C# WPF RichTextBox增量字体大小,c#,wpf,fonts,richtextbox,textrange,C#,Wpf,Fonts,Richtextbox,Textrange,正如标题所示,我的目标是增加/减少RichTextBox中当前所选文本的字体大小 这可能看起来很琐碎,事实上确实如此——只要TextRange中的所有文本的字体大小相同。当所选内容包含不同字体大小的文本时 range.GetPropertyValue(TextElement.FontSizeProperty); 我使用它来获取字体大小的上一个值(为了知道将值设置为什么是必需的),它返回DependencyProperty.UnsetValue 这不仅是一个问题,而且没有一个方法来增加大小,只有

正如标题所示,我的目标是增加/减少RichTextBox中当前所选文本的字体大小

这可能看起来很琐碎,事实上确实如此——只要TextRange中的所有文本的字体大小相同。当所选内容包含不同字体大小的文本时

range.GetPropertyValue(TextElement.FontSizeProperty);
我使用它来获取字体大小的上一个值(为了知道将值设置为什么是必需的),它返回DependencyProperty.UnsetValue

这不仅是一个问题,而且没有一个方法来增加大小,只有一个方法显式地将其设置为给定的值,这一事实在这里引起了一个问题

我曾考虑过尝试将TextRange解析为具有不同属性值的子范围,但这似乎是一种非常复杂的方法来实现一些应该是微不足道的事情


我该怎么办?提前感谢。

我认为任何实施(如果存在的话)最终都必须执行与您建议的完全相同的操作,因此您的选择是有限的:

如果您关心的是这是大量的操作,那么您可能是对的,我能想到的唯一概念上的捷径就是应用ScaleTransform。当然,这只会让它看起来有效,而不会改变选择的属性


如果您担心您的用户代码中存在大量混乱,那么您也是对的,但我建议您通过制作自己的RichTextBox并自己实现递增SizeOfsElection和递减SizeOfsElection来隐藏这一点。

通过RichTextBox的选择获取TextRange,这将为您提供TextRange,您的字体大小有一个双倍值。试试这个

 public void SetFontSizeForSelection(TextRange selection, double newFontSize)
        {
            if ((newFontSize - 0) < double.Epsilon)
                newFontSize = 1;
            selection.ApplyPropertyValue(TextElement.FontSizeProperty, newFontSize);
        }
public void SetFontSizeForSelection(文本范围选择,双newFontSize)
{
if((newFontSize-0)
我认为必要的操作并不太多。 我尝试过使用多个不同的文本大小——此示例仅在所选文本包含在元素中时有效。但是应该可以为不同的元素扩展代码,如 然而,我不知道如何处理这样的事情 加粗

    private void sizeTextSel(int iVal)
    {
        TextSelection ts = _richTextBox.Selection;
        TextPointer tpStart = ts.Start;
        TextPointer tpEnd = ts.End;
        TextPointer tpRun = tpStart;

        int iSelLength = tpStart.GetOffsetToPosition(tpEnd);
        int iStartRunLength;
        object fontSizeSelection;
        int iTotalSelected = 0,iSelect;

        do
        {                   
            iStartRunLength = tpRun.GetTextRunLength(LogicalDirection.Forward);
            iSelect = iStartRunLength < iSelLength - iTotalSelected ? iStartRunLength : iSelLength - iTotalSelected;
            ts.Select(tpRun, tpRun.GetPositionAtOffset(iSelect,LogicalDirection.Forward));
            fontSizeSelection = ts.GetPropertyValue(TextElement.FontSizeProperty);
            if (fontSizeSelection != null)
                ts.ApplyPropertyValue(TextElement.FontSizeProperty, (double)fontSizeSelection + iVal);

            tpRun = tpRun.GetPositionAtOffset(iSelect + 1);
            while (tpRun != null && tpRun.Parent.GetType() != typeof(Run))
            {
                tpRun = tpRun.GetPositionAtOffset(1);
                //iOffset +=1;
            }
            iTotalSelected += iSelect+1;

        } while (tpRun != null && iTotalSelected < iSelLength);

        ts.Select(tpStart, tpEnd);
    }
private void sizeTextSel(int-iVal)
{
TextSelection ts=\u richTextBox.Selection;
text指针tpStart=ts.Start;
text指针tpEnd=ts.End;
text指针tpRun=tpStart;
int iSelLength=tpStart.getOffsetPosition(tpEnd);
int iStartRunLength;
对象字体大小选择;
int iTotalSelected=0,iSelect;
做
{                   
iStartRunLength=tpRun.GetTextRunLength(LogicalDirection.Forward);
iSelect=iStartRunLength
我想现在应该可以了。。。正如我在上一篇文章中所评论的,当元素的textsize变为相同时,这会自动合并元素:

    private void sizeTextSel(int iVal)
    {
        TextSelection ts = _richTextBox.Selection;
        TextPointer tpStart = ts.Start;
        TextPointer tpEnd = ts.End;
        TextPointer tpRun = tpStart;

        int iSelLength = ts.Text.Length;
        int iStartRunLength;
        object fontSizeSelection;
        int iTotalSelected = 0, iSelect = 0,iNew=0;
        do
        {
            iStartRunLength = tpRun.GetTextRunLength(LogicalDirection.Forward);
            iSelect = iStartRunLength < iSelLength - iTotalSelected ? iStartRunLength : iSelLength - iTotalSelected;
            if (iStartRunLength > 0)
            {
                iSelect = iSelect == 0 ? 1 : iSelect;
                ts.Select(tpRun, tpRun.GetPositionAtOffset(iSelect, LogicalDirection.Forward));
                fontSizeSelection = ts.GetPropertyValue(TextElement.FontSizeProperty);
                if (fontSizeSelection != null)
                    ts.ApplyPropertyValue(TextElement.FontSizeProperty, (double)fontSizeSelection + iVal);
                iNew = tpRun.GetTextRunLength(LogicalDirection.Forward);
                if (iNew==0)
                    tpRun = tpRun.GetPositionAtOffset(iSelect + 1, LogicalDirection.Forward);
                else
                    tpRun = tpRun.GetPositionAtOffset(iSelect, LogicalDirection.Forward);
            }
            else
            {
                if (tpRun.Parent.GetType() == typeof(FlowDocument))
                    iSelect = 2;
                else
                    iSelect = 0;
                tpRun = tpRun.GetPositionAtOffset(1, LogicalDirection.Forward);
            }

            iTotalSelected += iSelect;

        } while (tpRun != null && iTotalSelected < iSelLength);

        ts.Select(tpStart, tpEnd);
    }
private void sizeTextSel(int-iVal)
{
TextSelection ts=\u richTextBox.Selection;
text指针tpStart=ts.Start;
text指针tpEnd=ts.End;
text指针tpRun=tpStart;
int iSelLength=ts.Text.Length;
int iStartRunLength;
对象字体大小选择;
int iTotalSelected=0,iSelect=0,iNew=0;
做
{
iStartRunLength=tpRun.GetTextRunLength(LogicalDirection.Forward);
iSelect=iStartRunLength0)
{
iSelect=iSelect==0?1:iSelect;
ts.Select(tpRun,tpRun.GetPositionAtOffset(iSelect,LogicalDirection.Forward));
fontSizeSelection=ts.GetPropertyValue(TextElement.FontSizeProperty);
如果(fontSizeSelection!=null)
ts.ApplyPropertyValue(TextElement.FontSizeProperty,(双精度)fontSizeSelection+iVal);
iNew=tpRun.gettextunlength(逻辑方向.Forward);
如果(iNew==0)
tpRun=tpRun.GetPositionAtOffset(iSelect+1,LogicalDirection.Forward);
其他的
tpRun=tpRun.GetPositionAtOffset(iSelect,LogicalDirection.Forward);
}
其他的
{
if(tpRun.Parent.GetType()==typeof(FlowDocument))
iSelect=2;
其他的
iSelect=0;
tpRun=tpRun.GetPositionAtOffset(1,LogicalDirection.Forward);
}
iTotalSelected+=iSelect;
}while(tpRun!=null&&iTotalSelected
如果文本选择的字体大小不同,正确的字体大小应该是多少?正如我所说,我的目标是增加/减少字体大小。因此,如果有多个字体大小,那么每个字体大小应该增加一小部分。我认为这在当前的RichTextBox中是不可能的。你自己的