您现在的位置是:网站首页> C#技术
C#多屏显示实现
- C#技术
- 2024-05-24
- 788人已阅读
C#多屏显示实现
Winform窗口实现多显示屏显示的2种方法
一台主机连接了2台显示器(2个显卡),要求一个程序的两个窗体在不同的显示器上显示:显示器1 显示From1,显示器2 显示From2。代码及说明如下
Form1不需要变更代码,From2添加如下代码:
// 方法一:
From2 frm2 = new From2();
if (Screen.AllScreens.Count() != 1)
{
frm2.Left = Screen.AllScreens[0].Bounds.Width;
frm2.Top = 0;
frm2.Size = new System.Drawing.Size(Screen.AllScreens[1].Bounds.Width, Screen.AllScreens[1].Bounds.Height);
}
// 方法二:
this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2);
this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2);
this.Size = new System.Drawing.Size(Screen.AllScreens[1].Bounds.Width, Screen.AllScreens[1].Bounds.Height);
C# winform 双屏显示
双屏显示1
// 利用WinForm中的Screen类,即可比较方便地实现多窗体分别在多个屏幕上显示。
//?获取当前系统连接的屏幕数量: Screen.AllScreens.Count();
//?获取当前屏幕的名称:string CurrentScreenName = Screen.FromControl(this).DeviceName;
//?获取当前屏幕对象:Screen CurrentScreen = Screen.FromControl(this);
//?获取当前鼠标所在的屏幕:Screen CurrentScreen = Screen.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y));
//?让窗体在第2个屏幕上显示:
//this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2);
//this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2);
private void showOnMonitor(int showOnMonitor)
{
Screen[] sc;
sc = Screen.AllScreens;
if (showOnMonitor >= sc.Length)
{
showOnMonitor = 0;
}
this.FormBorderStyle = FormBorderStyle.None; //无边框全屏显示
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);
//this.Location = new Point(((sc[showOnMonitor].Bounds.Width-this.Width)/2), ((sc[showOnMonitor].Bounds.Height-this.Height)/2));
// If you intend the form to be maximized, change it to normal then maximized.
//this.WindowState = FormWindowState.Normal;
this.WindowState = FormWindowState.Maximized;//最大化窗口
}
如果接双显卡时showOnMonitor 参数等于0为主屏,1为扩展屏
双屏显示2
private void showOnMonitor2()
{
Screen[] sc;
sc = Screen.AllScreens;
//get all the screen width and heights
Form1 f = new Form1();
f.FormBorderStyle = FormBorderStyle.None;
f.Left = sc[1].Bounds.Width;
f.Top = sc[1].Bounds.Height;
f.StartPosition = FormStartPosition.Manual;
f.Location = sc[1].Bounds.Location;
Point p = new Point(sc[1].Bounds.Location.X, sc[1].Bounds.Location.Y);
f.Location = p;
f.WindowState = FormWindowState.Maximized;
f.Show();
}
接入双显卡时sc[0]为主屏、sc[1]扩展屏
一个窗体双屏显示
his.Location = new Point(0,0);
Screen[] sc;
sc = Screen.AllScreens;
this.Width = (sc[0].Bounds.Width + sc[1].Bounds.Width);//+20;
this.Height = (sc[0].Bounds.Height); //+200;
this.FormBorderStyle = FormBorderStyle.None; //边框样式
webBrowser1.Width = sc[0].Bounds.Width;
webBrowser1.Height = sc[0].Bounds.Height;
webBrowser1.Location = new Point(sc[0].Bounds.Location.X, sc[0].Bounds.Location.Y);
webBrowser1.Url = new Uri("http://www.google.com.hk");
webBrowser2.Width = sc[1].Bounds.Width;
webBrowser2.Height = sc[1].Bounds.Height;
webBrowser2.Location = new Point(sc[1].Bounds.Location.X, sc[1].Bounds.Location.Y);
webBrowser2.Url = new Uri("http://www.baidu.com");
C#设置双屏显示模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace RecordSystem.Common
{
public static class ScreenHelper
{
private const uint SDC_APPLY = 0x00000080;
private const uint SDC_TOPOLOGY_INTERNAL = 0x00000001;
private const uint SDC_TOPOLOGY_CLONE = 0x00000002;
/// <summary>
/// 扩展模式
/// </summary>
private const uint SDC_TOPOLOGY_EXTEND = 0x00000004;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern long SetDisplayConfig(uint numPathArrayElements, IntPtr pathArray, uint numModeArrayElements,
IntPtr modeArray, uint flags);
/// <summary>
/// 设置屏幕的显示模式
/// </summary>
/// <param name="displayModel"></param>
/// <returns></returns>
private static bool SetScreen(uint displayModel)
{
return SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, SDC_APPLY | displayModel) == 0;
}
/// <summary>
/// 设置屏幕的显示模式
/// </summary>
/// <param name="type">模式(0 - 主屏 1 - 双屏复制 2 - 双屏扩展</param>
/// <returns></returns>
public static bool SetScreenMode(int type)
{
uint smode ;
switch (type)
{
case 0:
smode = SDC_APPLY | SDC_TOPOLOGY_INTERNAL ;
break;
case 1:
smode = SDC_APPLY | SDC_TOPOLOGY_CLONE ;
break;
case 2:
smode = SDC_APPLY | SDC_TOPOLOGY_EXTEND ;
break;
default :
smode = SDC_APPLY | SDC_TOPOLOGY_INTERNAL ;
break;
}
return SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, smode) == 0;
}
}
上一篇:MyCat使用技术收集