C# Winform多屏幕多显示器编程技巧实例
作者:junjie 时间:2021-09-19 16:49:09
在窗口的中间有一个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#,Winform,多屏幕,多显示器,编程
0
投稿
猜你喜欢
C#中Span相关的性能优化建议
2021-07-05 15:42:26
基于WPF实现简单放大镜效果
2022-02-15 23:19:12
使用 C# 下载文件的多种方法小结
2023-11-08 06:59:37
Java基础详解之内存泄漏
2022-04-14 03:45:46
java开发之Jdbc分页源码详解
2021-10-28 16:06:48
Spring容器注册组件实现过程解析
2023-07-10 11:08:07
Android蓝牙服务查找附近设备分析探索
2023-04-01 19:50:21
Android中Activity之间跳转和参数传递的实例
2023-01-31 21:40:32
浅析Java中Split函数的用法技巧
2023-02-19 09:13:27
C#实现随机数产生类实例
2021-11-16 15:46:49
使用java基础类实现zip压缩和zip解压工具类分享
2021-11-23 08:03:41
Android编程实现滑动按钮功能详解
2022-04-14 04:58:39
C#实现一键换IP、重置DNS、网关及掩码的方法
2021-10-03 00:32:33
图文浅析Java序列化和反序列化
2022-08-03 17:46:59
Mybatis Plus使用XML编写动态sql的超简易方法
2022-02-20 01:49:16
java后端解决跨域的几种问题解决
2022-01-05 06:34:24
浅谈java中异常抛出后代码是否会继续执行
2023-01-23 18:37:45
C# 时间与时间戳互转的方法(13位)
2023-12-04 10:26:39
Spring Boot利用@Async如何实现异步调用:自定义线程池
2021-11-09 17:32:11
Java基于ShardingSphere实现分库分表的实例详解
2022-04-20 18:43:37