欢迎来到山村网

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

2019-03-02 14:00:21浏览:527 来源:山村网   
核心摘要:  这篇文章主要介绍了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; } } } }
(责任编辑:豆豆)
下一篇:

C#设置开机启动项、取消开机启动项

上一篇:

c#实现多线程局域网聊天系统

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com