C# Winform 窗体用鼠标拖出虚线框....并且虚线框区域里的所有控件选中

wxm3630478 2009-12-03 02:23:50
网上找来点代码.....能够拉出虚线框了........但是不知道怎么选择控件...

Form窗体上...

this.GetChildAtPoint(e.Location); 用这个方法 但是不知道放什么地方(放那个事件里触发好);

或者谁有其他的方法........思路说说.......自己琢磨了变天搞出不来哈


代码在下面...

public partial class Form1 : Form
{
bool MouseIsDown = false;
Rectangle MouseRect = Rectangle.Empty;
public Form1()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(Form1_MouseDown);
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
this.MouseUp += new MouseEventHandler(Form1_MouseUp);
}

void Form1_MouseDown(object sender, MouseEventArgs e)
{
MouseIsDown = true;
DrawStart(e.Location);
}

void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (MouseIsDown)
{
ResizeToRectangle(e.Location);
}
//这里可以实现选中啊,但是必须要鼠标在子控件上活动过(鼠标路过子控件)才能选中
//Control ctl = this.GetChildAtPoint(e.Location);
//if (ctl != null)
//{
// ctl.BackColor = Color.Blue;
//}
}

void Form1_MouseUp(object sender, MouseEventArgs e)
{
this.Capture = false;
Cursor.Clip = Rectangle.Empty;
MouseIsDown = false;
DrawRectangle();
MouseRect = Rectangle.Empty;
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void ResizeToRectangle(Point p)
{
DrawRectangle();
MouseRect.Width = p.X - MouseRect.Left;
MouseRect.Height = p.Y - MouseRect.Top;
DrawRectangle();
}

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);
}

private void DrawRectangle()
{
Rectangle rect = this.RectangleToScreen(MouseRect);
ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);
}

private void Form1_ControlAdded(object sender, ControlEventArgs e)
{
}
}
...全文
1208 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
皮蛋 2009-12-04
  • 打赏
  • 举报
回复
用 zcl24 的方法可以啊。只要在MouseUp是修正下 MouseRect 的值就可以了
void frmMain_MouseUp(object sender, MouseEventArgs e)
{
this.Capture = false;
Cursor.Clip = Rectangle.Empty;
MouseIsDown = false;
DrawRectangle();
MouseRect = rectCorrection(MouseRect);
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;
}
//修正MouseRect 的值
Rectangle rectCorrection(Rectangle oldRect)
{
if (oldRect.Width < 0)
{
oldRect.X += oldRect.Width;
oldRect.Width *= -1;
}
if (oldRect.Height < 0)
{
oldRect.Y += oldRect.Height;
oldRect.Height *= -1;
}
return new Rectangle(oldRect.X, oldRect.Y, oldRect.Width, oldRect.Height);
}
wxm3630478 2009-12-04
  • 打赏
  • 举报
回复
谢谢 各位了.......

大家都是写在MouseUp事件里的.......

谁能弄个MouseMove 事件里写个了

我觉得还是要用这个方法: this.GetChildAtPoint(坐标);

现在就是不知道 虚线和子控件 交叉的一瞬间 怎么去触发一个事件,得到一个(坐标)......我相信应该是可以实现的
Sugar_Tiger 2009-12-03
  • 打赏
  • 举报
回复
小改一下,呵呵

foreach(Control ctl in this.Controls){
Graphics g = Graphics.FromHwnd(this.Handle);
//在此判断控件的位置,如果在选中范围内,则给其自绘选中效果
if(控件在选中矩形范围内)
{
g.DrawRectangle(new Pen(Brushes.Red, 1),
new Rectangle(ctl.Location.X - 1, ctl.Location.Y - 1,
ctl.Size.Width + 1, ctl.Size.Height + 1));
g.Dispose();
}
}

Sugar_Tiger 2009-12-03
  • 打赏
  • 举报
回复
1)把最后画的矩形保存下来,rect,
2)在MouseUp 事件里,建立画笔,
判断控件的位置是否在rect范围之内.如果在的话给其自绘选中效果,我这做了,
可以实现,但是刷新时有点小问题,没空调了,你自己试下吧

Graphics g = Graphics.FromHwnd(this.Handle);
//在此判断控件的位置,如果在选中范围内,刚给其自绘选中效果
{
g.DrawRectangle(new Pen(Brushes.Red, 1),
new Rectangle(ctl.Location.X - 1, ctl.Location.Y - 1,
ctl.Size.Width + 1, ctl.Size.Height + 1));
g.Dispose();
}
EsMo 2009-12-03
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 wxm3630478 的回复:]
Rectangle.Intersect()  还是不行了, 不知道在哪个事件里触发好......不用到循环最好了
[/Quote]

最后获取选中的控件 当然是在mouseup里了
用rectangle的Contain(Point)啊
point就是控件的location
wxm3630478 2009-12-03
  • 打赏
  • 举报
回复
晕了,帖子都快沉的米见了...........难到就没有人做过这个东西吗.....
wxm3630478 2009-12-03
  • 打赏
  • 举报
