请教各位:
winform窗体Form1 中有多个按钮按钮,如何实现和“在资源管理器中用鼠标框选多个项目”的操作?
我有俩点不清楚:
1、怎样实现让鼠标在窗体上面框选时出现一个矩形选择框?
2、有了矩形选择框后,如何才能确定在矩形框之内的按钮控件?
winform窗体Form1 中有多个按钮按钮,如何实现和“在资源管理器中用鼠标框选多个项目”的操作?
我有俩点不清楚:
1、怎样实现让鼠标在窗体上面框选时出现一个矩形选择框?
2、有了矩形选择框后,如何才能确定在矩形框之内的按钮控件?
2.使用Rectangle的IntersectWith方法,逐一判断各控件的Bounds是否与此矩形相交。
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;
using MapDemoControlLibrary;namespace MapDemoTest
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
pen = new Pen(Color.Red); UCPoint but;
for (int i = 0; i < 5; i++)
{
but = new UCPoint() { LabelText = "bt" + i, Image = global::MapDemoTest.Properties.Resource1.point1,UCPointArea = UCPointArea.BaseMapFrom };
this.pictureBox1.Controls.Add(but);
}
}
Pen pen = null;
Point PTStart;
Rectangle GraphicsRectangle;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
PTStart = new Point(e.X, e.Y);
}
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int x = PTStart.X ;
int y = PTStart.Y;
int width = e.X - PTStart.X;
int height = e.Y - PTStart.Y; if (width < 0)
{
x = e.X;
width = Math.Abs(width);
}
if (height < 0)
{
y = e.Y;
height = Math.Abs(height);
}
//绘制矩形的范围
if (e.X > this.pictureBox1.Size.Width - 2 || e.X < 1 || e.Y > this.pictureBox1.Size.Height - 2 || e.Y < 1) return;
GraphicsRectangle = new Rectangle(this.pictureBox1.PointToScreen(new Point(x, y)), new Size(width, height));
this.PictureBoxRefresh();
ControlPaint.DrawReversibleFrame(GraphicsRectangle , Color.Yellow, FrameStyle.Dashed); this.toolStripStatusLabel1.Text = "sX:" + x;
this.toolStripStatusLabel2.Text = " sY:" + y;
this.toolStripStatusLabel3.Text = " width:" + width;
this.toolStripStatusLabel4.Text = " height:" + height;
this.toolStripStatusLabel5.Text = " eX:" + e.X.ToString();
this.toolStripStatusLabel6.Text = " eY:" + e.Y.ToString();
}
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
foreach (UCPoint p in this.pictureBox1.Controls)
{
if (GraphicsRectangle.IntersectsWith(new Rectangle(this.pictureBox1.PointToScreen(p.Location), p.Size)))
if (p.Selected)
p.Selected = false;
else
p.Selected = true;
else
p.Selected = false;
}
GraphicsRectangle = new Rectangle();
this.PictureBoxRefresh();
}
} /// <summary>
/// 重绘控件
/// </summary>
private void PictureBoxRefresh()
{
foreach (Control c in this.pictureBox1.Controls)
{
c.Refresh();
}
this.pictureBox1.Refresh();
}
}
}