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;
}
}
}
}
(0)

相关推荐

  • C#实现windows form拷贝内容到剪贴板的方法

    本文实例讲述了C#实现windows form拷贝内容到剪贴板的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Data; using System.Windows.Forms; public class MainClass { public static void Main(string[

  • C#实现WinForm禁止最大化、最小化、双击标题栏、双击图标等操作的方法

    本文实例讲述了C#实现WinForm禁止最大化.最小化.双击标题栏.双击图标等操作的方法.分享给大家供大家参考.具体实现方法如下: protected override void WndProc(ref Message m) { if (m.Msg==0x112) { switch ((int) m.WParam) { //禁止双击标题栏关闭窗体 case 0xF063: case 0xF093: m.WParam = IntPtr.Zero; break; //禁止拖拽标题栏还原窗体 case

  • C#实现winform用子窗体刷新父窗体及子窗体改变父窗体控件值的方法

    本文实例讲述了C#实现winform用子窗体刷新父窗体及子窗体改变父窗体控件值的方法.分享给大家供大家参考.具体如下: 第一种方法: 用委托,Form2和Form3是同一组 Form2 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows

  • C#实现在Form里面内嵌dos窗体的方法

    本文实例讲述了C#实现在Form里面内嵌dos窗体的方法.分享给大家供大家参考.具体如下: using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; namespace cmdForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

  • C#调用dos窗口获取相关信息的方法

    本文实例讲述了C#调用dos窗口获取相关信息的方法.分享给大家供大家参考.具体实现方法如下: /// <summary> /// 调用dos窗口获取相关信息 /// </summary> /// <param name="cmd">如:netstat-ano或者ipconfig</param> /// <returns></returns> static string GetCode(string cmd) { P

  • C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法

    特别是针对循环或timer处理中需要在窗体控件显示数据时,因后台处理过度繁忙而出现没刷新或者假死现象时,可以使用 复制代码 代码如下: Application.DoEvents(); Application.DoEvents()的作用 复制代码 代码如下: private void button1_Click(object sender, EventArgs e)         {             for (int i = 0; i < 10000; i++)            

  • C#实现Winform中打开网页页面的方法

    本文实例讲述了C#实现Winform中打开网页页面的方法.分享给大家供大家参考.具体实现方法如下: 1.首先比较简单的我们知道有类似的方法如下 复制代码 代码如下: System.Diagnostics.Process.Start("http://www.baidu.com"); 2.比较灵活一点,可以定义窗口大小,我们要实现网页中脚本打开页面的方法,即window.open 那么,我们必然会想,如何调用页面的脚本呢?其实可以利用WebBrowser来实现 //连接 string ur

  • C#执行DOS命令的方法

    本文实例讲述了C#执行DOS命令的方法.分享给大家供大家参考.具体实现方法如下: 在c#程序中,有时会用到调用cmd命令完成一些功能,本文介绍的如下方法,可实现c#执行DOS命令,并返回结果的功能. 复制代码 代码如下: //dosCommand Dos命令语句  public string Execute(string dosCommand)  {      return Execute(dosCommand, 10);  }  /// <summary>  /// 执行DOS命令,返回DO

  • C#实现Winform动态添加菜单的方法

    本文实例讲述了C#实现Winform动态添加菜单的方法.分享给大家供大家参考.具体分析如下: 最近在做WINFORM开发,一直都在为主界面的点击事件及动态加载菜单苦脑.现在已解决这个问题了,可以实现数据库或都XML等配置完成动态生成菜单及事件加载.代码如下: private void Form1_Load(object sender, EventArgs e) { //添加菜单一 ToolStripMenuItem subItem; subItem = AddContextMenu("入库&qu

  • C#中如何使用Winform实现炫酷的透明动画界面

    做过.NET Winform窗体美化的人应该都很熟悉UpdateLayeredWindow吧,UpdateLayeredWindow可以实现窗体的任意透明,效果很好,不会有毛边.不过使用这个API之后,会有一个问题就是无法使用普通控件,而且没有Paint消息.为了解决这个问题,有两种方法. 一.使用双层窗体,底层窗体使用UpdateLayeredWindow作为背景,上层窗体用普通窗体,并且可以使用TransparencyKey或者Region来实现去除不需要的窗体内容,让上层窗体能看到底层的窗

随机推荐