C# 如何找到列表中最顶端的picbox<;picturebox>;在范围内。包含(例如位置)?

C# 如何找到列表中最顶端的picbox<;picturebox>;在范围内。包含(例如位置)?,c#,winforms,picturebox,C#,Winforms,Picturebox,我在panel1\u MouseMove活动中遇到了一个问题。我在列表pbox中有3个picturebox,它们都被绘制到面板上&picturebox控件被设置为Visible=false 问题是,当存在重叠图像时,当我单击一个图像时,它可能会选择其后面的pictureBox。像这样 代码如下: private void panel1_MouseMove(object sender, MouseEventArgs e) { if (picturebox1.Bounds.Contains(

我在panel1\u MouseMove活动中遇到了一个问题。我在列表pbox中有3个picturebox,它们都被绘制到面板上&picturebox控件被设置为Visible=false

问题是,当存在重叠图像时,当我单击一个图像时,它可能会选择其后面的pictureBox。像这样

代码如下:

    private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (picturebox1.Bounds.Contains(e.Location) && !picturebox1.Visible)
{
picturebox1.Show();
picturebox2.Hide();
picturebox3.Hide();
}
if (picturebox2.Bounds.Contains(e.Location) && !picturebox2.Visible)
{
picturebox1.Hide();
picturebox2.Show();
picturebox3.Hide();
}
if (picturebox3.Bounds.Contains(e.Location) && !picturebox3.Visible)
{
picturebox1.Hide();
picturebox2.Hide();
picturebox3.Show();
}

任何帮助都将不胜感激,是的,我必须为我的特定应用程序使用Bounds.Contains。另一种看待我的问题的方式是,我正在尝试创建一个pictureBox1\uuuuuMouseClick事件,但没有可见控件。

您可以尝试类似的方法。我添加了三个具有三种不同背景颜色的重叠图片框进行测试

初始表单设置:

private bool isDragging = false;
private Point dragOffset = Point.Empty;
private PictureBox dragBox;

public Form1() {
  InitializeComponent();

  panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
  panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
  panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
  panel1.Paint += new PaintEventHandler(panel1_Paint);
  panel1.DragEnter += new DragEventHandler(panel1_DragEnter);
  panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
拖放事件:

void panel1_DragDrop(object sender, DragEventArgs e) {
  dragBox.Location = panel1.PointToClient(
          new Point(e.X - dragOffset.X, e.Y - dragOffset.Y));
  panel1_MouseUp(null, null);
}

void panel1_DragEnter(object sender, DragEventArgs e) {
  e.Effect = DragDropEffects.Move;
}
鼠标事件:

void panel1_MouseMove(object sender, MouseEventArgs e) {
  if (dragBox != null && !isDragging) {
    isDragging = true;
    panel1.DoDragDrop(dragBox, DragDropEffects.Move);
  }
}

void panel1_MouseDown(object sender, MouseEventArgs e) {
  if (e.Button == MouseButtons.Left) {
    foreach (PictureBox pb in panel1.Controls.OfType<PictureBox>()
             .OrderBy(x => panel1.Controls.GetChildIndex(x))) {

      if (pb.Bounds.Contains(e.Location)) {
        pb.BringToFront();
        pb.Visible = true;
        dragBox = pb;
        dragOffset = new Point(e.X - pb.Left, e.Y - pb.Top);
        panel1.Invalidate();
        break;
      }
    }
  }
}

void panel1_MouseUp(object sender, MouseEventArgs e) {
  foreach (PictureBox pb in panel1.Controls.OfType<PictureBox>()) {
    pb.Visible = false;
  }
  dragBox = null;
  isDragging = false;
}
void panel1\u MouseMove(对象发送器,MouseEventArgs e){
if(dragBox!=null&&!isDraging){
IsDraging=true;
面板1.DoDragDrop(dragBox,DragDropEffects.Move);
}
}
无效面板1_MouseDown(对象发送器,MouseEventArgs e){
if(e.Button==MouseButtons.Left){
foreach(panel1.Controls.OfType()中的PictureBox pb)
.OrderBy(x=>panel1.Controls.GetChildIndex(x))){
if(pb.Bounds.Contains(e.Location)){
pb.BringToFront();
可见=真实;
dragBox=pb;
dragOffset=新点(e.X-pb.左,e.Y-pb.顶);
1.使无效();
打破
}
}
}
}
无效面板1_MouseUp(对象发送器,MouseEventArgs e){
foreach(panel1.Controls.OfType()中的PictureBox pb){
可见=假;
}
dragBox=null;
IsDraging=错误;
}
绘画活动:

void panel1_Paint(object sender, PaintEventArgs e) {
  foreach (PictureBox pb in panel1.Controls.OfType<PictureBox>()
           .OrderByDescending(x => panel1.Controls.GetChildIndex(x))) {
    e.Graphics.FillRectangle(new SolidBrush(pb.BackColor), pb.Bounds);
  }
}
void panel1\u Paint(对象发送器,PaintEventArgs e){
foreach(panel1.Controls.OfType()中的PictureBox pb)
.OrderByDescending(x=>panel1.Controls.GetChildIndex(x))){
e、 Graphics.FillRectangle(新的SolidBrush(pb.BackColor)、pb.Bounds);
}
}
它使用父面板的
GetChildIndex
获取面板内最高的PictureBox控件,如果找到它,则使“单击的”控件可见并位于前面,以便更新子索引

