Article directory
Preface
When developing WinForm applications, sometimes we need to display an interface with the same content as the main interface on another screen. This article will explain how to use C# to clone a running interface and display the same content on another screen.
1. Preparation
Before you begin, make sure your computer has multiple screens connected and that theSystem.Windows.Forms
namespace is referenced.
2. Steps
- Create a main interface instance:
Form mainForm = new Form();
- Clone the already running interface:
Form clonedForm = (Form)mainForm.Clone();
- Create a new screen object and position the cloned interface on that screen:
Screen secondaryScreen = Screen.AllScreens.Length > 1 ? Screen.AllScreens[1] : Screen.PrimaryScreen;
clonedForm.StartPosition = FormStartPosition.Manual;
clonedForm.Location = secondaryScreen.Bounds.Location;
- Display the cloned interface:
clonedForm.Show();
- Display the main interface:
Application.Run(mainForm);
3 Sample code
using System;
using System.Windows.Forms;
namespace YourAppName
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// 创建主界面实例
Form mainForm = new Form();
// 克隆已经运行的界面
Form clonedForm = (Form)mainForm.Clone();
// 创建一个新的屏幕
Screen secondaryScreen = Screen.AllScreens.Length > 1 ? Screen.AllScreens[1] : Screen.PrimaryScreen;
clonedForm.StartPosition = FormStartPosition.Manual;
clonedForm.Location = secondaryScreen.Bounds.Location;
// 显示克隆的界面
clonedForm.Show();
// 显示主界面
Application.Run(mainForm);
}
}
}
4 Conclusion
By using Clone
methods and Screen
classes in C#, we can easily clone the running WinForm interface and display the same on another screen Content. This provides us with more interface display and interaction possibilities.