如何实现winform一个窗体在多个屏幕上同时显示

zkj513703929 2018-06-05 02:36:50
如题所示,电脑外接两个显示屏,在电脑上打开后两个显示屏幕上同时显示相同的信息,窗体是winform窗体,我现在用如下代码
  this.StartPosition = FormStartPosition.Manual;

Screen[] screens = System.Windows.Forms.Screen.AllScreens;
if (screens.Length >= 2)
{
for (int i = 0; i < screens.Length; i++)
{
//获取扩展屏
Screen childerScreen = screens[i];
this.Width = childerScreen.Bounds.Width;
this.Height = childerScreen.Bounds.Height;
this.Location = childerScreen.WorkingArea.Location;
}
}

只在外接的一个屏幕上显示,另外一个屏幕上没有显示,不知道C# 是否有复制的功能
请知道的指点一下,谢谢!
...全文
1917 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2018-06-06
  • 打赏
  • 举报
回复
两个窗体数据同步:https://bbs.csdn.net/topics/360140208/
zkj513703929 2018-06-06
  • 打赏
  • 举报
回复
引用 11 楼 xomix 的回复:
代码随手写的,懒得用什么命名规则命名。 逻辑自己看,效果就是一个窗体点击可以新增窗体,一个更新按钮更新后全部窗体都会更新text。 原理代码里有,我懒得写注释了。 第一个窗体的代码真的太长上传不上来,你要是想要就回帖,然后我切分一下看能不能上传。
非常感谢
  • 打赏
  • 举报
回复
接下来我开始放代码,窗体设计代码和窗体代码都放出来,program.cs就不放了,就调用form1就好了。 Forms1.Designer.cs 放不出来代码了,太长了,放个截图自己设计吧。 就只有一个按钮绑定事件,这个你应该会。 Forms1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 测试集合
{
    public partial class Forms1 : Form
    {
        public Dictionary<string,string> baseText { get; set; }
        public event EventHandler ReSetTxt;

        public Forms1()
        {
            InitializeComponent();
        }

        public void Reset()
        {
            foreach (Control item in this.Controls)
            {
                Reset(item);
            }
        }

        private void Reset(Control con)
        {
            if (baseText.ContainsKey(con.Name))
            {
                con.Text = baseText[con.Name];
            }
            if (!con.HasChildren)
            {
                return;
            }
            foreach (Control c in con.Controls)
            {
                if (baseText.ContainsKey(c.Name))
                {
                    c.Text = baseText[c.Name];
                }
                if (c.HasChildren)
                {
                    Reset(c);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Set();
            Reset();
            ReSetTxt(sender, e);
        }

        private void Set()
        {
            foreach (Control item in this.Controls)
            {
                Set(item);
            }
        }

        private void Set(Control con)
        {
            if (baseText.ContainsKey(con.Name))
            {
                baseText[con.Name] = con.Text;
            }
            else
            {
                baseText.Add(con.Name, con.Text);
            }
            if (!con.HasChildren)
            {
                return;
            }
            foreach (Control c in con.Controls)
            {
                if (baseText.ContainsKey(c.Name))
                {
                    baseText[c.Name] = c.Text;
                }
                else
                {
                    baseText.Add(c.Name, c.Text);
                }

                if (c.HasChildren)
                {
                    Set(c);
                }
            }
        }

    }
}
  • 打赏
  • 举报
回复
代码随手写的,懒得用什么命名规则命名。 逻辑自己看,效果就是一个窗体点击可以新增窗体,一个更新按钮更新后全部窗体都会更新text。 原理代码里有,我懒得写注释了。 第一个窗体的代码真的太长上传不上来,你要是想要就回帖,然后我切分一下看能不能上传。
  • 打赏
  • 举报
回复
Form1.Designer.cs

namespace 测试集合
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(362, 222);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(117, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "新增一个窗体";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(13, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(120, 17);
            this.label1.TabIndex = 1;
            this.label1.Text = "联动窗体测试程序";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(491, 257);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
    }
}
Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 测试集合
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Dictionary<string, string> lsttxt = new Dictionary<string, string>();

        private void button1_Click(object sender, EventArgs e)
        {
            Forms1 f = new Forms1();
            f.baseText = lsttxt;
            string guid = Guid.NewGuid().ToString();
            f.Tag = guid;
            f.ReSetTxt += (fs,fe)=>{
                Control c = (Control)fs;
                Forms1 fs1 = new Forms1();

                while (c.Parent!=null)
                {
                    c = c.Parent;
                }
                fs1 = (Forms1)c;
                Dictionary<string, string> basetext=new Dictionary<string,string>();
                foreach (Form item in Application.OpenForms)
                {
                    if (item.Tag != null && fs1.Tag.ToString() == item.Tag.ToString())
                    {
                        basetext = ((Forms1)item).baseText;
                    }
                }
                foreach (Form item in Application.OpenForms)
                {
                    if (item.Tag != null && fs1.Tag.ToString() != item.Tag.ToString())
                    {
                        Forms1 f1 = (Forms1)item;
                        f1.baseText = basetext;
                        f1.Reset();
                    }
                }
            };
            f.Show();
        }
    }
}
zkj513703929 2018-06-05
  • 打赏
  • 举报
