C# Winform多屏幕多显示器编程技巧实例

 更新时间:2015年06月04日 16:20:53   投稿:junjie  
这篇文章主要介绍了C# Winform多屏幕多显示器编程技巧实例,本文直接给出代码实例,需要的朋友可以参考下

在窗口的中间有一个System.Windows.Forms.PictureBox控件(该控件区域的面积为所在窗口的1/4),当该控件的大部分区域落在其中一台显示器时,在另一台显示器将不显示该控件,(该PictureBox控件将移动到主显示器所在的窗口区域)。 

实现方法:

using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
namespace WindowsApplication12 
{ 
/// <summary> 
/// Summary description for Form1. 
/// </summary> 
public class Form1 : System.Windows.Forms.Form 
{ 
private int tmpx = 0; 
private int tmpy = 0; 
private System.Windows.Forms.PictureBox pictureBox1; 
/// <summary> 
/// Required designer variable. 
/// </summary> 
private System.ComponentModel.Container components = null; 
System.Drawing.Rectangle[] ScreensRect; 
public Form1() 
{ 
// 
// Required for Windows Form Designer support 
// 
InitializeComponent(); 
// 
// TODO: Add any constructor code after InitializeComponent call 
// 
} 
/// <summary> 
/// Clean up any resources being used. 
/// </summary> 
protected override void Dispose( bool disposing ) 
{ 
if( disposing ) 
{ 
if (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.pictureBox1 = new System.Windows.Forms.PictureBox(); 
this.SuspendLayout(); 
// 
// pictureBox1 
// 
this.pictureBox1.BackColor = System.Drawing.SystemColors.HotTrack; 
this.pictureBox1.Location = new System.Drawing.Point(120, 88); 
this.pictureBox1.Name = "pictureBox1"; 
this.pictureBox1.Size = new System.Drawing.Size(248, 176); 
this.pictureBox1.TabIndex = 0; 
this.pictureBox1.TabStop = false; 
// 
// Form1 
// 
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
this.ClientSize = new System.Drawing.Size(504, 357); 
this.Controls.Add(this.pictureBox1); 
this.Name = "Form1"; 
this.Text = "Form1"; 
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); 
this.Load += new System.EventHandler(this.Form1_Load); 
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp); 
this.ResumeLayout(false); 
} 
#endregion 
/// <summary> 
/// The main entry point for the application. 
/// </summary> 
[STAThread] 
static void Main() 
{ 
Application.Run(new Form1()); 
} 
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
{ 
this.tmpx = e.X; 
this.tmpy = e.Y; 
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.form1_MouseMove); 
} 
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
{ 
this.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.form1_MouseMove); 
System.Drawing.Rectangle pictureBox1Rect=Screen.GetWorkingArea(pictureBox1); 
for(int i=0;i<ScreensRect.Length;i++) 
{ 
if(ScreensRect[i].X==pictureBox1Rect.X && ScreensRect[i].Y==pictureBox1Rect.Y) 
this.Location=new Point(ScreensRect[i].X,pictureBox1Rect.Y); 
} 
//MessageBox.Show(" WorkingArea:" + re.ToString()); 
} 
private void form1_MouseMove(object sender, MouseEventArgs e) 
{ 
this.Location = new System.Drawing.Point(this.Location.X + e.X - this.tmpx, this.Location.Y + e.Y - this.tmpy); 
} 
private void Form1_Load(object sender, System.EventArgs e) 
{ 
Screen[] s=Screen.AllScreens; 
ScreensRect=new Rectangle[s.Length]; 
for(int i=0;i<s.Length;i++) 
{ 
ScreensRect[i]= s[i].WorkingArea; 
} 
} 
} 
} 

相关文章

  • C#如何检测操作系统版本

    C#如何检测操作系统版本

    这篇文章主要为大家详细介绍了C#如何检测操作系统版本的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • C#中的delegate委托类型基本学习教程

    C#中的delegate委托类型基本学习教程

    这篇文章主要介绍了C#中的delegate委托类型基本学习教程,委托是C#语言所具有的一个重要特性,需要的朋友可以参考下
    2016-01-01
  • 在c#中把字符串转为变量名并获取变量值的小例子

    在c#中把字符串转为变量名并获取变量值的小例子

    这篇文章介绍了在c#中把字符串转为变量名并获取变量值的小例子,有需要的朋友可以参考一下
    2013-09-09
  • 经典排序算法之冒泡排序(Bubble sort)代码

    经典排序算法之冒泡排序(Bubble sort)代码

    这篇文章主要介绍了经典排序算法之冒泡排序(Bubble sort)代码的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • C#实现合并多张图片为GIF动态图

    C#实现合并多张图片为GIF动态图

    这篇文章主要为大家详细介绍了C#如何将把一张又一张的图片去拼合成一张GIF动态图片,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-12-12
  • 详解c#读取XML的实例代码

    详解c#读取XML的实例代码

    XML文件是一种常用的文件格式,本篇文章主要介绍了c#读取XML的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • C# winform登陆框验证码的实现方法

    C# winform登陆框验证码的实现方法

    这篇文章主要为大家详细介绍了C# winform登陆框验证码的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • C#合并BitMap图像生成超大bitmap

    C#合并BitMap图像生成超大bitmap

    当两个图像合并的时候,以简单的使用gdi+,当需要将许多bitmap合并时就会造成宽度过大,那么怎么实现C#合并BitMap图像,本文就详细的介绍一下
    2021-11-11
  • C#使用Dapper存取数据库详解

    C#使用Dapper存取数据库详解

    Dapper是的一个开源对象关系映射(ORM)库.NET和.NET核心应用程序,本文将介绍C#如何使用Dapper操作数据库,感兴趣的小伙伴可以了解一下
    2024-12-12
  • C#实现子类与父类的相互转换

    C#实现子类与父类的相互转换

    这篇文章主要介绍了C#实现子类与父类的相互转换,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05

最新评论