您的位置:首页 > 移动开发

c#中利用ApplicationContext 类 同时启动双窗体的实现(源自MSDN)

2009-12-31 23:03 387 查看
下面的代码示例显示两个窗体,并在两个窗体都关闭时退出应用程序。在应用程序启动和退出时,它会记住每个窗体的位置。此示例演示如何使用 ApplicationContext 和 Application.Run(context) 方法在启动应用程序时显示多个窗体。

类 MyApplicationContext 从 ApplicationContext 继承,并跟踪每个窗体关闭的时间,然后在这两个窗体均关闭时退出当前线程。该类为用户存储每个窗体的位置。窗体位置数据存储在标题为 Appdata.txt 的文件中,该文件在 UserAppDataPath 确定的位置中创建。

给定 ApplicationContext 的情况下,Main 方法调用 Application.Run(context) 启动应用程序。

using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Text;
using System.IO;

namespace ScreenSplit
{
static class Program
{
// 初始化窗体1大小及标题文本
public class AppForm1 : System.Windows.Forms.Form
{
public AppForm1()
{
Screen screen = Screen.FromRectangle(new Rectangle(0, 0, 0, 0));
this.Size = new System.Drawing.Size(screen.WorkingArea.Width / 2, screen.WorkingArea.Height);
this.Text = "AppForm1";
}
}

// 初始化窗体2大小及标题文本
public class AppForm2 : System.Windows.Forms.Form
{
public AppForm2()
{
Screen screen = Screen.FromRectangle(new Rectangle(0, 0, 0, 0));
this.Size = new System.Drawing.Size(screen.WorkingArea.Width / 2, screen.WorkingArea.Height);
this.Text = "AppForm2";
}
}

// 利用ApplicationContext类处理程序的启动、关闭
class MyApplicationContext : ApplicationContext
{

private int formCount;
private AppForm1 form1;
private AppForm2 form2;

private Rectangle form1Position;
private Rectangle form2Position;

private FileStream userData;

private MyApplicationContext()
{
formCount = 0;

// 应用程序退出
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);

try
{
// 在用户目录使用appdata.txt文件保存窗体退出时的位置与大小
userData = new FileStream(Application.UserAppDataPath + "//appdata.txt", FileMode.OpenOrCreate);

}
catch (IOException e)
{
// 程序退出时异常处理.
MessageBox.Show("An error occurred while attempting to show the application." +
"The error is:" + e.ToString());

// 退出当前主线程
ExitThread();
}

//窗体关闭
form1 = new AppForm1();
form1.Closed += new EventHandler(OnFormClosed);
form1.Closing += new CancelEventHandler(OnFormClosing);
formCount++;

form2 = new AppForm2();
form2.Closed += new EventHandler(OnFormClosed);
form2.Closing += new CancelEventHandler(OnFormClosing);
formCount++;

// 从文件中读取保存窗体位置与大小的参数
if (ReadFormDataFromFile())
{
Screen screen = Screen.FromRectangle(new Rectangle(0, 0, 0, 0));
form1.Top = 0; form1.Left = 0;
form2.Top = 0; form2.Left = screen.WorkingArea.Width / 2;
form1.StartPosition = FormStartPosition.Manual;
form2.StartPosition = FormStartPosition.Manual;

form1.Bounds = form1Position;
form2.Bounds = form2Position;
}

// 显示双窗体
form1.Show();
form2.Show();
}

private void OnApplicationExit(object sender, EventArgs e)
{
// 保存窗体位置与大小
WriteFormDataToFile();

try
{
// 忽略窗体关闭时可能出现的错误
userData.Close();
}
catch { }
}

private void OnFormClosing(object sender, CancelEventArgs e)
{
// 保存窗体位置与大小
if (sender is AppForm1)
form1Position = ((Form)sender).Bounds;
else if (sender is AppForm2)
form2Position = ((Form)sender).Bounds;
}

private void OnFormClosed(object sender, EventArgs e)
{
// 关闭所有窗体,退出主线程
formCount--;
if (formCount == 0)
{
ExitThread();
}
}

private bool WriteFormDataToFile()
{
// 保存窗体位置与大小到用户文件
UTF8Encoding encoding = new UTF8Encoding();

RectangleConverter rectConv = new RectangleConverter();
String form1pos = rectConv.ConvertToString(form1Position);
String form2pos = rectConv.ConvertToString(form2Position);

byte[] dataToWrite = encoding.GetBytes("~" + form1pos + "~" + form2pos);

try
{
userData.Seek(0, SeekOrigin.Begin);
userData.Write(dataToWrite, 0, dataToWrite.Length);
userData.Flush();

userData.SetLength(dataToWrite.Length);
return true;

}
catch
{
return false;
}

}

private bool ReadFormDataFromFile()
{
// 从文件中读取窗体大小与位置
UTF8Encoding encoding = new UTF8Encoding();
String data;

if (userData.Length != 0)
{
byte[] dataToRead = new Byte[userData.Length];

try
{
userData.Seek(0, SeekOrigin.Begin);
userData.Read(dataToRead, 0, dataToRead.Length);

}
catch (IOException e)
{
String errorInfo = e.ToString();
return false;
}

data = encoding.GetString(dataToRead);

try
{
// 转换数据文件为 rectangles
RectangleConverter rectConv = new RectangleConverter();
String form1pos = data.Substring(1, data.IndexOf("~", 1) - 1);

form1Position = (Rectangle)rectConv.ConvertFromString(form1pos);

String form2pos = data.Substring(data.IndexOf("~", 1) + 1);
form2Position = (Rectangle)rectConv.ConvertFromString(form2pos);

return true;

}
catch
{
return false;
}

}
else
{
// 无数据文件时,缺省窗体位置与大小
return false;
}
}

[STAThread]
static void Main(string[] args)
{
MyApplicationContext context = new MyApplicationContext();
Application.Run(context);
}
}

}
}

//以上代码保存成Program.cs,修改form名称后可以直接使用。源自MSDN,稍作修改
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: