关闭

[wy231的专栏]关于C#多屏幕显示器编程

1725人阅读 评论(0) 收藏 举报
 关于C#多屏幕显示器编程

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

首先你需要知道一个屏幕是如何分割成多个显示器显示的,即每个显示器显示多大region。
而在初始化picturebox的时候,你可以通过
Screen.PrimaryScreen.Bounds
来获得屏幕大小,至于控件的位置,你可以先转换到屏幕坐标,然后按照前面获得比例尺寸(一个屏幕是如何分割成多个显示器显示的,即每个显示器显示多大region),最后进行判断处理。

 

多屏幕编程简单么?
比如分辨率1024*768
第一个屏幕就是0~1024,第二个屏幕就是1025~2048,....
如果你要把某一画面显示在第二个屏幕。。。只要把它的x,y位置即可。。。

 

受了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;
}
}
}
}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=742012

0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:306318次
    • 积分:4271
    • 等级:
    • 排名:第7050名
    • 原创:79篇
    • 转载:33篇
    • 译文:55篇
    • 评论:103条
    文章分类
    最新评论