
漫反射是指光线被粗糙表面无规则地向各个方向反射的现象。很多物体,如植物、书本、墙壁、衣服等,其表面看起来似乎是平滑,但用放大镜仔细观察,就会看到其表面是凹凸不平的,所以本来是平行的太阳光被这些表面反射后,就弥漫地射向不同方向。
例如,阳光射到镜子上,迎着反射光的方向可以看到刺眼的光。如果阳光射到白纸上,无论在哪个方向,都不会感到刺眼。
原来,镜面很光滑,而看上去很平的白纸,细微之处实际是凹凸不平的。这就是利用了漫反射。
WPF中没有直接的控件能够达到这个效果,要自己写控件。给你写了个自定义控件的例子,代码如下:
代码1自定义控件RowTextPresenter,提供隔行效果的控件。(示例代码,仅仅封装了FontSize和FontFamily属性,你可以仿造示例自行封装)
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemWindows;
using SystemWindowsControls;
using SystemWindowsData;
using SystemWindowsDocuments;
using SystemWindowsInput;
using SystemWindowsMedia;
using SystemWindowsMediaImaging;
using SystemWindowsNavigation;
using SystemWindowsShapes;
namespace WpfTextRowControls
{
public class RowTextPresenter : FrameworkElement
{
private TextBlock _textPresenter = new TextBlock();
private DrawingVisual _rowsPresenter = new DrawingVisual();
public RowTextPresenter()
{
_textPresenterTextWrapping = TextWrappingWrap;
thisAddVisualChild(_rowsPresenter);
thisAddVisualChild(_textPresenter);
}
protected override int VisualChildrenCount
{
get
{
return 2;
}
}
protected override Visual GetVisualChild(int index)
{
if (index == 0) return _rowsPresenter;
if (index == 1) return _textPresenter;
throw new IndexOutOfRangeException();
}
protected override void OnRender(DrawingContext drawingContext)
{
baseOnRender(drawingContext);
DrawRows();
}
protected override Size MeasureOverride(Size availableSize)
{
_textPresenterMeasure(availableSize);
return _textPresenterDesiredSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
_textPresenterArrange(new Rect(new Point(0, 0), finalSize));
return finalSize;
}
private void DrawRows()
{
if (_textPresenter == null || _rowsPresenter == null)
return;
var width = _textPresenterActualWidth;
// 通过反射的方式获取折行的行数
var lineCount = _textPresenterReflectGetProperty<int>("LineCount");
var dc = _rowsPresenterRenderOpen();
if (lineCount > 1)
{
var offsetY = 00;
var baseValue = (thisAlternationMode == AlternationModeEven) 1 : 0;
for (int i = 0; i < lineCount; i++)
{
// 通过反射的方式获取每一行的高度
var lineMetrics = _textPresenterReflectCall("GetLine", i);
var lineHeight = lineMetricsReflectGetProperty<double>("Height");
// 判断奇偶行,绘制背景块
if (i % 2 == baseValue)
{
dcDrawRectangle(thisAlternationBackground, null, new Rect(0, offsetY, width, lineHeight));
}
offsetY += lineHeight;
}
}
dcClose();
}
#region FontSize
public static readonly DependencyProperty FontSizeProperty =
TextElementFontSizePropertyAddOwner(typeof(RowTextPresenter),
new FrameworkPropertyMetadata((double)120));
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
#endregion
#region FontFamily
/// <summary>
/// FontFamily Dependency Property
/// </summary>
public static readonly DependencyProperty FontFamilyProperty =
TextElementFontFamilyPropertyAddOwner(typeof(RowTextPresenter),
new FrameworkPropertyMetadata(null));
/// <summary>
/// Gets or sets the FontFamily property This dependency property
/// indicates
/// </summary>
public FontFamily FontFamily
{
get { return (FontFamily)GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}
#endregion
#region Text
public static readonly DependencyProperty TextProperty =
DependencyPropertyRegister("Text", typeof(string), typeof(RowTextPresenter),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptionsAffectsRender,
new PropertyChangedCallback(OnTextChanged)));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((RowTextPresenter)d)OnTextChanged(e);
}
protected virtual void OnTextChanged(DependencyPropertyChangedEventArgs e)
{
_textPresenterText = (string)eNewValue;
}
#endregion
#region AlternationBackground
public static readonly DependencyProperty AlternationBackgroundProperty =
DependencyPropertyRegister("AlternationBackground", typeof(Brush), typeof(RowTextPresenter),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptionsNone,
new PropertyChangedCallback(OnAlternationBackgroundChanged)));
public Brush AlternationBackground
{
get { return (Brush)GetValue(AlternationBackgroundProperty); }
set { SetValue(AlternationBackgroundProperty, value); }
}
private static void OnAlternationBackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((RowTextPresenter)d)OnAlternationBackgroundChanged(e);
}
protected virtual void OnAlternationBackgroundChanged(DependencyPropertyChangedEventArgs e)
{
thisDrawRows();
}
#endregion
#region AlternationMode
public static readonly DependencyProperty AlternationModeProperty =
DependencyPropertyRegister("AlternationMode", typeof(AlternationMode), typeof(RowTextPresenter),
new FrameworkPropertyMetadata(AlternationModeEven,
FrameworkPropertyMetadataOptionsAffectsRender));
public AlternationMode AlternationMode
{
get { return (AlternationMode)GetValue(AlternationModeProperty); }
set { SetValue(AlternationModeProperty, value); }
}
#endregion
}
/// <summary>
/// 跳行模式
/// </summary>
public enum AlternationMode
{
/// <summary>
/// 从偶数行开始
/// </summary>
Even,
/// <summary>
/// 从奇数行开始
/// </summary>
Odd
}
}
代码2ReflectionService,提供反射功能的工具类
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemReflection;
namespace WpfTextRowControls
{
public static class ReflectionService
{
private static readonly BindingFlags ReflectionFlags =
BindingFlagsPublic |
BindingFlagsNonPublic |
BindingFlagsInstance;
public static T ReflectGetProperty<T>(this object target, string propertyName)
{
var type = targetGetType();
var propertyInfo = typeGetProperty(propertyName, ReflectionFlags);
return (T)propertyInfoGetValue(target, null);
}
public static object ReflectCall(this object target, string methodName, params object[] args)
{
var type = targetGetType();
var propertyInfo = typeGetMethod(methodName, ReflectionFlags);
return propertyInfoInvoke(target, args);
}
}
}
代码3使用的代码
<l:RowTextPresenter AlternationBackground="LightGreen"
FontSize="14"
Text="BEIJING—A study by China's Ministry of Commerce has found the European Union and its member states offered large subsidies to major telecommunications-infrastructure companies that Beijing views as a breach of World Trade Organization rules, a person familiar with the matter said WednesdayThe study, meant for internal distribution, could lay the foundation for a response by Beijing to any future punitive action by the EU following its recent investigation that found major Chinese telecom-equipment makers, such as Huawei Technologies Co and ZTE Corp, benefited from significant financial backing from the Chinese governmentThe study by the commerce ministry found Brussels and EU member countries gave such subsidies by funding research-and-development projects, and by extending export credits and loans, said the person, who declined to be named" />
效果
以上就是关于为什么我们能看清书本上的字属于漫反射全部的内容,包括:为什么我们能看清书本上的字属于漫反射、WPF中哪种组件可以实现以下文字显示效果、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)