C# Winform多屏幕多显示器编程技巧实例
作者:junjie 发布时间:2021-09-19 16:49:09
标签: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#观察者模式,分享给大家供大家参考。具体实现方法如下:现在假设有一个软件公司,每当有新产品推出,就把信息通知到一些
- 使用wait()和notify()实现Java多线程通信:两个线程交替打印A和B,如ABABABpublic class Test { &n
- RocketMq消息处理RocketMq消息处理整个流程如下:本系列RocketMQ4.8注释github地址,希望对大家有所帮助,要是觉得
- 对这种懒加载问题,最后的做法是利用Spring提供的一个针对Hibernate的一个支持类,其主要意思是在发起一个页面请求时打开Hibern
- 一. 线性表中的顺序表线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见
- springboot部署项目在linux的两种方式 可以选择 war包方式或者jar包方式(个人推荐使用jar方式)1.springboot
- 本文实例为大家分享了java实现人机猜拳游戏的具体代码,供大家参考,具体内容如下完成人机猜拳互动游戏的开发,用户通过控制台输入实现出拳,电脑
- 最近博主开始在项目中实践MVP模式,却意外发现内存泄漏比较严重,但却很少人谈到这个问题,促使了本文的发布,本文假设读者已了解MVP架构。MV
- 在拼接绝对路径的网址时,经常需要从Request.Url中获取根网址(比如https://git.oschina.net),然后与相对路径一
- 前言在 Java 开发领域,热部署一直是一个难以解决的问题,目前的 Java 虚拟机只能实现方法体的修改热部署,例如使用devtool来实现
- 可重入锁,从字面来理解,就是可以重复进入的锁。可重入锁,也叫做递归锁,指的是同一线程外层函数获得锁之后,内层递归函数仍然有获取该锁的代码,但
- 前言通过此篇文章,你将了解到:Flutter如何在Android上实现多窗口机制;Flutter与Android的事件机制和冲突解决;Flu
- 发现问题在C#连接SQL Server时,明明添加了引用using System.Data.SqlClient;却出现了下面这种情况:原因:
- /** * 考拉兹猜想:Collatz Conjecture * 又称为3n+1猜想、冰雹猜想、角谷猜想、哈塞猜想、乌拉姆猜想或叙拉古猜
- 一.前言:CentOS7.0虽然自带JDK1.7和1.8,运行“java -version”命令也可以看到版本信息,但是jdk的安装环境不全
- 本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正。什么是ZXing.Net
- 代码思路:想要循环遍历文件夹下所有子文件夹,就要用到递归。首先判断路径是否存在:是:获取文件判断是否文件夹:是:调用自身,继续获取子文件夹下
- 由于公司平台访问人数逐渐增多,公司项目的数据库已经几次出现宕机现象。为减轻数据库压力,我上个月对公司项目做了下调整。把新闻板块提取出来单独一
- 一.延续任务private async static void CallerWithAsync() {