可视对象和棋子
任务1.6的实现和画棋纵横线没有很大的差别,设置好字体、要显示的坐标的文字,然后调用DrawingContext的DrawText方法在指定的位置画出对象即可。为了满足任务中的要求,我们设置一个开关字段和对应的属性,代码如下:
public partial class BoardControl : Canvas
{
double scale = 0.03;
readonly int[,] demarkCount ={ { 2, 3 }, { 3, 3 }, { 3, 4 }, { 3, 5 }, { 3, 6 } };
bool showCoordinate = true;
Brush coordinateColor = Brushes.White;
……
public bool ShowCoordinate
{
get { return showCoordinate; }
set
{
if (showCoordinate != value)
{
showCoordinate = value;
InvalidateVisual();
}
}
}
……..
protected override void OnRender(DrawingContext dc)
{
……
if (showCoordinate)
{
double fontSize = 0.4;
for (int i = 1; i <= BoardSize; i++)
{
FormattedText fmtText = new FormattedText(i.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), fontSize, coordinateColor);
fmtText.TextAlignment = TextAlignment.Right;
dc.DrawText(fmtText, new Point(-0.5, (i - 1 - fmtText.Height * .5)));
fmtText =new FormattedText(((char)(i + 64)).ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), fontSize, coordinateColor);
fmtText.TextAlignment = TextAlignment.Center;
dc.DrawText(fmtText, new Point(i - 1, BoardSize - 0.5));
}
}
}
……
编译运行。为了测试显示隐藏坐标的功能,切换到MainWindow的Xaml方式,插入如下代码:
<StackPanelGrid.Row="1"Grid.Column="0"Orientation="Vertical"Margin="10">
……
<CheckBoxName="coordinateCheckBox"Click="ShowCoordinateClick"IsChec[1] [2] [3] [4] [5] [6] 下一页
(责任编辑:笑虎)