您的位置:首页 > 其它

(WPF学习记录)第一章 应用程序与窗口

2008-08-31 09:10 197 查看
我的工具:VS2008。

我的教材:Windows Presentation Foundation 程序设计指南(英文版的名字叫Applications = Code + Markup: A Guide to the Microsoft Windows Presentation Foundation)。

准备工作:

1. 新建一个空白解决方案。



2. 添加"PresentationCore"、"PresentationFramework"、"System"和"WindowsBase"几个引用。右击解决方案中的引用--添加引用,在.net中选择以上几个引用,确定。

3. 然后添加代码文件写代码就行了。项目--添加新项...



4. 以下会有很多个小程序,这些小程序相互独立,各自完整(有自己的Main函数等),虽然很多只一个代码文件。而一个工程里只能一次运行一个程序,调试、运行什么的。所以每次都需要新建一个工程(如果不怕麻烦的话),或者在添加新代码文件后,将上一个程序文件“从项目中拔除”,否则会出错。

第01个小程序:大家好(SayHello.cs)

using System;

using System.Windows;

namespace Chapter01

{

class SayHello

{

[STAThread] //申明该应用程序的初始线程模型为单线程单元,必须,否则编译失败

public static void Main()

{

Window win = new Window(); //Window类对象

win.Title = "Say Hello"; //窗口标题

win.Height = 400; //窗口高度

win.Width = 600; //窗口宽度

win.WindowStartupLocation = WindowStartupLocation.CenterScreen; //屏幕中心显示窗口

win.Show(); //显示窗口,也可不要这句,下面app.Run(win);

Application app = new Application(); //创建Application对象

app.Run(); //启动应用程序

}

}

}

运行时,会发现除我们的应用程序外,有一个控制台窗口也在运行:



这是源自编译选项的设定,我们可以这样更改:右击解决方案中工程名,选属性,然后在属性框中输出类型选Windows应用程序。



其实,那个控制台(console)窗口蛮有用的,程序运行时可以利用它来显示一些文本信息,以便调试。如果程序的Bug太多,甚至无法将GUI窗口显示出来,或者进入无限循环,这时只要在控制台窗口中输入Ctrl+C,就可以轻易地关闭程序。
第02个小程序:触发事件(HandleAnEvent.cs)

using System;

using System.Windows;

using System.Windows.Input;

namespace Chapter01

{

class HandleAnEvent

{

[STAThread]

public static void Main()

{

Application app = new Application();

Window win = new Window();

win.Title = "Handle An Event"; //窗口标题

win.Height = 400; //窗口高度

win.Width = 600; //窗口宽度

win.WindowStartupLocation = WindowStartupLocation.CenterScreen; //屏幕中心显示窗口

win.MouseDown += WindowOnMouseDown; //绑定MouseDown事件(添加事件处理器)

app.Run(win);

}

//这个处理器必须符合MouseButtonEventArgs委托,表现在参数上

static void WindowOnMouseDown(object sender, MouseButtonEventArgs args)

{

//Window win = sender as Window; //as将sender参数转型为Window对象,也可以:Window win = (Window)sender;

Window win = Application.Current.MainWindow; //获取应用程序主窗口,与上一句的功能相同

string strMessage =

string.Format("Window clicked with {0} button at point ({1})",

args.ChangedButton, //获取事件关联按钮

args.GetPosition(win)); //获取鼠标指针相对于指定元素(win)的位置

MessageBox.Show(strMessage, win.Title); //以弹出窗口方式显示鼠标点击位置。

}

}

}

使用此事件处理器的Main方法是static的,我们必须把此事件处理器也申明为static。

MessageBox显示出鼠标点击的位置,这个位置是相对于客户区的,且与设备无关的(不是以像素为单位,以1/96英寸为一个单位)。

第03个小程序:继承(InheritTheApp.cs)

using System;

using System.Windows;

using System.Windows.Input;

namespace Chapter01

{

class InheritTheApp : Application //继承Application的类

{

[STAThread]

public static void Main()

{

InheritTheApp app = new InheritTheApp(); //继承。InheritTheApp取代实现Application。

app.Run();

}

//调用app.Run()后(app是源Application对象),立即执行OnStartup方法。

protected override void OnStartup(StartupEventArgs args)

{

base.OnStartup(args); //先调用基类。不是绝对必要。

Window win = new Window();

win.Title = "Inherit the App"; //窗口标题

win.Height = 400; //窗口高度

win.Width = 600; //窗口宽度

win.WindowStartupLocation = WindowStartupLocation.CenterScreen; //屏幕中心显示窗口

win.Show();

}

//这个方法表示用户已经选择要注销Windows操作系统,或者要关闭电脑。

//此程序在结束前没有文档可以被存储,所以它忽略了事件响应,直接让此应用程序关闭。

protected override void OnSessionEnding(SessionEndingCancelEventArgs args)

{

base.OnSessionEnding(args); //先调用基类。不是绝对必要。

MessageBoxResult result = //用户单击了哪个信息框(结果)

MessageBox.Show("Do you want to save your data?",

this.MainWindow.Title,

MessageBoxButton.YesNoCancel,

MessageBoxImage.Question, MessageBoxResult.Yes);

args.Cancel = (result == MessageBoxResult.Cancel); //设置是否取消事件

}

//与OnStartup方法对应,当app.Run()返回时(关闭窗口),立即执行OnExit方法。

protected override void OnExit(ExitEventArgs args)

{

base.OnExit(args);

MessageBox.Show("The 'OnExit Method' is executing!"); //关闭窗口时显示信息

}

}

}

第04个小程序:窗口大聚会(ThrowWindowParty.cs)

using System;

using System.Windows;

using System.Windows.Input;

namespace Chapter01

