技术资讯
双屏收银软件开发:C# 将应用程序显示在扩展桌面
TIME:2018-09-29
有时候需要这个功能,就是通过代码将程序显示在扩展桌面,并且实现来回切换,
我看了好多blog,又是C++,又是window api,在这里无需任何api就可以实现,
超级简单 ,直接上代码
c# code,2个按钮事件
-
/// <summary>
-
/// 在屏幕一显示
-
/// </summary>
-
/// <param name="sender"></param>
-
/// <param name="e"></param>
-
private void scrren1Btn_Click(object sender, EventArgs e)
-
{
-
try
-
{
-
this.Left = 0;
-
this.Top = 0;
-
}
-
catch (Exception ex)
-
{
-
MessageBox.Show(ex.Message);
-
}
-
}
-
-
/// <summary>
-
/// 在屏幕二显示
-
/// </summary>
-
/// <param name="sender"></param>
-
/// <param name="e"></param>
-
private void scrren2Btn_Click(object sender, EventArgs e)
-
{
-
try
-
{
-
int x = Screen.PrimaryScreen.Bounds.Width;
-
this.Left = x;
-
this.Top = 0;
-
}
-
catch (Exception ex)
-
{
-
MessageBox.Show(ex.Message);
-
}
-
}
以上已经经过测试,可行,显示都是一样
猜想: 所谓屏幕扩展可能就是将屏幕的宽度变成2倍,如果把程序设置在大于一倍屏幕的时候,此程序窗口就到了扩展屏幕,那么就将程序显示在了扩展桌面
结果: 通过上面代码实现猜想。
相关资料,百度的,仅供参考
1)获取当前系统连接的屏幕数量: Screen.AllScreens.Count();
获取当前屏幕的名称: string CurrentScreenName = Screen.FromControl(this).DeviceName; 2)获取当前屏幕对象: Screen CurrentScreen = Screen.FromControl(this); 获取当前鼠标所在的屏幕: Screen CurrentScreen = Screen.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y)); 3)让窗体在第2个屏幕上显示,默认屏幕Index = 0、 第二个扩展屏幕Index=1……类推 //在第二个屏幕上,将窗体显示在屏幕中央 【没效果】 this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2); this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2);
注意: 以上代码只适合 StartPosition = FormStartPosition.Manual 情况
下面代码解决这个问题 showOnMonitor标识屏幕,从0开始
-
private void showOnMonitor(int showOnMonitor)
-
{
-
Screen[] sc;
-
sc = Screen.AllScreens;
-
if (showOnMonitor >= sc.Length)
-
{
-
showOnMonitor = 0;
-
}
-
-
this.StartPosition = FormStartPosition.Manual;
-
this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);
-
-
this.WindowState = FormWindowState.Normal;
-
this.WindowState = FormWindowState.Maximized;
-
}
只适合单个页面扩展,至于将整个界面扩展还没找到方法,还大神指点谢谢! --------------------- 本文来自 程序猿老黄 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/taoerit/article/details/52441805?utm_source=copy