1.Canvas面板

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using System.Windows.Input;
  6. using System.Windows.Shapes;
  7.  
  8. namespace LY.PaintTheButton
  9. {
  10. public class PaintTheButton:Window
  11. {
  12. [STAThread]
  13. public static void Main()
  14. {
  15. new Application().Run(new PaintTheButton());
  16. }
  17. public PaintTheButton()
  18. {
  19. Title = "Paint The Button";
  20. Button btn = new Button();
  21. btn.HorizontalAlignment = HorizontalAlignment.Center;
  22. btn.VerticalAlignment = VerticalAlignment.Center;
  23. Content = btn;
  24.  
  25. Canvas canv = new Canvas();
  26. canv.Width = 144;
  27. canv.Height = 144;
  28. btn.Content = canv;
  29.  
  30. Rectangle rect = new Rectangle();
  31. rect.Width = canv.Width;
  32. rect.Height = canv.Height;
  33. //使矩形的角变圆
  34. rect.RadiusX = 24;
  35. rect.RadiusY = 24;
  36. rect.Fill = Brushes.Blue;
  37. canv.Children.Add(rect);
  38. //根据坐标位置放置控件
  39. Canvas.SetLeft(rect, 0);
  40. Canvas.SetTop(rect, 0);
  41.  
  42. Polygon poly = new Polygon();
  43. poly.Fill = Brushes.Yellow;
  44. //颜色填充规则
  45. poly.FillRule = FillRule.Nonzero;
  46. //poly.Points=new PointCollection();
  47. for (int i = 0; i < 5; i++)
  48. {
  49. double angle = i * 4 * Math.PI / 5;
  50. Point pt = new Point(48 * Math.Sin(angle), -48 * Math.Cos(angle));
  51. poly.Points.Add(pt);
  52. }
  53. canv.Children.Add(poly);
  54. Canvas.SetLeft(poly, canv.Width / 2);
  55. Canvas.SetTop(poly, canv.Height / 2);
  56. }
  57. }
  58. }

  Canvas面板是根据坐标放置控件。