回复
引用 4 楼 zkj513703929 的回复:
[quote=引用 1 楼 xomix 的回复:] [quote=引用 楼主 zkj513703929 的回复:] 如题所示,电脑外接两个显示屏,在电脑上打开后两个显示屏幕上同时显示相同的信息,窗体是winform窗体,我现在用如下代码
  this.StartPosition = FormStartPosition.Manual;

            Screen[] screens = System.Windows.Forms.Screen.AllScreens;
            if (screens.Length >= 2)
            {
                for (int i = 0; i < screens.Length; i++)
                {
                    //获取扩展屏
                    Screen childerScreen = screens[i];
                    this.Width = childerScreen.Bounds.Width;
                    this.Height = childerScreen.Bounds.Height;
                    this.Location = childerScreen.WorkingArea.Location;
                }
            }
只在外接的一个屏幕上显示,另外一个屏幕上没有显示,不知道C# 是否有复制的功能 请知道的指点一下,谢谢!
你这段代码是把你的this在所有的屏幕里面移动一圈,最后停留在最后一个屏幕。 随口吐槽一下大于等于2和大于1有什么区别呢? 复制屏幕是系统级别操作的,如果说你想要延展到扩展屏幕或者利用扩展屏幕展示另外的窗口那是很好做的。 看到上一句话你应该也想到展示的另外的窗口如果是当前窗口的复制体呢,对这样就能实现功能,但是你自己要注意两个不同的窗口之间的联动。[/quote] 请问如果是扩展屏幕或者利用扩展屏幕展示另外的窗口,如何处理呢,复制窗体显示上面已经有人回答了,想深入了解一下如果扩展多个屏幕,谢谢
  • 打赏
  • 举报
回复
引用 4 楼 zkj513703929 的回复:
[quote=引用 1 楼 xomix 的回复:] [quote=引用 楼主 zkj513703929 的回复:] 如题所示,电脑外接两个显示屏,在电脑上打开后两个显示屏幕上同时显示相同的信息,窗体是winform窗体,我现在用如下代码
  this.StartPosition = FormStartPosition.Manual;

            Screen[] screens = System.Windows.Forms.Screen.AllScreens;
            if (screens.Length >= 2)
            {
                for (int i = 0; i < screens.Length; i++)
                {
                    //获取扩展屏
                    Screen childerScreen = screens[i];
                    this.Width = childerScreen.Bounds.Width;
                    this.Height = childerScreen.Bounds.Height;
                    this.Location = childerScreen.WorkingArea.Location;
                }
            }
只在外接的一个屏幕上显示,另外一个屏幕上没有显示,不知道C# 是否有复制的功能 请知道的指点一下,谢谢!
你这段代码是把你的this在所有的屏幕里面移动一圈,最后停留在最后一个屏幕。 随口吐槽一下大于等于2和大于1有什么区别呢? 复制屏幕是系统级别操作的,如果说你想要延展到扩展屏幕或者利用扩展屏幕展示另外的窗口那是很好做的。 看到上一句话你应该也想到展示的另外的窗口如果是当前窗口的复制体呢,对这样就能实现功能,但是你自己要注意两个不同的窗口之间的联动。[/quote] 首先要表示感谢,大于1和大于2这个可以忽略不计,我要的就是当前窗体的复制体,现在就是不知道如何联动了[/quote] 如果你想要联动很简单的,首先是窗体上的数据必须要可以刷新,然后就任意事件都刷新当前窗体即可。至于数据建议保存在另外一个位置不在两个窗体上,事件变更的是数据,然后刷新两个窗体即可。
zkj513703929 2018-06-05
  • 打赏
  • 举报
回复
引用 2 楼 duanzi_peng 的回复:
这个设置屏幕的分辨率相关操作就行了吧,在多显示器中选择“复制这些显示”即可。 查查相关的设置步骤
谢谢
zkj513703929 2018-06-05
  • 打赏
  • 举报
回复
引用 3 楼 qq_17486399 的回复:
改一下分辨率里面这个属性就可以实现两个屏同时显示了啊
首先感谢,我先试试
zkj513703929 2018-06-05
  • 打赏
  • 举报
回复
引用 1 楼 xomix 的回复:
[quote=引用 楼主 zkj513703929 的回复:] 如题所示,电脑外接两个显示屏,在电脑上打开后两个显示屏幕上同时显示相同的信息,窗体是winform窗体,我现在用如下代码
  this.StartPosition = FormStartPosition.Manual;

            Screen[] screens = System.Windows.Forms.Screen.AllScreens;
            if (screens.Length >= 2)
            {
                for (int i = 0; i < screens.Length; i++)
                {
                    //获取扩展屏
                    Screen childerScreen = screens[i];
                    this.Width = childerScreen.Bounds.Width;
                    this.Height = childerScreen.Bounds.Height;
                    this.Location = childerScreen.WorkingArea.Location;
                }
            }
只在外接的一个屏幕上显示,另外一个屏幕上没有显示,不知道C# 是否有复制的功能 请知道的指点一下,谢谢!
你这段代码是把你的this在所有的屏幕里面移动一圈,最后停留在最后一个屏幕。 随口吐槽一下大于等于2和大于1有什么区别呢? 复制屏幕是系统级别操作的,如果说你想要延展到扩展屏幕或者利用扩展屏幕展示另外的窗口那是很好做的。 看到上一句话你应该也想到展示的另外的窗口如果是当前窗口的复制体呢,对这样就能实现功能,但是你自己要注意两个不同的窗口之间的联动。[/quote] 首先要表示感谢,大于1和大于2这个可以忽略不计,我要的就是当前窗体的复制体,现在就是不知道如何联动了
大鱼> 2018-06-05
  • 打赏
  • 举报
回复

改一下分辨率里面这个属性就可以实现两个屏同时显示了啊
  • 打赏
  • 举报
回复
引用 楼主 zkj513703929 的回复:
如题所示,电脑外接两个显示屏,在电脑上打开后两个显示屏幕上同时显示相同的信息,窗体是winform窗体,我现在用如下代码
  this.StartPosition = FormStartPosition.Manual;

            Screen[] screens = System.Windows.Forms.Screen.AllScreens;
            if (screens.Length >= 2)
            {
                for (int i = 0; i < screens.Length; i++)
                {
                    //获取扩展屏
                    Screen childerScreen = screens[i];
                    this.Width = childerScreen.Bounds.Width;
                    this.Height = childerScreen.Bounds.Height;
                    this.Location = childerScreen.WorkingArea.Location;
                }
            }
只在外接的一个屏幕上显示,另外一个屏幕上没有显示,不知道C# 是否有复制的功能 请知道的指点一下,谢谢!
你这段代码是把你的this在所有的屏幕里面移动一圈,最后停留在最后一个屏幕。 随口吐槽一下大于等于2和大于1有什么区别呢? 复制屏幕是系统级别操作的,如果说你想要延展到扩展屏幕或者利用扩展屏幕展示另外的窗口那是很好做的。 看到上一句话你应该也想到展示的另外的窗口如果是当前窗口的复制体呢,对这样就能实现功能,但是你自己要注意两个不同的窗口之间的联动。
  • 打赏
  • 举报
回复
这个设置屏幕的分辨率相关操作就行了吧,在多显示器中选择“复制这些显示”即可。 查查相关的设置步骤

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