《WPF程序设计指南》读书笔记——第7章 Canvas
1.Canvas面板
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Input;
- using System.Windows.Shapes;
- namespace LY.PaintTheButton
- {
- public class PaintTheButton:Window
- {
- [STAThread]
- public static void Main()
- {
- new Application().Run(new PaintTheButton());
- }
- public PaintTheButton()
- {
- Title = "Paint The Button";
- Button btn = new Button();
- btn.HorizontalAlignment = HorizontalAlignment.Center;
- btn.VerticalAlignment = VerticalAlignment.Center;
- Content = btn;
- Canvas canv = new Canvas();
- canv.Width = 144;
- canv.Height = 144;
- btn.Content = canv;
- Rectangle rect = new Rectangle();
- rect.Width = canv.Width;
- rect.Height = canv.Height;
- //使矩形的角变圆
- rect.RadiusX = 24;
- rect.RadiusY = 24;
- rect.Fill = Brushes.Blue;
- canv.Children.Add(rect);
- //根据坐标位置放置控件
- Canvas.SetLeft(rect, 0);
- Canvas.SetTop(rect, 0);
- Polygon poly = new Polygon();
- poly.Fill = Brushes.Yellow;
- //颜色填充规则
- poly.FillRule = FillRule.Nonzero;
- //poly.Points=new PointCollection();
- for (int i = 0; i < 5; i++)
- {
- double angle = i * 4 * Math.PI / 5;
- Point pt = new Point(48 * Math.Sin(angle), -48 * Math.Cos(angle));
- poly.Points.Add(pt);
- }
- canv.Children.Add(poly);
- Canvas.SetLeft(poly, canv.Width / 2);
- Canvas.SetTop(poly, canv.Height / 2);
- }
- }
- }
Canvas面板是根据坐标放置控件。
2.UniformGrid面板
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Threading;
- namespace LY.Puzzle
- {
- public class PlayPuzzle : Window
- {
- int xEmpty, yEmpty, iCounter;
- UniformGrid unigrid;
- Random rand;
- [STAThread]
- public static void Main()
- {
- new Application().Run(new PlayPuzzle());
- }
- public PlayPuzzle()
- {
- Title = "LY的小游戏";
- SizeToContent = SizeToContent.WidthAndHeight;
- ResizeMode = ResizeMode.CanMinimize;
- Background = SystemColors.ControlBrush;
- //加入一个Stack面板
- StackPanel stack = new StackPanel();
- Content = stack;
- //加入按钮
- Button btn = new Button();
- btn.Content = "打乱(_S)";
- btn.HorizontalAlignment = HorizontalAlignment.Center;
- btn.Margin = new Thickness(10);
- btn.Click += btn_Click;
- stack.Children.Add(btn);
- //加入边框
- Border bord = new Border();
- bord.BorderThickness = new Thickness(1);
- bord.BorderBrush = SystemColors.ControlDarkDarkBrush;
- stack.Children.Add(bord);
- //加入UniformGrid面板
- unigrid = new UniformGrid();
- unigrid.Rows = 4;
- unigrid.Columns = 4;
- bord.Child = unigrid;
- for (int i = 0; i < 15; i++)
- {
- Tile tile = new Tile();
- tile.Text = (i + 1).ToString();
- tile.MouseDown += tile_MouseDown;
- unigrid.Children.Add(tile);
- }
- unigrid.Children.Add(new Empty());
- xEmpty = 3;
- yEmpty = 3;
- }
- private void btn_Click(object sender, RoutedEventArgs e)
- {
- rand = new Random();
- iCounter = 16 * 4 * 4;
- DispatcherTimer tim = new DispatcherTimer();
- tim.Interval = TimeSpan.FromMilliseconds(10);
- tim.Tick += tim_Tick;
- tim.Start();
- }
- void tim_Tick(object sender, EventArgs e)
- {
- for (int i = 0; i < 5; i++)
- {
- MoveTile(xEmpty, yEmpty + rand.Next(3) - 1);
- MoveTile(xEmpty + rand.Next(3) - 1, yEmpty);
- }
- iCounter--;
- if (iCounter == 0)
- (sender as DispatcherTimer).Stop();
- }
- private void tile_MouseDown(object sender, MouseButtonEventArgs e)
- {
- Tile tile = sender as Tile;
- int yTile = unigrid.Children.IndexOf(tile) / 4;
- int xTile = unigrid.Children.IndexOf(tile) % 4;
- //可以一次移动多个tile,所以用while循环
- if (xTile == xEmpty)
- while (yTile != yEmpty)
- MoveTile(xTile, yEmpty + (yTile - yEmpty) / Math.Abs(yTile - yEmpty));
- if (yTile == yEmpty)
- while (xTile != xEmpty)
- MoveTile(xEmpty + (xTile - xEmpty) / Math.Abs(xTile - xEmpty), yTile);
- }
- private void MoveTile(int xTile, int yTile)
- {
- //为tim_Tick事件剔除无用值
- if ((xTile == xEmpty && yTile == yEmpty) || xTile < 0 ||
- xTile > 3 || yTile < 0 || yTile > 3) return;
- int iTile = yTile * 4 + xTile;
- int iEmpty = yEmpty * 4 + xEmpty;
- UIElement elTile = unigrid.Children[iTile];
- UIElement elEmpty = unigrid.Children[iEmpty];
- unigrid.Children.Remove(elTile);
- unigrid.Children.Insert(iEmpty, elTile);
- unigrid.Children.Remove(elEmpty);
- unigrid.Children.Insert(iTile, elEmpty);
- xEmpty = xTile;
- yEmpty = yTile;
- }
- protected override void OnKeyDown(KeyEventArgs e)
- {
- base.OnKeyDown(e);
- switch (e.Key)
- {
- case Key.Right: MoveTile(xEmpty - 1, yEmpty); break;
- case Key.Left: MoveTile(xEmpty + 1, yEmpty); break;
- case Key.Down: MoveTile(xEmpty, yEmpty - 1); break;
- case Key.Up: MoveTile(xEmpty, yEmpty + 1); break;
- }
- }
- }
- }
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Shapes;
- namespace LY.Puzzle
- {
- public class Tile : Canvas
- {
- const int Bord = 6;
- const int Size = 64;
- TextBlock txtblk;
- public Tile()
- {
- Width = Size;
- Height = Size;
- //左上多边形
- Polygon poly = new Polygon();
- poly.Points = new PointCollection(new Point[]{
- new Point(0,0),new Point(Size,0),new Point(Size-Bord,Bord),
- new Point(Bord,Bord),new Point(Bord,Size-Bord),
- new Point(0,Size)});
- poly.Fill = SystemColors.ControlLightLightBrush;
- Children.Add(poly);
- //poly本身带着坐标信息,因此不用再设定在面板中的位置
- //Canvas.SetLeft(poly, 0);
- //Canvas.SetTop(poly, 0);
- //右下多边形
- poly = new Polygon();
- poly.Points = new PointCollection(new Point[]{
- new Point(Size,Size),new Point(0,Size),new Point(Bord,Size-Bord),
- new Point(Size-Bord,Size-Bord),new Point(Size-Bord,Bord),
- new Point(Size,0)});
- poly.Fill = SystemColors.ControlDarkBrush;
- Children.Add(poly);
- //很多图形对象本身带着坐标信息
- //Canvas.SetRight(poly, 0);
- //Canvas.SetBottom(poly, 0);
- //在元素周围绘制边框或背景
- Border border = new Border();
- border.Width = Size - 2 * Bord;
- border.Height = Size - 2 * Bord;
- //border.BorderThickness = new Thickness(6);
- border.Background = SystemColors.ControlBrush;
- Children.Add(border);
- //这里以下两种方式都可以
- SetLeft(border, Bord);
- Canvas.SetTop(border, Bord);
- //中间放上TextBlock控件
- txtblk = new TextBlock();
- txtblk.FontSize = 32d;
- txtblk.Foreground = SystemColors.ControlTextBrush;
- txtblk.HorizontalAlignment = HorizontalAlignment.Center;
- txtblk.VerticalAlignment = VerticalAlignment.Center;
- border.Child = txtblk;
- }
- public string Text
- {
- get { return txtblk.Text; }
- set { txtblk.Text = value; }
- }
- }
- //空白格子类
- class Empty : FrameworkElement
- {
- }
- }
UniformGrid面板类似于Grid面板,但它是固定行列大小的,用Rows、Columns直接指定行数和列数即可。
《WPF程序设计指南》读书笔记——第7章 Canvas的更多相关文章
- css权威指南读书笔记-第10章浮动和定位
这一章看了之后真是豁然开朗,之前虽然写了圣杯布局和双飞翼布局,有些地方也是模糊的,现在打算总结之后再写一遍. 以下都是从<css权威指南>中摘抄的我认为很有用的说明. 浮动元素 一个元素浮 ...
- 《Javascript高级程序设计》读书笔记(1-3章)
第一章 JavaScript简介 1.1 JavaScript简史 略 1.2 JavaScript实现 虽然 JavaScript 和 ECMAScript 通常都被人们用来表达相同的含义,但 Ja ...
- JavaScript权威指南读书笔记【第一章】
第一章 JavaScript概述 前端三大技能: HTML: 描述网页内容 CSS: 描述网页样式 JavaScript: 描述网页行为 特点:动态.弱类型.适合面向对象和函数式编程的风格 语法源自J ...
- 《JavaScript高级程序设计》 - 读书笔记 - 第5章 引用类型
5.1 Object 类型 对象是引用类型的实例.引用类型是一种数据结构,用于将数据和功能组织在一起. 新对象是使用new操作符后跟一个构造函数来创建的.构造函数本身就是一个函数,只不过该函数是出于创 ...
- 《JavaScript高级程序设计》 - 读书笔记 - 第4章 变量、作用域和内存问题
4.1 基本类型和引用类型的值 JavaScript变量是松散类型的,它只是保存特定值的一个名字而已. ECMAScript变量包含两种数据类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据 ...
- 《Linux程序设计》--读书笔记---第十三章进程间通信:管道
管道:进程可以通过它交换更有用的数据. 我们通常是把一个进程的输出通过管道连接到另一个进程的输入: 对shell命令来说,命令的连接是通过管道字符来完成的: cmd1 | cmd2 sh ...
- 《Visual C++ 程序设计》读书笔记 ----第8章 指针和引用
1.&取地址:*取内容. 2.指针变量“++”“--”,并不是指针变量的值加1或减1,而是使指针变量指向下一个或者上一个元素. 3.指针运算符*与&的优先级相同,左结合:++,--,* ...
- 《Linux内核设计与实现》第八周读书笔记——第四章 进程调度
<Linux内核设计与实现>第八周读书笔记——第四章 进程调度 第4章 进程调度35 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配 ...
- 《Linux内核设计与分析》第六周读书笔记——第三章
<Linux内核设计与实现>第六周读书笔记——第三章 20135301张忻估算学习时间:共2.5小时读书:2.0代码:0作业:0博客:0.5实际学习时间:共3.0小时读书:2.0代码:0作 ...
随机推荐
- poj1068解题报告(模拟类)
POJ 1068,题目链接http://poj.org/problem?id=1068 题意: 对于给出给出的原括号串S,对应两种数字密码串P.W: S (((()()()))) P- ...
- 火狐restclient
RESTClient是一款用于测试各种Web服务的插件,它可以向服务器发送各种HTTP请求(用户也可以自定义请求方式),并显示服务器响应.使用RESTClient您可以方便的测试各种Web服务,为您的 ...
- python(1) - 输入和输出
前面已经说过了,print()函数括号里加上字符串,就可以实现输出 >>> print('This is Python!') This is Python! print()函数也可以 ...
- css实现居中
--在html常常用到居中 --1.可以用<center></center> --2.可以用css 演示代码: <!DOCTYPE html PUBLIC "- ...
- 关于js中event的target和currentTarget的区别
今天又遇到这个问题了,总是搞不清楚target和currentTarget的区别,百度搜索的时候看到一遍文章解释得很清楚,特意记录下录,以备不时之需: target与currentTarget的区别? ...
- vim 编辑器 打开GB2312、GBK文件乱码解决方法
安装好的操作系统一般都带有vim编辑器,但是默认不支持GB2312中文,打开文件出现乱码,解决办法如下. 1.打开以下文件 sudo vim /var/lib/locales/supported.d/ ...
- HDU 2181 哈密顿绕行世界问题 (DFS)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- css3 transition 实现图片放大缩小
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Lombok(1.14.8) - @NonNull
@NonNull @NonNull,生成一个非空检查. package com.huey.lombok; import lombok.Getter; import lombok.NonNull; im ...
- Linux 命令 - mkdir: 创建目录
命令格式 mkdir [OPTION]... DIRECTORY... 命令参数 -m, --mode=MODE 设置文件的模式,类似于 chmod 命令. -p, --parents 需要时创建指定 ...