2.UniformGrid面板

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Controls.Primitives;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. using System.Windows.Threading;
  8.  
  9. namespace LY.Puzzle
  10. {
  11. public class PlayPuzzle : Window
  12. {
  13. int xEmpty, yEmpty, iCounter;
  14. UniformGrid unigrid;
  15. Random rand;
  16. [STAThread]
  17. public static void Main()
  18. {
  19. new Application().Run(new PlayPuzzle());
  20. }
  21. public PlayPuzzle()
  22. {
  23. Title = "LY的小游戏";
  24. SizeToContent = SizeToContent.WidthAndHeight;
  25. ResizeMode = ResizeMode.CanMinimize;
  26. Background = SystemColors.ControlBrush;
  27.  
  28. //加入一个Stack面板
  29. StackPanel stack = new StackPanel();
  30. Content = stack;
  31.  
  32. //加入按钮
  33. Button btn = new Button();
  34. btn.Content = "打乱(_S)";
  35. btn.HorizontalAlignment = HorizontalAlignment.Center;
  36. btn.Margin = new Thickness(10);
  37. btn.Click += btn_Click;
  38. stack.Children.Add(btn);
  39.  
  40. //加入边框
  41. Border bord = new Border();
  42. bord.BorderThickness = new Thickness(1);
  43. bord.BorderBrush = SystemColors.ControlDarkDarkBrush;
  44. stack.Children.Add(bord);
  45.  
  46. //加入UniformGrid面板
  47. unigrid = new UniformGrid();
  48. unigrid.Rows = 4;
  49. unigrid.Columns = 4;
  50. bord.Child = unigrid;
  51.  
  52. for (int i = 0; i < 15; i++)
  53. {
  54. Tile tile = new Tile();
  55. tile.Text = (i + 1).ToString();
  56. tile.MouseDown += tile_MouseDown;
  57. unigrid.Children.Add(tile);
  58. }
  59. unigrid.Children.Add(new Empty());
  60. xEmpty = 3;
  61. yEmpty = 3;
  62. }
  63.  
  64. private void btn_Click(object sender, RoutedEventArgs e)
  65. {
  66. rand = new Random();
  67. iCounter = 16 * 4 * 4;
  68. DispatcherTimer tim = new DispatcherTimer();
  69. tim.Interval = TimeSpan.FromMilliseconds(10);
  70. tim.Tick += tim_Tick;
  71. tim.Start();
  72. }
  73.  
  74. void tim_Tick(object sender, EventArgs e)
  75. {
  76. for (int i = 0; i < 5; i++)
  77. {
  78. MoveTile(xEmpty, yEmpty + rand.Next(3) - 1);
  79. MoveTile(xEmpty + rand.Next(3) - 1, yEmpty);
  80. }
  81. iCounter--;
  82. if (iCounter == 0)
  83. (sender as DispatcherTimer).Stop();
  84.  
  85. }
  86.  
  87. private void tile_MouseDown(object sender, MouseButtonEventArgs e)
  88. {
  89. Tile tile = sender as Tile;
  90. int yTile = unigrid.Children.IndexOf(tile) / 4;
  91. int xTile = unigrid.Children.IndexOf(tile) % 4;
  92. //可以一次移动多个tile,所以用while循环
  93. if (xTile == xEmpty)
  94. while (yTile != yEmpty)
  95. MoveTile(xTile, yEmpty + (yTile - yEmpty) / Math.Abs(yTile - yEmpty));
  96. if (yTile == yEmpty)
  97. while (xTile != xEmpty)
  98. MoveTile(xEmpty + (xTile - xEmpty) / Math.Abs(xTile - xEmpty), yTile);
  99. }
  100.  
  101. private void MoveTile(int xTile, int yTile)
  102. {
  103. //为tim_Tick事件剔除无用值
  104. if ((xTile == xEmpty && yTile == yEmpty) || xTile < 0 ||
  105. xTile > 3 || yTile < 0 || yTile > 3) return;
  106. int iTile = yTile * 4 + xTile;
  107. int iEmpty = yEmpty * 4 + xEmpty;
  108.  
  109. UIElement elTile = unigrid.Children[iTile];
  110. UIElement elEmpty = unigrid.Children[iEmpty];
  111. unigrid.Children.Remove(elTile);
  112. unigrid.Children.Insert(iEmpty, elTile);
  113. unigrid.Children.Remove(elEmpty);
  114. unigrid.Children.Insert(iTile, elEmpty);
  115. xEmpty = xTile;
  116. yEmpty = yTile;
  117. }
  118. protected override void OnKeyDown(KeyEventArgs e)
  119. {
  120. base.OnKeyDown(e);
  121. switch (e.Key)
  122. {
  123. case Key.Right: MoveTile(xEmpty - 1, yEmpty); break;
  124. case Key.Left: MoveTile(xEmpty + 1, yEmpty); break;
  125. case Key.Down: MoveTile(xEmpty, yEmpty - 1); break;
  126. case Key.Up: MoveTile(xEmpty, yEmpty + 1); break;
  127. }
  128. }
  129.  
  130. }
  131. }

  

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using System.Windows.Shapes;
  7.  
  8. namespace LY.Puzzle
  9. {
  10. public class Tile : Canvas
  11. {
  12. const int Bord = 6;
  13. const int Size = 64;
  14. TextBlock txtblk;
  15.  
  16. public Tile()
  17. {
  18. Width = Size;
  19. Height = Size;
  20.  
  21. //左上多边形
  22. Polygon poly = new Polygon();
  23. poly.Points = new PointCollection(new Point[]{
  24. new Point(0,0),new Point(Size,0),new Point(Size-Bord,Bord),
  25. new Point(Bord,Bord),new Point(Bord,Size-Bord),
  26. new Point(0,Size)});
  27. poly.Fill = SystemColors.ControlLightLightBrush;
  28. Children.Add(poly);
  29. //poly本身带着坐标信息,因此不用再设定在面板中的位置
  30. //Canvas.SetLeft(poly, 0);
  31. //Canvas.SetTop(poly, 0);
  32.  
  33. //右下多边形
  34. poly = new Polygon();
  35. poly.Points = new PointCollection(new Point[]{
  36. new Point(Size,Size),new Point(0,Size),new Point(Bord,Size-Bord),
  37. new Point(Size-Bord,Size-Bord),new Point(Size-Bord,Bord),
  38. new Point(Size,0)});
  39. poly.Fill = SystemColors.ControlDarkBrush;
  40. Children.Add(poly);
  41. //很多图形对象本身带着坐标信息
  42. //Canvas.SetRight(poly, 0);
  43. //Canvas.SetBottom(poly, 0);
  44.  
  45. //在元素周围绘制边框或背景
  46. Border border = new Border();
  47. border.Width = Size - 2 * Bord;
  48. border.Height = Size - 2 * Bord;
  49. //border.BorderThickness = new Thickness(6);
  50. border.Background = SystemColors.ControlBrush;
  51. Children.Add(border);
  52. //这里以下两种方式都可以
  53. SetLeft(border, Bord);
  54. Canvas.SetTop(border, Bord);
  55.  
  56. //中间放上TextBlock控件
  57. txtblk = new TextBlock();
  58. txtblk.FontSize = 32d;
  59. txtblk.Foreground = SystemColors.ControlTextBrush;
  60. txtblk.HorizontalAlignment = HorizontalAlignment.Center;
  61. txtblk.VerticalAlignment = VerticalAlignment.Center;
  62. border.Child = txtblk;
  63. }
  64. public string Text
  65. {
  66. get { return txtblk.Text; }
  67. set { txtblk.Text = value; }
  68. }
  69. }
  70.  
  71. //空白格子类
  72. class Empty : FrameworkElement
  73. {
  74. }
  75. }

  UniformGrid面板类似于Grid面板,但它是固定行列大小的,用Rows、Columns直接指定行数和列数即可。

《WPF程序设计指南》读书笔记——第7章 Canvas的更多相关文章

  1. css权威指南读书笔记-第10章浮动和定位

    这一章看了之后真是豁然开朗,之前虽然写了圣杯布局和双飞翼布局,有些地方也是模糊的,现在打算总结之后再写一遍. 以下都是从<css权威指南>中摘抄的我认为很有用的说明. 浮动元素 一个元素浮 ...

  2. 《Javascript高级程序设计》读书笔记(1-3章)

    第一章 JavaScript简介 1.1 JavaScript简史 略 1.2 JavaScript实现 虽然 JavaScript 和 ECMAScript 通常都被人们用来表达相同的含义,但 Ja ...

  3. JavaScript权威指南读书笔记【第一章】

    第一章 JavaScript概述 前端三大技能: HTML: 描述网页内容 CSS: 描述网页样式 JavaScript: 描述网页行为 特点:动态.弱类型.适合面向对象和函数式编程的风格 语法源自J ...

  4. 《JavaScript高级程序设计》 - 读书笔记 - 第5章 引用类型

    5.1 Object 类型 对象是引用类型的实例.引用类型是一种数据结构,用于将数据和功能组织在一起. 新对象是使用new操作符后跟一个构造函数来创建的.构造函数本身就是一个函数,只不过该函数是出于创 ...

  5. 《JavaScript高级程序设计》 - 读书笔记 - 第4章 变量、作用域和内存问题

    4.1 基本类型和引用类型的值 JavaScript变量是松散类型的,它只是保存特定值的一个名字而已. ECMAScript变量包含两种数据类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据 ...

  6. 《Linux程序设计》--读书笔记---第十三章进程间通信:管道

    管道:进程可以通过它交换更有用的数据. 我们通常是把一个进程的输出通过管道连接到另一个进程的输入: 对shell命令来说,命令的连接是通过管道字符来完成的: cmd1    |     cmd2 sh ...

  7. 《Visual C++ 程序设计》读书笔记 ----第8章 指针和引用

    1.&取地址:*取内容. 2.指针变量“++”“--”,并不是指针变量的值加1或减1,而是使指针变量指向下一个或者上一个元素. 3.指针运算符*与&的优先级相同,左结合:++,--,* ...

  8. 《Linux内核设计与实现》第八周读书笔记——第四章 进程调度

    <Linux内核设计与实现>第八周读书笔记——第四章 进程调度 第4章 进程调度35 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配 ...

  9. 《Linux内核设计与分析》第六周读书笔记——第三章

    <Linux内核设计与实现>第六周读书笔记——第三章 20135301张忻估算学习时间:共2.5小时读书:2.0代码:0作业:0博客:0.5实际学习时间:共3.0小时读书:2.0代码:0作 ...

随机推荐

  1. poj1068解题报告(模拟类)

    POJ 1068,题目链接http://poj.org/problem?id=1068 题意: 对于给出给出的原括号串S,对应两种数字密码串P.W: S         (((()()()))) P- ...

  2. 火狐restclient

    RESTClient是一款用于测试各种Web服务的插件,它可以向服务器发送各种HTTP请求(用户也可以自定义请求方式),并显示服务器响应.使用RESTClient您可以方便的测试各种Web服务,为您的 ...

  3. python(1) - 输入和输出

    前面已经说过了,print()函数括号里加上字符串,就可以实现输出 >>> print('This is Python!') This is Python! print()函数也可以 ...

  4. css实现居中

    --在html常常用到居中 --1.可以用<center></center> --2.可以用css 演示代码: <!DOCTYPE html PUBLIC "- ...

  5. 关于js中event的target和currentTarget的区别

    今天又遇到这个问题了,总是搞不清楚target和currentTarget的区别,百度搜索的时候看到一遍文章解释得很清楚,特意记录下录,以备不时之需: target与currentTarget的区别? ...

  6. vim 编辑器 打开GB2312、GBK文件乱码解决方法

    安装好的操作系统一般都带有vim编辑器,但是默认不支持GB2312中文,打开文件出现乱码,解决办法如下. 1.打开以下文件 sudo vim /var/lib/locales/supported.d/ ...

  7. HDU 2181 哈密顿绕行世界问题 (DFS)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. css3 transition 实现图片放大缩小

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Lombok(1.14.8) - @NonNull

    @NonNull @NonNull,生成一个非空检查. package com.huey.lombok; import lombok.Getter; import lombok.NonNull; im ...

  10. Linux 命令 - mkdir: 创建目录

    命令格式 mkdir [OPTION]... DIRECTORY... 命令参数 -m, --mode=MODE 设置文件的模式,类似于 chmod 命令. -p, --parents 需要时创建指定 ...