日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF学习笔记-第二周【基本笔刷】

發布時間:2023/12/20 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF学习笔记-第二周【基本笔刷】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? 書接上回,這一次,講的是WPF中的基本筆刷,由于是菜鳥,就不多說了,繼續帖示例代碼:)

?

第一部份 代碼

第二章 基本筆刷

第一個 示例

?

VaryTheBackgroud P38 ?1?????#region?VaryTheBackgroud?P38
?2?????
?3?????///?<summary>
?4?????///?改變背景的示例
?5?????///?通過鼠標移動事件,根據鼠標對應的窗口中心位置的距離計算并改變窗口的背景顏色
?6?????///?</summary>
?7?????public?class?VaryTheBackgroud?:?Window
?8?????{
?9?????????///?<summary>
10?????????///?定義一個?Brush,存放窗口的背景顏色
11?????????///?</summary>
12?????????SolidColorBrush?brush?=?new?SolidColorBrush(Colors.Black);
13?????????///?<summary>
14?????????///?構造窗口
15?????????///?</summary>
16?????????public?VaryTheBackgroud()
17?????????{
18?????????????//指定標題\高\寬和背景顏色
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?????????????//定義并運行程序
29?????????????Application?app?=?new?Application();
30?????????????app.Run(new?VaryTheBackgroud());
31?????????}
32?
33?????????///?<summary>
34?????????///?重寫鼠標移動事件
35?????????///?</summary>
36?????????///?<param?name="e"></param>
37?????????protected?override?void?OnMouseMove(MouseEventArgs?e)
38?????????{
39?????????????//base.OnMouseMove(e);
40?????????????//獲得窗口的工作區的寬和高(減去加框和標題欄后的高寬度)
41?????????????double?width?=?ActualWidth?-?2?*?SystemParameters.ResizeFrameVerticalBorderWidth;
42?????????????double?height?=?ActualHeight?-?2?*?SystemParameters.ResizeFrameHorizontalBorderHeight?-?SystemParameters.CaptionHeight;
43?????????????//當前鼠標的位置
44?????????????Point?ptMouse=e.GetPosition(this);
45?????????????//窗口工作區中心點
46?????????????Point?ptCenter=new?Point(width/2,height/2);
47?????????????//鼠標相對工作區中心的偏移量
48?????????????Vector?vectMouse?=?ptMouse?-?ptCenter;
49?????????????//計算角度
50?????????????double?angle?=?Math.Atan2(vectMouse.Y,?vectMouse.X);
51?????????????//計算一個圓形的區域
52?????????????Vector?vectEllipse?=?new?Vector(width?/?2?*?Math.Cos(angle),?height?/?2?*?Math.Sin(angle));
53?????????????//計算鼠標的位置與圓形區域的比例,并*255?以轉換成顏色值
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?????//相關知識:
65?????//1、Brush?的改變后會自動重畫窗口,
66?????//原因是因繼承自Freezable類,該類實現了一個名為Changed的事件,會在發生改變后被觸發。
67?????//2、Colors?定義了141種顏色,Brushs也同樣的定義了同名的141種畫刷
68?????//3、如果直接?SolidColorBrush?brush?=Brushs.Black?后再來指定顏色則會報錯,因由?Brushs?所得的畫刷對象是處于凍結狀態的,不能再被改變
69?????//4、被凍結的Freezable對象可以在不同的線程之間共享,未被凍結的則不行。
70?????
71?????#endregion

?

?

第二個示例

?

FlipTroughTheBrushes P41 ?1?????#region?FlipTroughTheBrushes?P41
?2?????
?3?????///?<summary>
?4?????///?使用?Brushes?類定義的?141?種顏色筆刷填充窗口
?5?????///?通過重寫?OnKeyDown?事件按一下下方向鍵,就使用一種顏色的筆刷填充窗口
?6?????///?</summary>
?7?????public?class?FlipThroughTheBushes:Window
?8?????{
?9?????????//定義計數器和筆刷組
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?類定義的所有公有成員和靜態成員
23?????????????//BindingFlags?標識成員的類型,Brushes?只有Public?和?static?所以可以不用限制
24?????????????props?=?typeof(Brushes).GetProperties(BindingFlags.Public?|?BindingFlags.Static);
25?????????????//設置窗口標題和背景顏色
26?????????????setTitleAndBackground();
27?????????}
28?
29?????????///?<summary>
30?????????///?重寫?OnKeyDown?事件
31?????????///?</summary>
32?????????///?<param?name="e"></param>
33?????????protected?override?void?OnKeyDown(KeyEventArgs?e)
34?????????{
35?????????????//根據上下方向鍵的動作來設定計數
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?????????????????//設置窗口標題和背景顏色
42?????????????????setTitleAndBackground();
43?????????????}
44?????????????base.OnKeyDown(e);
45?????????}
46?????????///?<summary>
47?????????///?設置窗口標題和背景顏色
48?????????///?</summary>
49?????????void?setTitleAndBackground()
50?????????{
51?????????????//在標題顯示當前使用的顏色
52?????????????Title?=?"Flip?Through?the?Brushes?-"?+?props[index].Name;
53?????????????//設置背景色
54?????????????//PropertyInfo[].GetValue(null,null)?在這里實際返回是的?SolidColorBrush?對象
55?????????????Background?=?(Brush)props[index].GetValue(null,?null);
56?????????}
57?????}
58?
59?????//知識:
60?????//1、System.Window?命名空間?有?SystemColors?類其作用相似于?Colors?和?Brushes?只具有靜態的只讀property,返回Color值和SolidColorBrush對象。
61?????//2、這些值存儲在Windows的注冊表中,可以由SystemColors.WindowColor?和?SystemColors.WindowTextBrush?獲得用戶對窗口工作區背景和文字顏色的喜好
62?????//3、SystemColors返回的畫刷是凍結的?可以?Brush?brush=new?SystemColorBrush(SystemColors.WindowColor);?但不能改變?Brush?brush=SystemColors.WindowBrush;?這樣的畫刷
63?????//4、繼承自Freezable?的類對象有凍結的問題,但Color?是結構體,沒有凍結的問題。
64?????//5、SolidColorBrush?是單色畫刷,漸變畫刷是:?LinearGradientBrush
65?????
66?????#endregion

