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

本文提供了一个C#示例,展示如何使用ApplicationContext类来启动并同时显示两个窗体,窗体的位置在关闭时会被保存,再次启动时会恢复。应用程序在所有窗体关闭后退出。代码详细解释了如何处理窗体的打开、关闭以及位置存储。
摘要由CSDN通过智能技术生成

下面的代码示例显示两个窗体,并在两个窗体都关闭时退出应用程序。在应用程序启动和退出时,它会记住每个窗体的位置。此示例演示如何使用 ApplicationContextApplication.Run(context) 方法在启动应用程序时显示多个窗体。

MyApplicationContextApplicationContext 继承,并跟踪每个窗体关闭的时间,然后在这两个窗体均关闭时退出当前线程。该类为用户存储每个窗体的位置。窗体位置数据存储在标题为 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);
   

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值