回复
Rectangle.Intersect() 还是不行了, 不知道在哪个事件里触发好......不用到循环最好了
zcl24 2009-12-03
  • 打赏
  • 举报
回复
其实就 这些代码有用,其它都是测试用的 代码太长了 提交不了 所以才精简了
MouseRect.Contains(ct.Location)这个算法是可以扩展的

          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)
{
EsMo 2009-12-03
  • 打赏
  • 举报
回复
貌似选中以后 只能在内存中知道 在界面上是表现不出来的吧 除非手动设置边框选项或自己改皮肤等.....
jin20000 2009-12-03
  • 打赏
  • 举报
回复
鼠标拖动事件应该也可以,这很明显必须,鼠标按下,鼠标移动,鼠标弹起三个事件一起用才是
biny101 2009-12-03
  • 打赏
  • 举报
回复
测试了一下楼上的 用VS2008 提示两个问题
1、this.AutoScaleMode = AutoScaleMode.Font;提示不能手动更改
2、using System.Linq; 这个08里面的吧
wxm3630478 2009-12-03
  • 打赏
  • 举报
回复
谢谢 上面的兄弟 试代码呀,这个是松开鼠标 选中控件......效果好像还行,不知道控件多了咋样

但是你的代码还是有点问题的:只能从窗体的左上向右下拉 才可以,其他的方向拉就选不中了,应该是那个判断的问题吧......呵呵
biny101 2009-12-03
  • 打赏
  • 举报
回复
UP
zcl24 2009-12-03
  • 打赏
  • 举报
回复
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 TestApp
{
public partial class Form2 : Form
{
bool MouseIsDown = false;
Rectangle MouseRect = Rectangle.Empty;
public Form2()
{
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);
}
}
}
zcl24 2009-12-03
  • 打赏
  • 举报
