C# Winform多屏幕多显示器编程技巧实例
在窗口的中间有一个System.Windows.Forms.PictureBox控件(该控件区域的面积为所在窗口的1/4),当该控件的大部分区域落在其中一台显示器时,在另一台显示器将不显示该控件,(该PictureBox控件将移动到主显示器所在的窗口区域)。
实现方法:
usingSystem; usingSystem.Drawing; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Windows.Forms; usingSystem.Data; namespaceWindowsApplication12 { ///<summary> ///SummarydescriptionforForm1. ///</summary> publicclassForm1:System.Windows.Forms.Form { privateinttmpx=0; privateinttmpy=0; privateSystem.Windows.Forms.PictureBoxpictureBox1; ///<summary> ///Requireddesignervariable. ///</summary> privateSystem.ComponentModel.Containercomponents=null; System.Drawing.Rectangle[]ScreensRect; publicForm1() { // //RequiredforWindowsFormDesignersupport // InitializeComponent(); // //TODO:AddanyconstructorcodeafterInitializeComponentcall // } ///<summary> ///Cleanupanyresourcesbeingused. ///</summary> protectedoverridevoidDispose(booldisposing) { if(disposing) { if(components!=null) { components.Dispose(); } } base.Dispose(disposing); } #regionWindowsFormDesignergeneratedcode ///<summary> ///RequiredmethodforDesignersupport-donotmodify ///thecontentsofthismethodwiththecodeeditor. ///</summary> privatevoidInitializeComponent() { this.pictureBox1=newSystem.Windows.Forms.PictureBox(); this.SuspendLayout(); // //pictureBox1 // this.pictureBox1.BackColor=System.Drawing.SystemColors.HotTrack; this.pictureBox1.Location=newSystem.Drawing.Point(120,88); this.pictureBox1.Name="pictureBox1"; this.pictureBox1.Size=newSystem.Drawing.Size(248,176); this.pictureBox1.TabIndex=0; this.pictureBox1.TabStop=false; // //Form1 // this.AutoScaleBaseSize=newSystem.Drawing.Size(5,13); this.ClientSize=newSystem.Drawing.Size(504,357); this.Controls.Add(this.pictureBox1); this.Name="Form1"; this.Text="Form1"; this.MouseDown+=newSystem.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); this.Load+=newSystem.EventHandler(this.Form1_Load); this.MouseUp+=newSystem.Windows.Forms.MouseEventHandler(this.Form1_MouseUp); this.ResumeLayout(false); } #endregion ///<summary> ///Themainentrypointfortheapplication. ///</summary> [STAThread] staticvoidMain() { Application.Run(newForm1()); } privatevoidForm1_MouseDown(objectsender,System.Windows.Forms.MouseEventArgse) { this.tmpx=e.X; this.tmpy=e.Y; this.MouseMove+=newSystem.Windows.Forms.MouseEventHandler(this.form1_MouseMove); } privatevoidForm1_MouseUp(objectsender,System.Windows.Forms.MouseEventArgse) { this.MouseMove-=newSystem.Windows.Forms.MouseEventHandler(this.form1_MouseMove); System.Drawing.RectanglepictureBox1Rect=Screen.GetWorkingArea(pictureBox1); for(inti=0;i<ScreensRect.Length;i++) { if(ScreensRect[i].X==pictureBox1Rect.X&&ScreensRect[i].Y==pictureBox1Rect.Y) this.Location=newPoint(ScreensRect[i].X,pictureBox1Rect.Y); } //MessageBox.Show("WorkingArea:"+re.ToString()); } privatevoidform1_MouseMove(objectsender,MouseEventArgse) { this.Location=newSystem.Drawing.Point(this.Location.X+e.X-this.tmpx,this.Location.Y+e.Y-this.tmpy); } privatevoidForm1_Load(objectsender,System.EventArgse) { Screen[]s=Screen.AllScreens; ScreensRect=newRectangle[s.Length]; for(inti=0;i<s.Length;i++) { ScreensRect[i]=s[i].WorkingArea; } } } }