WPF学习笔记-第一周
看了蓝色右手把WPF用得那么爽,不禁让我又有了重新学习的WPF的热情,再次拿起那本放下过两次的【WPF探秘】,感觉已经不再象以前那个迷茫了。
但似乎【WPF探秘】不太适合我看,很多的例子只是讲述关键的代码,让我走了不少的弯路,为了找一本简单点的WPF入门书籍,在购书中心呆了两个半天,把仅有的六本WPF相关的书都翻了一遍。发现有一本【WFP编程】到是很适合我的。本想买,但又感觉太厚,象我这样每周来回广州珠海还背着本本+生活用品,再背这么厚一本还是真的受不了。后来想想还是做罢,等哪天稳定下来了,再买回来放到案头上做参考书到是不错。
于是又在网上搜罗了很久,找了一些电子书,其中有一本 【WPF程序设计指南(中文版)】感觉很不错,如是决定按这本书的路线开始学习WPF,由于最近工作较忙,每天都要加班,所以只天也只能在睡前抽两到三个小时看书学习。而且一直是做WEB开发的,所以对 Windows 程序的开发不太了解,看到这本书也从这些基础的讲起。所以正好按作者的指引从头学起。
学习这本书,我计划要将其中的每个示例都打出来,并运行起来,以加深自己的印象,避免象以前哪样看了就忘。为了激励自己,也为了以后可以回这头来看自己的学习效果,所以我计划以后会把自己打出来的示例代码贴到这里。好了不多说了,就从这里开始吧
第一部分:代码
第一章:应用程序与窗口
第一个示例
SayHello P14
1
2
3 /// <summary>
4 /// 第一个WPF窗口程序
5 /// </summary>
6 class SayHello
7 {
8 /// <summary>
9 /// 程序入口,必须指定 STAThread 否则会报错
10 /// </summary>
11 [STAThread]
12 static void Main()
13 {
14 //定义应用程序类
15 Application app = new Application();
16 //定义winodw
17 Window win = new Window();
18 //设置标题
19 win.Title = "SayHello!";
20 //显示窗口
21 win.Show();
22 //运行应用程序
23 app.Run();
24 }
25 }
26
27 //知识:
28 //1、引出命名空间 System 和 System.Window
29 //2、 System.Window命名空间包含了所有的基本WPF类别、结构(struct)、接口(Interface)、委托(delegate)以及枚举(enum)类型 ,其中还包括 Application 和 Window 两个类
30 //3、其他的WPF命名空间都以 System.Window 开头。只有System.Window.Forms 例外,他是传统 windows.Form 的命名空间
31 //4、System.Window.Forms.Integration 则又是一个特例,是用来集成 传统windows 程序和WPF程序的
32 //5、每个程序的入口必须是名为 Main() 的一个静态函数 ,并且要被 【STAThread】属性修饰,否则编译会失败
33 //6、使用 Application.Run() 来建立一个消息循环
34 //7、在工程属性的 output Type设成 Console Application 则可以同时打开控制台,可便于调试。
35
36
2
3 /// <summary>
4 /// 第一个WPF窗口程序
5 /// </summary>
6 class SayHello
7 {
8 /// <summary>
9 /// 程序入口,必须指定 STAThread 否则会报错
10 /// </summary>
11 [STAThread]
12 static void Main()
13 {
14 //定义应用程序类
15 Application app = new Application();
16 //定义winodw
17 Window win = new Window();
18 //设置标题
19 win.Title = "SayHello!";
20 //显示窗口
21 win.Show();
22 //运行应用程序
23 app.Run();
24 }
25 }
26
27 //知识:
28 //1、引出命名空间 System 和 System.Window
29 //2、 System.Window命名空间包含了所有的基本WPF类别、结构(struct)、接口(Interface)、委托(delegate)以及枚举(enum)类型 ,其中还包括 Application 和 Window 两个类
30 //3、其他的WPF命名空间都以 System.Window 开头。只有System.Window.Forms 例外,他是传统 windows.Form 的命名空间
31 //4、System.Window.Forms.Integration 则又是一个特例,是用来集成 传统windows 程序和WPF程序的
32 //5、每个程序的入口必须是名为 Main() 的一个静态函数 ,并且要被 【STAThread】属性修饰,否则编译会失败
33 //6、使用 Application.Run() 来建立一个消息循环
34 //7、在工程属性的 output Type设成 Console Application 则可以同时打开控制台,可便于调试。
35
36
第二个示例
HandleAnEvent p17
1
2
3 /// <summary>
4 /// 带事件的窗口
5 /// </summary>
6 class HandleAnEvent
7 {
8 [STAThread]
9 static void Main()
10 {
11 Application app = new Application();
12 Window win = new Window();
13 win.Title = "Hello World!";
14 //指定事件的委托
15 win.MouseDown += win_MouseDown;
16 win.Show();
17 app.Run();
18 }
19
20 /// <summary>
21 /// 鼠标按下事件(因是在 Mian() 中直接使用,所以要定义为 static )
22 /// </summary>
23 /// <param name="sender"></param>
24 /// <param name="e"></param>
25 protected static void win_MouseDown(object sender, MouseButtonEventArgs e)
26 {
27 //当前窗口
28 //Window win = sender as Window;
29 Window win = Application.Current.MainWindow;
30 MessageBox.Show(string.Format("Window Clicked With {0} button at point ({1})", e.ChangedButton, e.GetPosition(win)));
31 }
32
33 }
34
35 //知识:
36 //1、键盘鼠标事件需引入 命名空间 System.Windows.Input
37 //2、Window.MouseDown 事件的定义必须是一个 MouseEventHandle 的委托
38
2
3 /// <summary>
4 /// 带事件的窗口
5 /// </summary>
6 class HandleAnEvent
7 {
8 [STAThread]
9 static void Main()
10 {
11 Application app = new Application();
12 Window win = new Window();
13 win.Title = "Hello World!";
14 //指定事件的委托
15 win.MouseDown += win_MouseDown;
16 win.Show();
17 app.Run();
18 }
19
20 /// <summary>
21 /// 鼠标按下事件(因是在 Mian() 中直接使用,所以要定义为 static )
22 /// </summary>
23 /// <param name="sender"></param>
24 /// <param name="e"></param>
25 protected static void win_MouseDown(object sender, MouseButtonEventArgs e)
26 {
27 //当前窗口
28 //Window win = sender as Window;
29 Window win = Application.Current.MainWindow;
30 MessageBox.Show(string.Format("Window Clicked With {0} button at point ({1})", e.ChangedButton, e.GetPosition(win)));
31 }
32
33 }
34
35 //知识:
36 //1、键盘鼠标事件需引入 命名空间 System.Windows.Input
37 //2、Window.MouseDown 事件的定义必须是一个 MouseEventHandle 的委托
38
第三个示例
InheritTheApp P20
1
2
3 /// <summary>
4 /// 继承 Application 类
5 /// </summary>
6 class InheritTheApp : Application
7 {
8 [STAThread]
9 static void Main()
10 {
11 InheritTheApp app = new InheritTheApp();
12 app.Run();
13 }
14
15 /// <summary>
16 /// 覆盖 OnStartup 件事,并在该事件中启动窗口
17 /// </summary>
18 /// <param name="e"></param>
19 protected override void OnStartup(StartupEventArgs e)
20 {
21 base.OnStartup(e);
22
23 Window win = new Window();
24 win.Title = "Hello World!";
25 win.MouseDown += win_MouseDown;
26 win.Show();
27 }
28
29 /// <summary>
30 /// 覆盖注销事件
31 /// </summary>
32 /// <param name="e"></param>
33 protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
34 {
35 base.OnSessionEnding(e);
36 MessageBoxResult result = MessageBox.Show("Do you want to save your data?", MainWindow.Title, MessageBoxButton.YesNo);
37
38 if (result == MessageBoxResult.No)
39 {
40 //取消
41 e.Cancel = true;
42 }
43 }
44
45 protected static void win_MouseDown(object sender, MouseButtonEventArgs e)
46 {
47 //Window win = sender as Window;
48 Window win = Application.Current.MainWindow;
49 MessageBox.Show(string.Format("Window Clicked With {0} button at point ({1})", e.ChangedButton, e.GetPosition(win)));
50 }
51 }
52
53 //知识:
54 //1、可对Application 进行继承,并覆盖其中的相关方法
55 //2、Application.OnStartup 会在程序启动时执行 ,在继承时可通过重写该方法来执行一些需要在程序动时执行的操作
56 //3、...CancelEventArgs 可通过 ...CancelEventArgs.Cancel = true; 取消相关操作
57
58
2
3 /// <summary>
4 /// 继承 Application 类
5 /// </summary>
6 class InheritTheApp : Application
7 {
8 [STAThread]
9 static void Main()
10 {
11 InheritTheApp app = new InheritTheApp();
12 app.Run();
13 }
14
15 /// <summary>
16 /// 覆盖 OnStartup 件事,并在该事件中启动窗口
17 /// </summary>
18 /// <param name="e"></param>
19 protected override void OnStartup(StartupEventArgs e)
20 {
21 base.OnStartup(e);
22
23 Window win = new Window();
24 win.Title = "Hello World!";
25 win.MouseDown += win_MouseDown;
26 win.Show();
27 }
28
29 /// <summary>
30 /// 覆盖注销事件
31 /// </summary>
32 /// <param name="e"></param>
33 protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
34 {
35 base.OnSessionEnding(e);
36 MessageBoxResult result = MessageBox.Show("Do you want to save your data?", MainWindow.Title, MessageBoxButton.YesNo);
37
38 if (result == MessageBoxResult.No)
39 {
40 //取消
41 e.Cancel = true;
42 }
43 }
44
45 protected static void win_MouseDown(object sender, MouseButtonEventArgs e)
46 {
47 //Window win = sender as Window;
48 Window win = Application.Current.MainWindow;
49 MessageBox.Show(string.Format("Window Clicked With {0} button at point ({1})", e.ChangedButton, e.GetPosition(win)));
50 }
51 }
52
53 //知识:
54 //1、可对Application 进行继承,并覆盖其中的相关方法
55 //2、Application.OnStartup 会在程序启动时执行 ,在继承时可通过重写该方法来执行一些需要在程序动时执行的操作
56 //3、...CancelEventArgs 可通过 ...CancelEventArgs.Cancel = true; 取消相关操作
57
58
第三个示例
ThrowWindowParty P22
1
2
3 /// <summary>
4 /// 显示多个窗口
5 /// </summary>
6 class ThrowWindowParty : Application
7 {
8 [STAThread]
9 static void Main()
10 {
11 ThrowWindowParty app = new ThrowWindowParty();
12 app.Run();
13 }
14
15 protected override void OnStartup(StartupEventArgs e)
16 {
17 base.OnStartup(e);
18
19 Window win = new Window();
20 win.Title = "Main WIndow!";
21 win.MouseDown += win_MouseDown;
22 win.Show();
23 for (int i = 0; i < 3; i++)
24 {
25 Window w = new Window();
26 w.Title = "Extra Window No." + i.ToString();
27 w.Show();
28 }
29 }
30
31
32 protected static void win_MouseDown(object sender, MouseButtonEventArgs e)
33 {
34 //Window win = sender as Window;
35 Window win = new Window();
36 win.Title = "Model Doalog box";
37 win.ShowDialog();
38 }
39 }
40
41 //知识:
42 //1、经试验,多个弹出窗口,对第一个窗口进行弹出,隐藏,显示,关闭 重新创建,后 MainWindow始终是第一个窗口
43 //2、 win.ShowDialog(); 弹出模态窗口
44 //3、所有的窗口关闭后 Application.Run()才返回
45
46
47
2
3 /// <summary>
4 /// 显示多个窗口
5 /// </summary>
6 class ThrowWindowParty : Application
7 {
8 [STAThread]
9 static void Main()
10 {
11 ThrowWindowParty app = new ThrowWindowParty();
12 app.Run();
13 }
14
15 protected override void OnStartup(StartupEventArgs e)
16 {
17 base.OnStartup(e);
18
19 Window win = new Window();
20 win.Title = "Main WIndow!";
21 win.MouseDown += win_MouseDown;
22 win.Show();
23 for (int i = 0; i < 3; i++)
24 {
25 Window w = new Window();
26 w.Title = "Extra Window No." + i.ToString();
27 w.Show();
28 }
29 }
30
31
32 protected static void win_MouseDown(object sender, MouseButtonEventArgs e)
33 {
34 //Window win = sender as Window;
35 Window win = new Window();
36 win.Title = "Model Doalog box";
37 win.ShowDialog();
38 }
39 }
40
41 //知识:
42 //1、经试验,多个弹出窗口,对第一个窗口进行弹出,隐藏,显示,关闭 重新创建,后 MainWindow始终是第一个窗口
43 //2、 win.ShowDialog(); 弹出模态窗口
44 //3、所有的窗口关闭后 Application.Run()才返回
45
46
47
第五个示例
InheritTheMyApp p25
1
2
3 /// <summary>
4 /// 多次继承
5 /// </summary>
6 class MyApplication : Application
7 {
8 protected override void OnStartup(StartupEventArgs e)
9 {
10 base.OnStartup(e);
11
12 Window win = new Window();
13
14 win.Title = "Application";
15
16 win.Show();
17 }
18 }
19
20 class InheritTheMyApp : MyApplication
21 {
22 [STAThread]
23 static void Main()
24 {
25 MyApplication app = new MyApplication();
26 app.Run();
27 }
28 }
29
30
2
3 /// <summary>
4 /// 多次继承
5 /// </summary>
6 class MyApplication : Application
7 {
8 protected override void OnStartup(StartupEventArgs e)
9 {
10 base.OnStartup(e);
11
12 Window win = new Window();
13
14 win.Title = "Application";
15
16 win.Show();
17 }
18 }
19
20 class InheritTheMyApp : MyApplication
21 {
22 [STAThread]
23 static void Main()
24 {
25 MyApplication app = new MyApplication();
26 app.Run();
27 }
28 }
29
30
第六个示例
InheritTheAppAndWindow p26
1
2
3 /// <summary>
4 /// 继承 Application
5 /// </summary>
6 class MyApplication : Application
7 {
8 protected override void OnStartup(StartupEventArgs e)
9 {
10 base.OnStartup(e);
11
12 MyWindow win = new MyWindow();
13
14 win.Title = "MyWindow";
15
16 win.Show();
17 }
18 }
19
20 /// <summary>
21 /// 继承 Window
22 /// </summary>
23 class MyWindow : Window
24 {
25 public MyWindow()
26 {
27 this.Title = "Inherit App and Window";
28 }
29
30 protected override void OnMouseDown(MouseButtonEventArgs e)
31 {
32 base.OnMouseDown(e);
33
34 MessageBox.Show(string.Format("window clicked with {0} button at point ({1})",
35 e.ChangedButton, e.GetPosition(this)), Title);
36 }
37 }
38
39 /// <summary>
40 /// 再次继承
41 /// </summary>
42 class InheritTheAppAndWindow : MyApplication
43 {
44 [STAThread]
45 static void Main()
46 {
47 InheritTheAppAndWindow app = new InheritTheAppAndWindow();
48 app.Run();
49 }
50 }
51
52
2
3 /// <summary>
4 /// 继承 Application
5 /// </summary>
6 class MyApplication : Application
7 {
8 protected override void OnStartup(StartupEventArgs e)
9 {
10 base.OnStartup(e);
11
12 MyWindow win = new MyWindow();
13
14 win.Title = "MyWindow";
15
16 win.Show();
17 }
18 }
19
20 /// <summary>
21 /// 继承 Window
22 /// </summary>
23 class MyWindow : Window
24 {
25 public MyWindow()
26 {
27 this.Title = "Inherit App and Window";
28 }
29
30 protected override void OnMouseDown(MouseButtonEventArgs e)
31 {
32 base.OnMouseDown(e);
33
34 MessageBox.Show(string.Format("window clicked with {0} button at point ({1})",
35 e.ChangedButton, e.GetPosition(this)), Title);
36 }
37 }
38
39 /// <summary>
40 /// 再次继承
41 /// </summary>
42 class InheritTheAppAndWindow : MyApplication
43 {
44 [STAThread]
45 static void Main()
46 {
47 InheritTheAppAndWindow app = new InheritTheAppAndWindow();
48 app.Run();
49 }
50 }
51
52
第七个示例
InheritTheWin p27
1
2
3 /// <summary>
4 /// 常见和简洁的做法
5 /// </summary>
6 class InheritTheWin : Window
7 {
8 [STAThread]
9 static void Main()
10 {
11 new Application().Run(new InheritTheWin());
12 }
13
14 public InheritTheWin()
15 {
16 this.Title = "Inherit The Win";
17 this.Width = 100 * 3.14;
18 this.Height = 100 * Math.PI;
19 }
20 }
21
22
InheritTheWin p27
1
2
3 /// <summary>
4 /// 常见和简洁的做法
5 /// </summary>
6 class InheritTheWin : Window
7 {
8 [STAThread]
9 static void Main()
10 {
11 new Application().Run(new InheritTheWin());
12 }
13
14 public InheritTheWin()
15 {
16 this.Title = "Inherit The Win";
17 this.Width = 100 * 3.14;
18 this.Height = 100 * Math.PI;
19 }
20 }
21
22
2
3 /// <summary>
4 /// 常见和简洁的做法
5 /// </summary>
6 class InheritTheWin : Window
7 {
8 [STAThread]
9 static void Main()
10 {
11 new Application().Run(new InheritTheWin());
12 }
13
14 public InheritTheWin()
15 {
16 this.Title = "Inherit The Win";
17 this.Width = 100 * 3.14;
18 this.Height = 100 * Math.PI;
19 }
20 }
21
22
第八个示例
GrowAndShrink p31
1
2
3 /// <summary>
4 /// 响应键盘事件改变窗口大小
5 /// </summary>
6 class GrowAndShrink : Window
7 {
8 [STAThread]
9 static void Main()
10 {
11 new Application().Run(new GrowAndShrink());
12 }
13
14 public GrowAndShrink()
15 {
16 this.Title = "Grow & Shrink";
17 this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
18 this.Width = 192;
19 this.Height = 192;
20 }
21
22 protected override void OnKeyDown(KeyEventArgs e)
23 {
24 base.OnKeyDown(e);
25
26 if (e.Key == Key.Up)
27 {
28 this.Left -= 0.05 * this.Width;
29 this.Top -= 0.05 * this.Height;
30 this.Width *= 1.1;
31 this.Height *= 1.1;
32 }
33 else if (e.Key == Key.Down)
34 {
35 this.Left += 0.05 * (Width /= 1.1);
36 this.Top += 0.05 * (Height /= 1.1);
37 }
38 }
39 }
40
2
3 /// <summary>
4 /// 响应键盘事件改变窗口大小
5 /// </summary>
6 class GrowAndShrink : Window
7 {
8 [STAThread]
9 static void Main()
10 {
11 new Application().Run(new GrowAndShrink());
12 }
13
14 public GrowAndShrink()
15 {
16 this.Title = "Grow & Shrink";
17 this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
18 this.Width = 192;
19 this.Height = 192;
20 }
21
22 protected override void OnKeyDown(KeyEventArgs e)
23 {
24 base.OnKeyDown(e);
25
26 if (e.Key == Key.Up)
27 {
28 this.Left -= 0.05 * this.Width;
29 this.Top -= 0.05 * this.Height;
30 this.Width *= 1.1;
31 this.Height *= 1.1;
32 }
33 else if (e.Key == Key.Down)
34 {
35 this.Left += 0.05 * (Width /= 1.1);
36 this.Top += 0.05 * (Height /= 1.1);
37 }
38 }
39 }
40
第九个示例
TypeYourTitle p32
1
2
3 /// <summary>
4 /// 响应键盘输入,并在窗口标题栏显示键盘输入的字符
5 /// </summary>
6 class TypeYourTitle : Window
7 {
8 [STAThread]
9 static void Main()
10 {
11 new Application().Run(new TypeYourTitle());
12 }
13
14 public TypeYourTitle()
15 {
16 this.Title = "Type Your Title";
17 this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
18 this.Width = 500;
19 this.Height = 500;
20 }
21
22 protected override void OnKeyDown(KeyEventArgs e)
23 {
24 base.OnKeyDown(e);
25
26 if (e.Key == Key.Up)
27 {
28 this.Left -= 0.05 * this.Width;
29 this.Top -= 0.05 * this.Height;
30 this.Width *= 1.1;
31 this.Height *= 1.1;
32 }
33 else if (e.Key == Key.Down)
34 {
35 this.Left += 0.05 * (Width /= 1.1);
36 this.Top += 0.05 * (Height /= 1.1);
37 }
38 }
39
40 protected override void OnTextInput(TextCompositionEventArgs e)
41 {
42 base.OnTextInput(e);
43 //由输入改变标题栏文字
44 if (e.Text == "\b" && this.Title.Length > 0)
45 {
46 this.Title = this.Title.Substring(0, this.Title.Length - 1);
47 }
48 else if(e.Text.Length>0 && !Char.IsControl(e.Text[0]))
49 {
50 this.Title += e.Text;
51 }
52 }
53 }
54
2
3 /// <summary>
4 /// 响应键盘输入,并在窗口标题栏显示键盘输入的字符
5 /// </summary>
6 class TypeYourTitle : Window
7 {
8 [STAThread]
9 static void Main()
10 {
11 new Application().Run(new TypeYourTitle());
12 }
13
14 public TypeYourTitle()
15 {
16 this.Title = "Type Your Title";
17 this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
18 this.Width = 500;
19 this.Height = 500;
20 }
21
22 protected override void OnKeyDown(KeyEventArgs e)
23 {
24 base.OnKeyDown(e);
25
26 if (e.Key == Key.Up)
27 {
28 this.Left -= 0.05 * this.Width;
29 this.Top -= 0.05 * this.Height;
30 this.Width *= 1.1;
31 this.Height *= 1.1;
32 }
33 else if (e.Key == Key.Down)
34 {
35 this.Left += 0.05 * (Width /= 1.1);
36 this.Top += 0.05 * (Height /= 1.1);
37 }
38 }
39
40 protected override void OnTextInput(TextCompositionEventArgs e)
41 {
42 base.OnTextInput(e);
43 //由输入改变标题栏文字
44 if (e.Text == "\b" && this.Title.Length > 0)
45 {
46 this.Title = this.Title.Substring(0, this.Title.Length - 1);
47 }
48 else if(e.Text.Length>0 && !Char.IsControl(e.Text[0]))
49 {
50 this.Title += e.Text;
51 }
52 }
53 }
54
以上是第一章的九个示例代码,贴个图慰劳一下自己:)
接下来会是第二章【基本画刷】...
浙公网安备 33010602011771号