欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-09 14:13:04
在窗口的中间有一个system.windows.forms.picturebox控件(该控件区域的面积为所在窗口的1/4),当该控件的大部分区域落在其中一台显示器时,在另一...

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