回复
using System.Windows.Forms;
using System.Drawing;
namespace TestApp
{
partial class Form2
{

private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.cb1 = new CheckBox();
this.cb3 = new CheckBox();
this.cb5 = new CheckBox();
this.cb6 = new CheckBox();
this.cb7 = new CheckBox();
this.cb8 = new CheckBox();
this.cb9 = new CheckBox();
this.cb10 = new CheckBox();
this.cb2 = new CheckBox();
this.cb4 = new CheckBox();
this.cb11 = new CheckBox();
this.cb12 = new CheckBox();
this.cb13 = new CheckBox();
this.cb14 = new CheckBox();
this.cb15 = new CheckBox();
this.cb16 = new CheckBox();
this.cb17 = new CheckBox();
this.cb18 = new CheckBox();

this.SuspendLayout();
//
// cb1
//
this.cb1.AutoSize = true;
this.cb1.BackgroundImageLayout = ImageLayout.None;
this.cb1.Location = new Point(29, 51);
this.cb1.Name = "cb1";
this.cb1.Size = new Size(80, 17);
this.cb1.TabIndex = 0;
this.cb1.Text = "cb1";
this.cb1.UseVisualStyleBackColor = true;
//
// cb3
//
this.cb3.AutoSize = true;
this.cb3.Location = new Point(29, 75);
this.cb3.Name = "cb3";
this.cb3.Size = new Size(80, 17);
this.cb3.TabIndex = 2;
this.cb3.Text = "cb3";
this.cb3.UseVisualStyleBackColor = true;
//
// cb5
//
this.cb5.AutoSize = true;
this.cb5.Location = new Point(29, 98);
this.cb5.Name = "cb5";
this.cb5.Size = new Size(80, 17);
this.cb5.TabIndex = 4;
this.cb5.Text = "cb5";
this.cb5.UseVisualStyleBackColor = true;
//
// cb6
//
this.cb6.AutoSize = true;
this.cb6.Location = new Point(29, 121);
this.cb6.Name = "cb6";
this.cb6.Size = new Size(80, 17);
this.cb6.TabIndex = 5;
this.cb6.Text = "cb6";
this.cb6.UseVisualStyleBackColor = true;
//
// cb7
//
this.cb7.AutoSize = true;
this.cb7.Location = new Point(29, 212);
this.cb7.Name = "cb7";
this.cb7.Size = new Size(80, 17);
this.cb7.TabIndex = 9;
this.cb7.Text = "cb7";

//
// cb8
//
this.cb8.AutoSize = true;
this.cb8.Location = new Point(29, 189);
this.cb8.Name = "cb8";

this.cb8.TabIndex = 8;
this.cb8.Text = "cb8";
this.cb8.UseVisualStyleBackColor = true;
//
// cb9
//
this.cb9.AutoSize = true;
this.cb9.Location = new Point(29, 166);
this.cb9.Name = "cb9";
this.cb9.Size = new Size(80, 17);
this.cb9.TabIndex = 7;
this.cb9.Text = "cb9";
this.cb9.UseVisualStyleBackColor = true;
//
// cb10
//
this.cb10.AutoSize = true;
this.cb10.Location = new Point(29, 142);
this.cb10.Name = "cb10";
this.cb10.Size = new Size(86, 17);
this.cb10.TabIndex = 6;
this.cb10.Text = "cb10";
this.cb10.UseVisualStyleBackColor = true;
//
// cb2
//
this.cb2.AutoSize = true;
this.cb2.Location = new Point(115, 212);
this.cb2.Name = "cb2";
this.cb2.Size = new Size(80, 17);
this.cb2.TabIndex = 17;
this.cb2.Text = "cb2";
this.cb2.UseVisualStyleBackColor = true;
//
// cb4
//
this.cb4.AutoSize = true;
this.cb4.Location = new Point(115, 189);
this.cb4.Name = "cb4";
this.cb4.Size = new Size(80, 17);
this.cb4.TabIndex = 16;
this.cb4.Text = "cb4";
this.cb4.UseVisualStyleBackColor = true;
//
// cb11
//
this.cb11.AutoSize = true;
this.cb11.Location = new Point(115, 166);
this.cb11.Name = "cb11";
this.cb11.Size = new Size(86, 17);
this.cb11.TabIndex = 15;
this.cb11.Text = "cb11";
this.cb11.UseVisualStyleBackColor = true;
//
// cb12
//
this.cb12.AutoSize = true;
this.cb12.Location = new Point(115, 142);
this.cb12.Name = "cb12";
this.cb12.Size = new Size(86, 17);
this.cb12.TabIndex = 14;
this.cb12.Text = "cb12";
this.cb12.UseVisualStyleBackColor = true;
//
// cb13
//
this.cb13.AutoSize = true;
this.cb13.Location = new Point(115, 121);
this.cb13.Name = "cb13";
this.cb13.Size = new Size(86, 17);
this.cb13.TabIndex = 13;
this.cb13.Text = "cb13";
this.cb13.UseVisualStyleBackColor = true;
//
// cb14
//
this.cb14.AutoSize = true;
this.cb14.Location = new Point(115, 98);
this.cb14.Name = "cb14";
this.cb14.Size = new Size(86, 17);
this.cb14.TabIndex = 12;
this.cb14.Text = "cb14";
this.cb14.UseVisualStyleBackColor = true;
//
// cb15
//
this.cb15.AutoSize = true;
this.cb15.Location = new Point(115, 75);
this.cb15.Name = "cb15";
this.cb15.Size = new Size(86, 17);
this.cb15.TabIndex = 11;
this.cb15.Text = "cb15";
this.cb15.UseVisualStyleBackColor = true;
//
// cb16
//
this.cb16.AutoSize = true;
this.cb16.Location = new Point(115, 51);
this.cb16.Name = "cb16";
this.cb16.Size = new Size(86, 17);
this.cb16.TabIndex = 10;
this.cb16.Text = "cb16";
this.cb16.UseVisualStyleBackColor = true;
//
// cb17
//
this.cb17.AutoSize = true;
this.cb17.Location = new Point(220, 212);
this.cb17.Name = "cb17";
this.cb17.Size = new Size(86, 17);
this.cb17.TabIndex = 25;
this.cb17.Text = "cb17";
this.cb17.UseVisualStyleBackColor = true;

//
// Form2
//
this.AllowDrop = true;
this.AutoScaleDimensions = new SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(403, 297);
this.Controls.Add(this.cb17);
this.Controls.Add(this.cb2);
this.Controls.Add(this.cb4);
this.Controls.Add(this.cb11);
this.Controls.Add(this.cb12);
this.Controls.Add(this.cb13);
this.Controls.Add(this.cb14);
this.Controls.Add(this.cb15);
this.Controls.Add(this.cb16);
this.Controls.Add(this.cb7);
this.Controls.Add(this.cb8);
this.Controls.Add(this.cb9);
this.Controls.Add(this.cb10);
this.Controls.Add(this.cb6);
this.Controls.Add(this.cb5);
this.Controls.Add(this.cb3);
this.Controls.Add(this.cb1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private CheckBox cb1;
private CheckBox cb3;
private CheckBox cb5;
private CheckBox cb6;
private CheckBox cb7;
private CheckBox cb8;
private CheckBox cb9;
private CheckBox cb10;
private CheckBox cb2;
private CheckBox cb4;
private CheckBox cb11;
private CheckBox cb12;
private CheckBox cb13;
private CheckBox cb14;
private CheckBox cb15;
private CheckBox cb16;
private CheckBox cb17;
private CheckBox cb18;
}
}
wxm3630478 2009-12-03
  • 打赏
  • 举报
回复
额,让我想想.....

顺便顶一把.........别沉了
Red_angelX 2009-12-03
  • 打赏
  • 举报
回复
子控件的Rectangle可以事先遍历得到
wxm3630478 2009-12-03
  • 打赏
  • 举报
回复
Rectangle.Intersect 咋满足啊......???

Rectangle.Intersect好像要得到两个矩形框......子控件的Rectangle咋得到呢??
wxm3630478 2009-12-03
  • 打赏
  • 举报
回复
写在鼠标弹起事件里,感觉不好.......

谁有米有好的点子啊.......
EsMo 2009-12-03
  • 打赏
  • 举报
回复
mousedown mousemove mouseup都要用
加载更多回复(8)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