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#读写INI文件的方法

    C#读写INI文件的方法

    这篇文章主要介绍了C#读写INI文件的方法,涉及C#读写ini文件的相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • C#实现图像选择验证码的示例代码

    C#实现图像选择验证码的示例代码

    为了防止网站被非法登陆,网站一般通过验证码的方式,防止黑客用软件非法登陆,本文主要介绍了C#实现图像选择验证码的示例代码,具有一定的参考价值,感兴趣的可以了解一下
    2023-08-08
  • C#做线形图的方法

    C#做线形图的方法

    在本篇内容中小编给大家总结了C#怎么做线形图的教程内容,对此有需要的朋友们可以跟着学习下。
    2018-12-12
  • c# List和Dictionary常用的操作

    c# List和Dictionary常用的操作

    这篇文章主要介绍了c# List和Dictionary常用的操作,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-04-04
  • c#关于非托管内存的释放问题及解读

    c#关于非托管内存的释放问题及解读

    这篇文章主要介绍了c#关于非托管内存的释放问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • C#权限管理和设计浅谈

    C#权限管理和设计浅谈

    权限管理是很多软件中相当重要的一个模块——它的设计的好坏直接影响到软件的安全性、权限管理的可扩展性和易操作性 以及代码中权限判断的复杂程度和效率等方面
    2012-02-02
  • C#实现拆分合并Word表格中的单元格

    C#实现拆分合并Word表格中的单元格

    我们在使用Word制作表格时,由于表格较为复杂,只是简单的插入行、列并不能满足我们的需要。要做一个完整的表格,很多时候需要将单元格进行拆分或者合并。本文将详细为您介绍在Word表格中拆分或合并单元格的思路及方法,希望对大家有所帮助
    2022-12-12
  • C#简单了解接口(Interface)使用方法

    C#简单了解接口(Interface)使用方法

    这篇文章主要介绍了C#简单了解接口(Interface)使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • C# XML操作类分享

    C# XML操作类分享

    这篇文章主要分享了C# XML操作类的实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C# yield在WCF中的错误用法(一)

    C# yield在WCF中的错误用法(一)

    这篇文章主要介绍了C# yield在WCF中的错误使用(一),本文讲解的内容据说是99%的开发人员都有可能犯的错误,需要的朋友可以参考下
    2015-04-04

最新评论