?

?

第三個示例

?

GradilteTheBrush P43 ?1?????#region?GradilteTheBrush?P43
?2?????
?3?????///?<summary>
?4?????///?以漸變色填充窗口工作區
?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?????????????//?定義一個漸變色的畫刷?,LinearGradientBrush中?從?Point(0,?0)到?Point(1,?1)就是指從窗口工作區的右上角到左下角
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?????//知識:
31?????//1、WPF中的點是與設備無關的,WPF漸變畫刷有特性是讓你不用基于窗口尺寸來調整的點,默認情況下你指定的點是相對于表面的,這里的表面被視為一個單位寬一個單位高,即左上角為(0,0),右下角為(1,1),左下角為(0,1)?右下角為(1,0)
32?????//2、LinearGradientBrush?設置的漸變色效果是以兩點進行連線,連線中點的顏色就是兩點顏色的平均值,垂直與連線的線上的點顏色是相同的
33?????//3、LinearGradientBrush?的重載版本中可以設置360度角度(angle)進行漸變填充?如?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

?

?

第四個示例

?

AdjustTheBrush P47 ?1?????#region?AdjustTheBrush?P47
?2?????
?3?????///?<summary>
?4?????///?對角線為漸變中線的漸變畫刷填充
?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?????//知識:
44?????//1、??StartPoint?和?是LinearGradientBrush僅有的兩個?property
45?????//2、??LinearGradientBrush?可以使用?GradientBrush?定義的GradinetStops?property?設置更多的顏色
46?????//3、?new?GradientStop(color1,offset);
47?????
48?????#endregion

?

?

第五個示例

?

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?的彩虹記住系統
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

?

?

第六個示例

?

FollowTheRainbow P49 ?1?????#region?FollowTheRainbow?P49
?2?????
?3?????///?<summary>
?4?????///?由內向處的彩虹顏色的窗體
?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?的彩虹記住系統
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

?

?

第七個示例

?

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

?

?

第八個示例

?

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

?

?

?????? 本章主要是通過示例對WPF中各種畫刷的使用進行了練習,通過這些練習熟悉了對簡單畫刷和漸變畫刷的各種使用。

?????? 貫例,帖張圖作為對自己的獎勵

????????

?

?????? 下一章將繼續WPF學習之旅,內容就是 WPF中的 Content。

?

?

?

?

轉載于:https://www.cnblogs.com/l6098627/archive/2009/12/07/1618551.html

總結

以上是生活随笔為你收集整理的WPF学习笔记-第二周【基本笔刷】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。