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

所属分类: 软件编程 / C#教程 阅读数: 36
收藏 0 赞 0 分享

在窗口的中间有一个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; 
} 
} 
} 
} 

更多精彩内容其他人还在看

winform用datagridview制作课程表实例

这篇文章主要介绍了winform用datagridview制作课程表的方法,实例分析了WinForm实现课程表的结构、数据库及调用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#中winform控制textbox输入只能为数字的方法

这篇文章主要介绍了C#中winform控制textbox输入只能为数字的方法,包括使用keyPress事件限制键盘输入以及TextChanged事件限制粘贴等情况,来实现控制输入为数字的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

C#省份城市下拉框联动简单实现方法

这篇文章主要介绍了C#省份城市下拉框联动简单实现方法,涉及字典的定义与索引的用法,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#处理MySql多个返回集的方法

这篇文章主要介绍了C#处理MySql多个返回集的方法,实现了对处理MySql多个返回集进行封装,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#无限参数的写法

这篇文章主要介绍了C#无限参数的写法,通过循环遍历再结合paras.Add方法实现无限参数的功能,是比较实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#反射应用实例

这篇文章主要介绍了C#反射应用,实例分析了通过反射实现多系统数据库的配置方法,是比较实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#窗体传值实例汇总

这篇文章主要介绍了C#窗体传值,实例形式汇总了静态变量传值、委托传值、对话框之间的传值等常见应用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#把数组中的某个元素取出来放到第一个位置的实现方法

这篇文章主要介绍了C#把数组中的某个元素取出来放到第一个位置的实现方法,涉及C#针对数组的常见操作技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#中Equality和Identity浅析

这篇文章主要介绍了C#中Equality和Identity浅析,本文先是讲解了Equality和Identity的定义,同时讲解了判断两个对象等价性的4种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

在Linux上运行C#的方法

这篇文章主要介绍了在Linux上运行C#的方法,实例分析了Linux平台下Mono软件包的应用技巧,以及在此基础之上的C#运行方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多