由于PictureBox总是隐藏的(除非拖动),因此此代码将把正确的PictureBox带到前面。我添加了我的绘制事件,该事件以相反的顺序绘制PictureBox,以便最后绘制最高的框

更改代码以在鼠标向下显示选定的PictureBox,然后在鼠标上再次隐藏它


添加了拖放代码。

您可以尝试类似的方法。我添加了三个具有三种不同背景颜色的重叠图片框进行测试

初始表单设置:

private bool isDragging = false;
private Point dragOffset = Point.Empty;
private PictureBox dragBox;

public Form1() {
  InitializeComponent();

  panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
  panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
  panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
  panel1.Paint += new PaintEventHandler(panel1_Paint);
  panel1.DragEnter += new DragEventHandler(panel1_DragEnter);
  panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
拖放事件:

void panel1_DragDrop(object sender, DragEventArgs e) {
  dragBox.Location = panel1.PointToClient(
          new Point(e.X - dragOffset.X, e.Y - dragOffset.Y));
  panel1_MouseUp(null, null);
}

void panel1_DragEnter(object sender, DragEventArgs e) {
  e.Effect = DragDropEffects.Move;
}
鼠标事件:

void panel1_MouseMove(object sender, MouseEventArgs e) {
  if (dragBox != null && !isDragging) {
    isDragging = true;
    panel1.DoDragDrop(dragBox, DragDropEffects.Move);
  }
}

void panel1_MouseDown(object sender, MouseEventArgs e) {
  if (e.Button == MouseButtons.Left) {
    foreach (PictureBox pb in panel1.Controls.OfType<PictureBox>()
             .OrderBy(x => panel1.Controls.GetChildIndex(x))) {

      if (pb.Bounds.Contains(e.Location)) {
        pb.BringToFront();
        pb.Visible = true;
        dragBox = pb;
        dragOffset = new Point(e.X - pb.Left, e.Y - pb.Top);
        panel1.Invalidate();
        break;
      }
    }
  }
}

void panel1_MouseUp(object sender, MouseEventArgs e) {
  foreach (PictureBox pb in panel1.Controls.OfType<PictureBox>()) {
    pb.Visible = false;
  }
  dragBox = null;
  isDragging = false;
}
void panel1\u MouseMove(对象发送器,MouseEventArgs e){
if(dragBox!=null&&!isDraging){
IsDraging=true;
面板1.DoDragDrop(dragBox,DragDropEffects.Move);
}
}
无效面板1_MouseDown(对象发送器,MouseEventArgs e){
if(e.Button==MouseButtons.Left){
foreach(panel1.Controls.OfType()中的PictureBox pb)
.OrderBy(x=>panel1.Controls.GetChildIndex(x))){
if(pb.Bounds.Contains(e.Location)){
pb.BringToFront();
可见=真实;
dragBox=pb;
dragOffset=新点(e.X-pb.左,e.Y-pb.顶);
1.使无效();
打破
}
}
}
}
无效面板1_MouseUp(对象发送器,MouseEventArgs e){
foreach(panel1.Controls.OfType()中的PictureBox pb){
可见=假;
}
dragBox=null;
IsDraging=错误;
}
绘画活动:

void panel1_Paint(object sender, PaintEventArgs e) {
  foreach (PictureBox pb in panel1.Controls.OfType<PictureBox>()
           .OrderByDescending(x => panel1.Controls.GetChildIndex(x))) {
    e.Graphics.FillRectangle(new SolidBrush(pb.BackColor), pb.Bounds);
  }
}
void panel1\u Paint(对象发送器,PaintEventArgs e){
foreach(panel1.Controls.OfType()中的PictureBox pb)
.OrderByDescending(x=>panel1.Controls.GetChildIndex(x))){
e、 Graphics.FillRectangle(新的SolidBrush(pb.BackColor)、pb.Bounds);
}
}
它使用父面板的
GetChildIndex
获取面板内最高的PictureBox控件,如果找到它,则使“单击的”控件可见并位于前面,以便更新子索引

由于PictureBox总是隐藏的(除非拖动),因此此代码将把正确的PictureBox带到前面。我添加了我的绘制事件,该事件以相反的顺序绘制PictureBox,以便最后绘制最高的框

更改代码以在鼠标向下显示选定的PictureBox,然后在鼠标上再次隐藏它


添加了拖放代码。

表单设计器中picturebox控件的布局如何…?普通picturebox控件的布局如何,我只是将其设置为“不可见”,使其再次可见的唯一方法是将鼠标放在绘制的picturebox的边界内并单击鼠标左键。为什么不尝试将控件放置在面板上或它自己的单独面板上。我已将picturebox控件放置在其绘制到的面板上,但我无法使其可见。唯一可以使控件可见的时间是在拖动控件时。该小组甚至实际上是我的错误。我想你可以说,我正在尝试创建自己的pictureBox\u鼠标单击事件,而不使用可见的pictureBox。我会把它添加到我原来的帖子中。我希望您能帮助我如何在窗体设计器中放置picturebox控件的布局…?如何放置普通picturebox控件,我只是将其设置为“不可见”,使其再次可见的唯一方法是将鼠标放在绘制的picturebox的边界内并单击鼠标左键。为什么不尝试将控件放置在面板上或它自己的单独面板上。我已将picturebox控件放置在其绘制到的面板上,但我无法使其可见。唯一可以使控件可见的时间是在拖动控件时。该小组甚至实际上是我的错误。我想你可以说,我正在尝试创建自己的pictureBox\u鼠标点击事件,而不使用