{

class ThrowWindowParty : Application //继承类

{

[STAThread]

public static void Main()

{

ThrowWindowParty app = new ThrowWindowParty();

app.Run();

}

//重载OnStartup方法

protected override void OnStartup(StartupEventArgs args)

{

Window winMain = new Window();

//ShutdownMode = ShutdownMode.OnMainWindowClose;

winMain.Title = "Main Window";

winMain.WindowStartupLocation = WindowStartupLocation.CenterScreen;

winMain.MouseDown += WindowOnMouseDown;

winMain.Show(); //显示主窗口

for (int i = 0; i < 2; i++)

{

Window win = new Window();

//win.Owner = winMain;

win.Title = "Extra Window No. " + (i + 1);

win.Show(); //显示附加窗口

}

}

//鼠标按下。只在主窗口中按下鼠标有效。弹出窗口为主窗口的子窗口。

void WindowOnMouseDown(object sender,MouseButtonEventArgs args)

{

Window win = new Window();

win.Title = "Modal Dialog Box";

win.ShowDialog();

}

}

}

运行后,会出现三个窗口,它们地位相等,可以随意叠放、乱次关闭。
这三个窗口全部关闭时程序才会结束,因为Application 的ShutdownMode默认为ShutdownMode.OnLastWindowClose,即上一个窗口关闭后程序结束,只要还有窗口存在,程序依然运行。如果添加代码更改其默认属性为:

ShutdownMode = ShutdownMode.OnMainWindowClose;

则当主窗口关闭时,Run方法返回,程序结束。ShutdownMode还有个属性ShutdownMode.OnExplicitShutdown,这时只当程序呼叫Application 的Shutdown方法时,Run方法才会返回。
如果在for循环中插入以下代码

win.Owner = winMain;

那么新建的窗口就为winMain的子窗口。现在,这个三个窗口还可以相互切换,但是“被拥有”的子窗口一定在“拥有者”(主窗口)的前面。当将主窗口最小化时,子窗口也会从屏幕上消失;主窗口关闭时,子窗口也会被自动关闭。

在此程序中的鼠标按下事件中,新建的窗口win是调用ShowDialog来显示的,而不是Show。ShowDialog与Show的区别就是:ShowDialog不会立刻返回,且利用此方法显示的模式对话框,不会让你切换到同一个程序的其他窗口(可以切换到别的程序窗口),只有在关闭此对话框之后,ShowDialog的调用才会返回。

第05个小程序:多处继承(有三个源码文件)

第一个:MyWindow .cs

using System;

using System.Windows;

using System.Windows.Input;

namespace Chapter01

{

public class MyWindow : Window //继承Window类

{

public MyWindow()

{

Title = "Inherit App & Window"; //标题。

}

// 鼠标事件

protected override void OnMouseDown(MouseButtonEventArgs args)

{

base.OnMouseDown(args);

string strMessage =

string.Format("Window clicked with {0} button at point ({1})",

args.ChangedButton,

args.GetPosition(this));

MessageBox.Show(strMessage, Title);

}

}

}

继承Window的类,通常使用构造函数来初始化窗口本身。这里设定标题(Title),前面不需要加任何对象名称。这里也不需要绑定事件,直接重载OnMouseDown方法。
第二个:MyApplication .cs

using System;

using System.Windows;

using System.Windows.Input;

namespace Chapter01

{

class MyApplication : Application //继承Application的类

{

protected override void OnStartup(StartupEventArgs args) //重载OnStartup

{

base.OnStartup(args);

MyWindow win = new MyWindow(); //MyWindow对象

win.Show(); //显示窗口

}

}

}

第三个:InheritAppAndWindow.cs

using System;

using System.Windows;

using System.Windows.Input;

namespace Chapter01

{

class InheritAppAndWindow

{

[STAThread]

public static void Main()

{

MyApplication app = new MyApplication(); //MyApplication对象

app.Run();

}

}

}

第06个小程序:窗口放缩(GrowAndShrink.cs)
按向上向下键时,窗口尺寸随之增减10%。

using System;

using System.Windows;

using System.Windows.Input;

namespace Chapter01

{

public class GrowAndShrink : Window

{

[STAThread]

public static void Main()

{

Application app = new Application();

app.Run(new GrowAndShrink());

}

public GrowAndShrink()

{

Title = "Grow & Shrink";

WindowStartupLocation = WindowStartupLocation.CenterScreen;

Width = 192;

Height = 192;

}

protected override void OnKeyDown(KeyEventArgs args)

{

base.OnKeyDown(args);

if (args.Key == Key.Up)

{

//改变窗口位置

Left -= 0.05 * Width;

Top -= 0.05 * Height;

//尺寸增10%

Width *= 1.1;

Height *= 1.1;

}

else if (args.Key == Key.Down)

{

//尺寸减10%,同时改变窗口位置

Left += 0.05 * (Width /= 1.1);

Top += 0.05 * (Height /= 1.1);

}

}

}

}

第07个小程序:输入窗口标题(TypeYourTitle.cs)
程序运行后手动输入标题。

using System;

using System.Windows;

using System.Windows.Input;

namespace Chapter01

{

public class TypeYourTitle : Window

{

[STAThread]

public static void Main()

{

Application app = new Application();

app.Run(new TypeYourTitle());

}

//获取实际的Unicode字符

protected override void OnTextInput(TextCompositionEventArgs args)

{

base.OnTextInput(args);

if (args.Text == "/b" && Title.Length > 0) //按一下BackSpace健时,Title减一个字符

Title = Title.Substring(0, Title.Length - 1);

//检测是否有文本输入、指定字符是否属于控制类型

else if (args.Text.Length > 0 && !Char.IsControl(args.Text[0]))

Title += args.Text;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: