关于C#多屏幕显示器编程,请对此精通的高手解答,多谢! 在窗口的中间有一个System.Windows.Forms.PictureBox控件(该控件区域的面积为所在窗口的1/4),当该控件的大部分区域落在其中一台显示器时,在另一台显示器将不显示该控件,(该PictureBox控件将移动到主显示器所在的窗口区域)。是否应该调用GetMonitorInfo?请教高手如何处理?最好能给出个例子,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先你需要知道一个屏幕是如何分割成多个显示器显示的,即每个显示器显示多大region。而在初始化picturebox的时候,你可以通过Screen.PrimaryScreen.Bounds来获得屏幕大小,至于控件的位置,你可以先转换到屏幕坐标,然后按照前面获得比例尺寸(一个屏幕是如何分割成多个显示器显示的,即每个显示器显示多大region),最后进行判断处理。 有一个函数,可以读取一个窗口大部分区域所在的显示器,然后在根据显示器的属性确定窗体的显示状态,比如picbox的位置。 受了Knight94(愚翁)的启发,做了个测试,可以实现我需要的效果了,谢谢Knight94。测试代码如下: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; } } }} 我做过类似的项目。现在的显卡大部分都有二个输出,VGA/DVI/S端子。有些有3个输出的,但要在BIOS里设置只能同时用二个。win2000/xp/2003内置对多显示器支持,接上显示器后再开机系统就会认得出双输出了。在显示器的属性里选第二个显示器的“将桌面扩展到此显示器上”,然后可分别设置二个显示器的分辨率。要在二个显示器上显示Form其实也就是对设置Form的坐标就行了。比如二个显示器的分辨率都是800*600,那么第一个显示器的可视范围就是(0,1,800,600),第二个显示器的坐标范围是(801,601,1600,1200)。要把Form显示在哪个显示器上就把left,top属性设置到相应的显示器显示区域中就行了。在.net 2.0中system.windows.form命名空间里有个Screen类可以获取多显示器的信息的。可以参看我的这篇博客http://www.cnblogs.com/wuchang/archive/2006/07/01/440609.html还不行的可以联系我[email protected]qq 3263262 问个关于C#代码问题 c#调用vb的函数,遇到了一个很奇怪的事情,高手来看看。 winforms中datagrid如何隐藏一列? Control类实例可否脱离form存在 跪求Listbox的一个问题! 请教一个多个群聊的实现思路 请教:如何用代码设置 DataGrid 单元格的宽度 flash 的 as 是不是c# 写的 怎么把系统的任务栏隐藏掉? 简单问题:如何定义一个程序级的字符串? c#中怎样让一个窗口中弹出另一个窗口后,该窗口不可用,直到另一个窗口关闭后??? 在.net中实现mvc的一点思路,参与有分!!
而在初始化picturebox的时候,你可以通过
Screen.PrimaryScreen.Bounds
来获得屏幕大小,至于控件的位置,你可以先转换到屏幕坐标,然后按照前面获得比例尺寸(一个屏幕是如何分割成多个显示器显示的,即每个显示器显示多大region),最后进行判断处理。
测试代码如下:
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;
}
}
}
}
在显示器的属性里选第二个显示器的“将桌面扩展到此显示器上”,然后可分别设置二个显示器的分辨率。要在二个显示器上显示Form其实也就是对设置Form的坐标就行了。
比如二个显示器的分辨率都是800*600,那么第一个显示器的可视范围就是(0,1,800,600),第二个显示器的坐标范围是(801,601,1600,1200)。
要把Form显示在哪个显示器上就把left,top属性设置到相应的显示器显示区域中就行了。
在.net 2.0中system.windows.form命名空间里有个Screen类可以获取多显示器的信息的。
可以参看我的这篇博客
http://www.cnblogs.com/wuchang/archive/2006/07/01/440609.html还不行的可以联系我
[email protected]
qq 3263262