C# Winform多屏幕多顯示器編程技巧實例
更新時間:2015年06月04日 16:20:53 投稿:junjie
這篇文章主要介紹了C# Winform多屏幕多顯示器編程技巧實例,本文直接給出代碼實例,需要的朋友可以參考下
在窗口的中間有一個System.Windows.Forms.PictureBox控件(該控件區(qū)域的面積為所在窗口的1/4),當(dāng)該控件的大部分區(qū)域落在其中一臺顯示器時,在另一臺顯示器將不顯示該控件,(該PictureBox控件將移動到主顯示器所在的窗口區(qū)域)。
實現(xiàn)方法:
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#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- C#實現(xiàn)WinForm禁止最大化、最小化、雙擊標(biāo)題欄、雙擊圖標(biāo)等操作的方法
- C#中如何使用Winform實現(xiàn)炫酷的透明動畫界面
- C#實現(xiàn)winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法
- C# WinForm程序處理后臺繁忙導(dǎo)致前臺控件假死現(xiàn)象解決方法
- C#實現(xiàn)windows form拷貝內(nèi)容到剪貼板的方法
- C#實現(xiàn)Winform動態(tài)添加菜單的方法
- C#調(diào)用dos窗口獲取相關(guān)信息的方法
- C#執(zhí)行DOS命令的方法
- C#實現(xiàn)在Form里面內(nèi)嵌dos窗體的方法
相關(guān)文章
C#中的delegate委托類型基本學(xué)習(xí)教程
這篇文章主要介紹了C#中的delegate委托類型基本學(xué)習(xí)教程,委托是C#語言所具有的一個重要特性,需要的朋友可以參考下2016-01-01
在c#中把字符串轉(zhuǎn)為變量名并獲取變量值的小例子
這篇文章介紹了在c#中把字符串轉(zhuǎn)為變量名并獲取變量值的小例子,有需要的朋友可以參考一下2013-09-09
經(jīng)典排序算法之冒泡排序(Bubble sort)代碼
這篇文章主要介紹了經(jīng)典排序算法之冒泡排序(Bubble sort)代碼的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06

