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

   这篇文章主要介绍了C# Winform多屏幕多显示器编程技巧实例,本文直接给出代码实例,需要的朋友可以参考下

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

  实现方法:

  ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

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

时间: 2024-08-29 13:14:36

C# Winform多屏幕多显示器编程技巧实例的相关文章

Android编程常用技巧实例总结_Android

本文实例讲述了Android编程常用技巧.分享给大家供大家参考,具体如下: 1. 登录的时候,如果输入有误,则输入框左右震动,表示输入有误 在res下准备一个anim文件夹,里面包含两个文件,main_login_shake.xml 和 main_login_cycle_7.xml, 其中,main_login_shake.xml如下: <?xml version="1.0" encoding="utf-8"?> <translate xmlns:

VC实现Windows多显示器编程的方法_C 语言

本文实例讲述了VC实现Windows多显示器编程的方法.分享给大家供大家参考.具体如下: 一.Windows中接入多个显示器时,可设置为复制和扩展屏. 1.设置为复制屏幕时,多个显示器的分辨率是一样的,位置为0~分辨率值 2.设置为扩展屏幕时,显示器之间的关系比较复杂些.首先Windows系统会识别一个主显示器,这个可以在屏幕分辨率中更改.多个显示器之间的位置关系也可以再屏幕分辨率中更改.其中主显示器的位置为(0,0)到(width,height),其他显示器位置由与主显示器的位置关系决定,在主

MFC SDI单文档 状态栏 进度条 启动画面编程技巧 ...

      状态栏的编程离不开  CStatusBar类     CProgressCtrl  进 度条相关的控件      下面我们介绍状态栏 编程技巧  以及在状态栏添加进度条   应用程序的启动界面    状态栏的添加步骤     SDI 单文档中      下面是MFC给的 状态栏的创建步骤      Construct the CStatusBar object.    构造一个 CStatusBar对象    Call the Create (or CreateEx) functio

Android实用编程技巧代码总结

本文实例总结了Android实用编程技巧.分享给大家供大家参考,具体如下: 1.让一个图片透明: Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); buffer.eraseColor(Color.TRANSPARENT); 2.直接发送邮件: Intent intent = new Intent(Intent.ACTION_SENDTO, Uri .fromParts("mailto"

JavaApplet编程技巧

编程|技巧 1. Java Applet 概 述 Java Applet 程 序 分 为 两 类:Java 小 程 序(Applet) 和Java 应 用 程 序(Application), 这 两 类 程 序 是 有 区 别 的.Java Applet 嵌 入 在WWW 的 页 面, 作 为 页 面 的 组 成 部 分 被 下 载, 并 能 运 行 在 实 现Java 虚 机 器(JVM) 的Web 浏 览 器 中.Java 的 安 全 机 制 可 以 防 止 小 程 序 存 取 本 地 文

PHP编程技巧:看实例学正则表达式

编程|技巧|正则     首先,让我们看看两个特别的字符:'^' 和 '$' 他们是分别用来匹配字符串的开始和结束,一下分别举例说明: "^The": 匹配以 "The"开头的字符串; "of despair$": 匹配以 "of despair" 结尾的字符串; "^abc$": 匹配以abc开头和以abc结尾的字符串,实际上是只有abc与之匹配: "notice": 匹配包含noti

ASP编程技巧大全

编程|技巧 <% ''#######以下是一个类文件,下面的注解是调用类的方法################################################ ''# 注意:如果系统不支持建立Scripting.FileSystemObject对象,那么数据库压缩功能将无法使用 ''# Access 数据库类 ''# CreateDbFile 建立一个Access 数据库文件 ''# CompactDatabase 压缩一个Access 数据库文件 ''# 建立对象方法: ''#

关于我的自动生成编号的方法(来这么久,第一次写个人的编程技巧,不知能不能上精华)

编程|技巧|精华 前言:    用过许多序号的方法,indentity 或 new id() ,都不好用,自己写了一个,这个序号的特点是:每次取相应表中的系统当天最大序号,如果当天无记录,则自动生成一个当天序号 1.建种子表,这个表用来保存各个表目前已使用到的最大序号--种子表create table SEED (BM  varchar(20) not null,   --表名BH   varchar(12) not null,   --种子编号constraint PK_SEED primar

WCF技术剖析之三十:一个很有用的WCF调用编程技巧[上篇]

在进行基于会话信道的WCF服务调用中,由于受到并发信道数量的限制,我们需要及时的关闭信道:当遇到某些异常,我们需要强行中止(Abort)信道,相关的原理,可以参考我的文章<服务代理不能得到及时关闭会有什么后果?>.在真正的企业级开发中,正如我们一般不会让开发人员手工控制数据库连接的开启和关闭一样,我们一般也不会让开发人员手工去创建.开启.中止和关闭信道,这些工作是框架应该完成的操作.这篇文章,我们就来介绍如果通过一些编程技巧,让开发者能够无视"信道"的存在,像调用一个普通对