WPF学习笔记-第二周【基本笔刷】
生活随笔
收集整理的這篇文章主要介紹了
WPF学习笔记-第二周【基本笔刷】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? 書接上回,這一次,講的是WPF中的基本筆刷,由于是菜鳥,就不多說了,繼續(xù)帖示例代碼:)
?
第一部份 代碼
第二章 基本筆刷
第一個(gè) 示例
?
VaryTheBackgroud P38 ?1?????#region?VaryTheBackgroud?P38?2?????
?3?????///?<summary>
?4?????///?改變背景的示例
?5?????///?通過鼠標(biāo)移動(dòng)事件,根據(jù)鼠標(biāo)對(duì)應(yīng)的窗口中心位置的距離計(jì)算并改變窗口的背景顏色
?6?????///?</summary>
?7?????public?class?VaryTheBackgroud?:?Window
?8?????{
?9?????????///?<summary>
10?????????///?定義一個(gè)?Brush,存放窗口的背景顏色
11?????????///?</summary>
12?????????SolidColorBrush?brush?=?new?SolidColorBrush(Colors.Black);
13?????????///?<summary>
14?????????///?構(gòu)造窗口
15?????????///?</summary>
16?????????public?VaryTheBackgroud()
17?????????{
18?????????????//指定標(biāo)題\高\(yùn)寬和背景顏色
19?????????????this.Title?=?"Vary?The?Backgroud";
20?????????????this.Width?=?384;
21?????????????this.Height?=?384;
22?????????????Background?=?brush;
23?????????}
24?
25?????????[STAThread]
26?????????static?void?Main()
27?????????{
28?????????????//定義并運(yùn)行程序
29?????????????Application?app?=?new?Application();
30?????????????app.Run(new?VaryTheBackgroud());
31?????????}
32?
33?????????///?<summary>
34?????????///?重寫鼠標(biāo)移動(dòng)事件
35?????????///?</summary>
36?????????///?<param?name="e"></param>
37?????????protected?override?void?OnMouseMove(MouseEventArgs?e)
38?????????{
39?????????????//base.OnMouseMove(e);
40?????????????//獲得窗口的工作區(qū)的寬和高(減去加框和標(biāo)題欄后的高寬度)
41?????????????double?width?=?ActualWidth?-?2?*?SystemParameters.ResizeFrameVerticalBorderWidth;
42?????????????double?height?=?ActualHeight?-?2?*?SystemParameters.ResizeFrameHorizontalBorderHeight?-?SystemParameters.CaptionHeight;
43?????????????//當(dāng)前鼠標(biāo)的位置
44?????????????Point?ptMouse=e.GetPosition(this);
45?????????????//窗口工作區(qū)中心點(diǎn)
46?????????????Point?ptCenter=new?Point(width/2,height/2);
47?????????????//鼠標(biāo)相對(duì)工作區(qū)中心的偏移量
48?????????????Vector?vectMouse?=?ptMouse?-?ptCenter;
49?????????????//計(jì)算角度
50?????????????double?angle?=?Math.Atan2(vectMouse.Y,?vectMouse.X);
51?????????????//計(jì)算一個(gè)圓形的區(qū)域
52?????????????Vector?vectEllipse?=?new?Vector(width?/?2?*?Math.Cos(angle),?height?/?2?*?Math.Sin(angle));
53?????????????//計(jì)算鼠標(biāo)的位置與圓形區(qū)域的比例,并*255?以轉(zhuǎn)換成顏色值
54?????????????Byte?byLevel?=?(byte)(255?*?(1?-?Math.Min(1,?vectMouse.Length?/?vectEllipse.Length)));
55?????????????//修改brush?的顏色
56?????????????Color?clr=brush.Color;
57?
58?????????????clr.R=clr.G=clr.B=byLevel;
59?
60?????????????brush.Color=clr;
61?????????}
62?????}
63?
64?????//相關(guān)知識(shí):
65?????//1、Brush?的改變后會(huì)自動(dòng)重畫窗口,
66?????//原因是因繼承自Freezable類,該類實(shí)現(xiàn)了一個(gè)名為Changed的事件,會(huì)在發(fā)生改變后被觸發(fā)。
67?????//2、Colors?定義了141種顏色,Brushs也同樣的定義了同名的141種畫刷
68?????//3、如果直接?SolidColorBrush?brush?=Brushs.Black?后再來指定顏色則會(huì)報(bào)錯(cuò),因由?Brushs?所得的畫刷對(duì)象是處于凍結(jié)狀態(tài)的,不能再被改變
69?????//4、被凍結(jié)的Freezable對(duì)象可以在不同的線程之間共享,未被凍結(jié)的則不行。
70?????
71?????#endregion
?
?
第二個(gè)示例
?
FlipTroughTheBrushes P41 ?1?????#region?FlipTroughTheBrushes?P41?2?????
?3?????///?<summary>
?4?????///?使用?Brushes?類定義的?141?種顏色筆刷填充窗口
?5?????///?通過重寫?OnKeyDown?事件按一下下方向鍵,就使用一種顏色的筆刷填充窗口
?6?????///?</summary>
?7?????public?class?FlipThroughTheBushes:Window
?8?????{
?9?????????//定義計(jì)數(shù)器和筆刷組
10?????????int?index?=?0;
11?????????PropertyInfo[]?props;
12?
13?????????[STAThread]
14?????????public?static?void?Main()
15?????????{
16?????????????Application?app?=?new?Application();
17?????????????app.Run(new?FlipThroughTheBushes());
18?????????}
19?
20?????????public?FlipThroughTheBushes()
21?????????{
22?????????????//通過反射獲取?Brushes?類定義的所有公有成員和靜態(tài)成員
23?????????????//BindingFlags?標(biāo)識(shí)成員的類型,Brushes?只有Public?和?static?所以可以不用限制
24?????????????props?=?typeof(Brushes).GetProperties(BindingFlags.Public?|?BindingFlags.Static);
25?????????????//設(shè)置窗口標(biāo)題和背景顏色
26?????????????setTitleAndBackground();
27?????????}
28?
29?????????///?<summary>
30?????????///?重寫?OnKeyDown?事件
31?????????///?</summary>
32?????????///?<param?name="e"></param>
33?????????protected?override?void?OnKeyDown(KeyEventArgs?e)
34?????????{
35?????????????//根據(jù)上下方向鍵的動(dòng)作來設(shè)定計(jì)數(shù)
36?????????????if?(e.Key?==?Key.Down?||?e.Key?==?Key.Up)
37?????????????{
38?????????????????index?+=?e.Key?==?Key.Up???1?:?props.Length?-?1;
39?????????????????
40?????????????????index?%=?props.Length;
41?????????????????//設(shè)置窗口標(biāo)題和背景顏色
42?????????????????setTitleAndBackground();
43?????????????}
44?????????????base.OnKeyDown(e);
45?????????}
46?????????///?<summary>
47?????????///?設(shè)置窗口標(biāo)題和背景顏色
48?????????///?</summary>
49?????????void?setTitleAndBackground()
50?????????{
51?????????????//在標(biāo)題顯示當(dāng)前使用的顏色
52?????????????Title?=?"Flip?Through?the?Brushes?-"?+?props[index].Name;
53?????????????//設(shè)置背景色
54?????????????//PropertyInfo[].GetValue(null,null)?在這里實(shí)際返回是的?SolidColorBrush?對(duì)象
55?????????????Background?=?(Brush)props[index].GetValue(null,?null);
56?????????}
57?????}
58?
59?????//知識(shí):
60?????//1、System.Window?命名空間?有?SystemColors?類其作用相似于?Colors?和?Brushes?只具有靜態(tài)的只讀property,返回Color值和SolidColorBrush對(duì)象。
61?????//2、這些值存儲(chǔ)在Windows的注冊(cè)表中,可以由SystemColors.WindowColor?和?SystemColors.WindowTextBrush?獲得用戶對(duì)窗口工作區(qū)背景和文字顏色的喜好
62?????//3、SystemColors返回的畫刷是凍結(jié)的?可以?Brush?brush=new?SystemColorBrush(SystemColors.WindowColor);?但不能改變?Brush?brush=SystemColors.WindowBrush;?這樣的畫刷
63?????//4、繼承自Freezable?的類對(duì)象有凍結(jié)的問題,但Color?是結(jié)構(gòu)體,沒有凍結(jié)的問題。
64?????//5、SolidColorBrush?是單色畫刷,漸變畫刷是:?LinearGradientBrush
65?????
66?????#endregion
?
?
第三個(gè)示例
?
GradilteTheBrush P43 ?1?????#region?GradilteTheBrush?P43?2?????
?3?????///?<summary>
?4?????///?以漸變色填充窗口工作區(qū)
?5?????///?</summary>
?6?????public?class?GradialteTheBrush?:?Window
?7?????{
?8?????????[STAThread]
?9?????????public?static?void?Main()
10?????????{
11?????????????Application?app?=?new?Application();
12?????????????app.Run(new?GradialteTheBrush());
13?????????}
14?????????public?GradialteTheBrush()
15?????????{
16?????????????Title?=?"Gradiate?The?Brush";
17?????????????//?定義一個(gè)漸變色的畫刷?,LinearGradientBrush中?從?Point(0,?0)到?Point(1,?1)就是指從窗口工作區(qū)的右上角到左下角
18?????????????//LinearGradientBrush?brush?=?new?LinearGradientBrush(Colors.Red,?Colors.Blue,?new?Point(0,?0),?new?Point(1,?1));
19????????????????????????
20?
21?????????????LinearGradientBrush?brush?=?new?LinearGradientBrush(Colors.Red,?Colors.Blue,?new?Point(0,?0),?new?Point(0.25,?0.25));
22?????????????brush.SpreadMethod?=?GradientSpreadMethod.Reflect;
23?
24?????????????//指定為背景色
25?????????????Background?=?brush;
26?????????}
27?
28?????}
29?
30?????//知識(shí):
31?????//1、WPF中的點(diǎn)是與設(shè)備無關(guān)的,WPF漸變畫刷有特性是讓你不用基于窗口尺寸來調(diào)整的點(diǎn),默認(rèn)情況下你指定的點(diǎn)是相對(duì)于表面的,這里的表面被視為一個(gè)單位寬一個(gè)單位高,即左上角為(0,0),右下角為(1,1),左下角為(0,1)?右下角為(1,0)
32?????//2、LinearGradientBrush?設(shè)置的漸變色效果是以兩點(diǎn)進(jìn)行連線,連線中點(diǎn)的顏色就是兩點(diǎn)顏色的平均值,垂直與連線的線上的點(diǎn)顏色是相同的
33?????//3、LinearGradientBrush?的重載版本中可以設(shè)置360度角度(angle)進(jìn)行漸變填充?如?new?LinearGradientBrush(Color1,?Color2,?angle)
34?????//4、LinearGradientBrush?的重載版本中垂直漸變填充?如?new?LinearGradientBrush(Color1,?Color2,?new?Point(0,?0),?new?Point(1,?1))
35?????//5、LinearGradientBrush?的重載版本中任意角度漸變填充?如?new?LinearGradientBrush(Color1,?Color2,?new?Point(0,?0),?new?Point(cos(angle),sin(angle)));
36????
37?????#endregion
?
?
第四個(gè)示例
?
AdjustTheBrush P47 ?1?????#region?AdjustTheBrush?P47?2?????
?3?????///?<summary>
?4?????///?對(duì)角線為漸變中線的漸變畫刷填充
?5?????///?</summary>
?6?????public?class?AdjustTheGradient?:?Window
?7?????{
?8?????????LinearGradientBrush?brush;
?9?
10?????????[STAThread]
11?????????static?void?Main()
12?????????{
13?????????????Application?app?=?new?Application();
14?????????????app.Run(new?AdjustTheGradient());
15?????????}
16?
17?????????public?AdjustTheGradient()
18?????????{
19?????????????Title?=?"Adjust?The?Gradient";
20?????????????SizeChanged?+=?WindowOnSizeSizeChanged;
21?????????????brush?=?new?LinearGradientBrush(Colors.Red,?Colors.Blue,?0);
22?????????????brush.MappingMode?=?BrushMappingMode.Absolute;
23?????????????Background?=?brush;
24?????????}
25?
26?????????void?WindowOnSizeSizeChanged(object?sender,?SizeChangedEventArgs?e)
27?????????{
28?????????????double?width?=?ActualWidth?-?2?*?SystemParameters.ResizeFrameVerticalBorderWidth;
29?????????????double?height?=?ActualHeight?-?2?*?SystemParameters.ResizeFrameHorizontalBorderHeight?-?SystemParameters.CaptionHeight;
30?
31?????????????Point?ptCenter?=?new?Point(width?/?2,?height?/?2);
32?????????????Vector?vectDiag?=?new?Vector(width,?-height);
33?????????????Vector?vectperp?=?new?Vector(vectDiag.Y,?-vectDiag.X);
34?
35?????????????vectperp.Normalize();
36?????????????vectperp?*=?width?*?height?/?vectDiag.Length;
37?
38?????????????brush.StartPoint?=?ptCenter?+?vectperp;
39?????????????brush.EndPoint?=?ptCenter?-?vectperp;
40?????????}
41?
42?????}
43?????//知識(shí):
44?????//1、??StartPoint?和?是LinearGradientBrush僅有的兩個(gè)?property
45?????//2、??LinearGradientBrush?可以使用?GradientBrush?定義的GradinetStops?property?設(shè)置更多的顏色
46?????//3、?new?GradientStop(color1,offset);
47?????
48?????#endregion
?
?
第五個(gè)示例
?
FollowTheRainbow P48 ?1?????#region?FollowTheRainbow?P48?2?????
?3?????///?<summary>
?4?????///?彩虹顏色的窗體
?5?????///?</summary>
?6?????class?FollowTheRainbow:Window
?7?????{
?8?????????[STAThread]
?9?????????static?void?Main()
10?????????{
11?????????????Application?app?=?new?Application();
12?????????????app.Run(new?FollowTheRainbow());
13?????????}
14?
15?????????public?FollowTheRainbow()
16?????????{
17?????????????Title?=?"Follow?The?Rainbow";
18?????????????LinearGradientBrush?brush?=?new?LinearGradientBrush();
19?????????????brush.StartPoint?=?new?Point(0,?0);
20?????????????brush.EndPoint?=?new?Point(1,?0);
21?
22?????????????Background?=?brush;
23?
24?????????????//?采用Rey?G.Biv?的彩虹記住系統(tǒng)
25?????????????brush.GradientStops.Add(new?GradientStop(Colors.Red,?0));
26?????????????brush.GradientStops.Add(new?GradientStop(Colors.Orange,?.17));
27?????????????brush.GradientStops.Add(new?GradientStop(Colors.Yellow,.33));
28?????????????brush.GradientStops.Add(new?GradientStop(Colors.Green,?.5));
29?????????????brush.GradientStops.Add(new?GradientStop(Colors.Blue,?.67));
30?????????????brush.GradientStops.Add(new?GradientStop(Colors.Indigo,?.84));
31?????????????brush.GradientStops.Add(new?GradientStop(Colors.Violet,?1));
32?????????}
33?
34?????}
35?????
36?????#endregion
?
?
第六個(gè)示例
?
FollowTheRainbow P49 ?1?????#region?FollowTheRainbow?P49?2?????
?3?????///?<summary>
?4?????///?由內(nèi)向處的彩虹顏色的窗體
?5?????///?</summary>
?6?????class?ClrcleTheRainbow?:?Window
?7?????{
?8?????????[STAThread]
?9?????????static?void?Main()
10?????????{
11?????????????Application?app?=?new?Application();
12?????????????app.Run(new?ClrcleTheRainbow());
13?????????}
14?
15?????????public?ClrcleTheRainbow()
16?????????{
17?????????????Title?=?"Follow?The?Rainbow";
18?????????????RadialGradientBrush?brush?=?new?RadialGradientBrush();
19?????????????Background?=?brush;
20?
21?????????????//?采用Rey?G.Biv?的彩虹記住系統(tǒng)
22?????????????brush.GradientStops.Add(new?GradientStop(Colors.Red,?0));
23?????????????brush.GradientStops.Add(new?GradientStop(Colors.Orange,?.17));
24?????????????brush.GradientStops.Add(new?GradientStop(Colors.Yellow,?.33));
25?????????????brush.GradientStops.Add(new?GradientStop(Colors.Green,?.5));
26?????????????brush.GradientStops.Add(new?GradientStop(Colors.Blue,?.67));
27?????????????brush.GradientStops.Add(new?GradientStop(Colors.Indigo,?.84));
28?????????????brush.GradientStops.Add(new?GradientStop(Colors.Violet,?1));
29?????????}
30?
31?????}
32?????
33?????#endregion
?
?
第七個(gè)示例
?
FollowTheRainbow P51 ?1?????#region?FollowTheRainbow?P51?2?????
?3?????///?<summary>
?4?????///?同心圓的漸變
?5?????///?</summary>
?6?????class?ClickTheGradientCenter?:?Window
?7?????{
?8?????????[STAThread]
?9?????????static?void?Main()
10?????????{
11?????????????Application?app?=?new?Application();
12?????????????app.Run(new?ClickTheGradientCenter());
13?????????}
14?????????RadialGradientBrush?brush;
15?????????public?ClickTheGradientCenter()
16?????????{
17?????????????Title?=?"Click?The?Gradient?Center";
18?????????????brush?=?new?RadialGradientBrush(Colors.White,Colors.Red);
19?????????????brush.RadiusX?=?brush.RadiusY?=?.10;
20?????????????brush.SpreadMethod?=?GradientSpreadMethod.Repeat;
21?????????????Background?=?brush;
22?????????}
23?
24?????????protected?override?void?OnMouseDown(MouseButtonEventArgs?e)
25?????????{
26?????????????double?width?=?ActualWidth?-?2?*?SystemParameters.ResizeFrameVerticalBorderWidth;
27?????????????double?height?=?ActualHeight?-?2?*?SystemParameters.ResizeFrameHorizontalBorderHeight?-?SystemParameters.CaptionHeight;
28?
29?????????????Point?ptMouse?=?e.GetPosition(this);
30?????????????ptMouse.X?/=?width;
31?????????????ptMouse.Y?/=?height;
32?
33?????????????if?(e.ChangedButton?==?MouseButton.Left)
34?????????????{
35?????????????????brush.Center?=?ptMouse;
36?????????????????brush.GradientOrigin?=?ptMouse;
37?????????????}
38?????????????else?if?(e.ChangedButton==MouseButton.Right)
39?????????????{
40?????????????????brush.GradientOrigin?=?ptMouse;
41?????????????}
42?????????}
43?
44?????}
45?????
46?????#endregion
?
?
第八個(gè)示例
?
FollowTheRainbow P52 ?1?????#region?FollowTheRainbow?P52?2?
?3?????///?<summary>
?4?????///?同心圓的漸變
?5?????///?</summary>
?6?????class?RotateTheGradientOrigin?:?Window
?7?????{
?8?????????[STAThread]
?9?????????static?void?Main()
10?????????{
11?????????????Application?app?=?new?Application();
12?????????????app.Run(new?RotateTheGradientOrigin());
13?????????}
14?????????RadialGradientBrush?brush;
15?????????double?angle;
16?
17?????????public?RotateTheGradientOrigin()
18?????????{
19?????????????Title?=?"Rotate?The?Gradient?Origin";
20?????????????this.WindowStartupLocation?=?WindowStartupLocation.CenterScreen;
21?????????????Width?=?384;
22?????????????Height?=?384;
23?????????????brush?=?new?RadialGradientBrush(Colors.White,?Colors.Blue);
24?????????????brush.RadiusX?=?brush.RadiusY?=?.10;
25?????????????brush.SpreadMethod?=?GradientSpreadMethod.Repeat;
26?????????????//Background?=?brush;
27?????????????Background?=?new?LinearGradientBrush(Colors.Red,?Colors.Blue,?new?Point(0,?0),?new?Point(1,?1));
28?
29?????????????//BorderBrush?=?Brushes.SaddleBrown;
30?????????????//BorderThickness?=?new?Thickness(25,?50,?75,?100);
31?????????????BorderBrush?=?new?LinearGradientBrush(Colors.Red,?Colors.Blue,?new?Point(0,?0),?new?Point(1,?1));
32?????????????BorderThickness?=?new?Thickness(50);
33?
34?????????????DispatcherTimer?timer?=?new?DispatcherTimer();
35?????????????timer.Interval?=?TimeSpan.FromMilliseconds(100);?;
36?????????????timer.Tick?+=?timerOnTick;
37?????????????timer.Start();
38?????????}
39?
40?
41?
42?????????void?timerOnTick(object?sender,?EventArgs?e)
43?????????{
44?
45?????????????Point?pt?=?new?Point(.5?+?.05?*?Math.Cos(angle),?.5?+?.05?*?Math.Sin(angle));
46?????????????brush.GradientOrigin?=?pt;
47?????????????angle?+=?Math.PI?/?6;//ie?,30?degrees
48?????????}
49?????}
50?
51?????#endregion
?
?
?????? 本章主要是通過示例對(duì)WPF中各種畫刷的使用進(jìn)行了練習(xí),通過這些練習(xí)熟悉了對(duì)簡(jiǎn)單畫刷和漸變畫刷的各種使用。
?????? 貫例,帖張圖作為對(duì)自己的獎(jiǎng)勵(lì)
????????
?
?????? 下一章將繼續(xù)WPF學(xué)習(xí)之旅,內(nèi)容就是 WPF中的 Content。
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/l6098627/archive/2009/12/07/1618551.html
總結(jié)
以上是生活随笔為你收集整理的WPF学习笔记-第二周【基本笔刷】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海康ehome协议及应用
- 下一篇: 终极成语接龙,谁能继续往下接,哈哈!!!