using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace NewMyFormDesigner { public partial class HostFrame : Form { bool MouseIsDown = false; Rectangle MouseRect = Rectangle.Empty; public HostFrame() { InitializeComponent(); this.MouseDown += new MouseEventHandler(frmMain_MouseDown); this.MouseMove += new MouseEventHandler(frmMain_MouseMove); this.MouseUp += new MouseEventHandler(frmMain_MouseUp); } void frmMain_MouseUp(object sender, MouseEventArgs e) { this.Capture = false; Cursor.Clip = Rectangle.Empty; MouseIsDown = false; DrawRectangle(); foreach (Control ct in this.Controls) { // Point ctrlCenter = new Point(ct.Location.X + ct.Width / 2, ct.Location.Y + ct.Height / 2); //if (ctrlCenter.X > MouseRect.Location.X // && ctrlCenter.X < MouseRect.Location.X + MouseRect.Width // && ctrlCenter.Y > MouseRect.Location.Y && ctrlCenter.Y > MouseRect.Location.Y + MouseRect.Height) if (MouseRect.Contains(ct.Location)) { if (ct is CheckBox) { if (((CheckBox)ct).Checked) //值选中 { ((CheckBox)ct).Checked = false; } else { ((CheckBox)ct).Checked = true; } //控件选中 } } } MouseRect = Rectangle.Empty; } void frmMain_MouseMove(object sender, MouseEventArgs e) { if (MouseIsDown) ResizeToRectangle(e.Location); } void frmMain_MouseDown(object sender, MouseEventArgs e) { MouseIsDown = true; DrawStart(e.Location); } private void ResizeToRectangle(Point p) { DrawRectangle(); MouseRect.Width = p.X - MouseRect.Left; MouseRect.Height = p.Y - MouseRect.Top; DrawRectangle(); } private void DrawRectangle() { Rectangle rect = this.RectangleToScreen(MouseRect); ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed); } private void DrawStart(Point StartPoint) { this.Capture = true; Cursor.Clip = this.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height)); MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0); } } }