编辑:所以我猜对了投票,人们有问题再现问题?这对我来说并不难,复制我包含的代码,阅读原始预期和实际结果,运行程序,点击几下并拖动。我可以每次都重现这个。它并不重要,因为我解决了这个问题,但不得不等待它的回答。因此,如果持有这个保留的管理员可以删除帖子或标记我的答案作为答案,这将是伟大的。我提供了指南所概述的所有内容,以及#34;最小代码"的可能意见。它有所需的和实际的结果,重现的步骤,我不知道如何在不进行屏幕录制的情况下使其更清晰。如果你提出不同的意见,你实际上要求对某些事情作出澄清,以便我能看出错误。
原件:
我这里有一个有趣的问题。我试图在C#中做一个简单的橡皮筋选择,除了一个奇怪的错误,如果我不做出选择,我就一切正常。
预期结果:MouseDown / MouseUp没有移动鼠标,移动鼠标后不应该选择。
实际结果:MouseDown / MouseUp没有移动鼠标,然后当我鼠标移动鼠标时,我移动鼠标时绘制选择。如果单击,选择将消失。然后,如果我再次单击并鼠标移动鼠标,则会不断重新绘制新选择。
我在表单中添加了一个标签来检查bool的状态。当我这样做时,一切都按预期工作。但是,一旦我注释掉label1.Text
行,它就不再按预期工作了。我已经尝试添加多个bool来解决上述问题但无济于事。
那么除非我设置标签文本,为什么bool不被识别?附:我是C#的新手,所以我欢迎任何批评。我从http://csharphelper.com/blog/2014/08/use-a-rubber-band-box-to-let-the-user-select-an-area-in-a-picture-in-c/
中提取了此代码这是我目前的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RubberBand
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Bitmap m_Orig = null;
private int X0, X1, Y0, Y1;
private bool sArea, isDown, hasMoved = false;
private Bitmap sImg = null;
private Graphics sGraph = null;
private void Form1_Load(object sender, EventArgs e)
{
m_Orig = new Bitmap(pictureBox1.Image);
this.KeyPreview = true;
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 27)
{
if (!sArea) return;
sArea = false;
isDown = false;
hasMoved = false;
sImg = null;
sGraph = null;
pictureBox1.Image = m_Orig;
pictureBox1.Refresh();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
sArea = true;
isDown = true;
//label1.Text = isDown.ToString();
X0 = e.X;
Y0 = e.Y;
sImg = new Bitmap(m_Orig);
sGraph = Graphics.FromImage(sImg);
pictureBox1.Image = sImg;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (!sArea) return;
hasMoved = true;
//label1.Text = isDown.ToString();
if (isDown)
{
X1 = e.X;
Y1 = e.Y;
sGraph.DrawImage(m_Orig, 0, 0);
using (Pen select_pen = new Pen(Color.Red))
{
select_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
Rectangle rect = MakeRectangle(X0, Y0, X1, Y1);
sGraph.DrawRectangle(select_pen, rect);
}
pictureBox1.Refresh();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (!sArea) return;
if (!hasMoved) return;
sArea = false;
hasMoved = false;
isDown = false;
//label1.Text = isDown.ToString();
sImg = null;
sGraph = null;
pictureBox1.Image = m_Orig;
pictureBox1.Refresh();
Rectangle rect = MakeRectangle(X0, Y0, X1, Y1);
if ((rect.Width > 0) && (rect.Height > 0))
{
MessageBox.Show(rect.ToString());
}
}
private Rectangle MakeRectangle(int x0, int y0, int x1, int y1)
{
return new Rectangle(
Math.Min(x0, x1),
Math.Min(y0, y1),
Math.Abs(x0 - x1),
Math.Abs(y0 - y1));
}
}
}
答案 0 :(得分:0)
好的,经过更多的研究,这似乎是一个众所周知的问题。由于此页面,我已解决了我的问题:https://social.msdn.microsoft.com/Forums/vstudio/en-US/8e85eb78-77f2-485f-9bd3-3eaa44233a8a/mousedown-and-mousemove-events
以下是更新后的工作代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RubberBand
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Bitmap m_Orig = null;
private int X0, X1, Y0, Y1;
private bool sArea, hasMoved = false;
private Bitmap sImg = null;
private Graphics sGraph = null;
private void Form1_Load(object sender, EventArgs e)
{
m_Orig = new Bitmap(pictureBox1.Image);
this.KeyPreview = true;
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 27)
{
if (!sArea) return;
sArea = false;
hasMoved = false;
sImg = null;
sGraph = null;
pictureBox1.Image = m_Orig;
pictureBox1.Refresh();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
sArea = true;
X0 = e.X;
Y0 = e.Y;
sImg = new Bitmap(m_Orig);
sGraph = Graphics.FromImage(sImg);
pictureBox1.Image = sImg;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (!sArea) return;
hasMoved = true;
X1 = e.X;
Y1 = e.Y;
sGraph.DrawImage(m_Orig, 0, 0);
using (Pen select_pen = new Pen(Color.Red))
{
select_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
Rectangle rect = MakeRectangle(X0, Y0, X1, Y1);
sGraph.DrawRectangle(select_pen, rect);
}
pictureBox1.Refresh();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (!sArea) return;
if (!hasMoved) return;
sArea = false;
hasMoved = false;
sImg = null;
sGraph = null;
pictureBox1.Image = m_Orig;
pictureBox1.Refresh();
Rectangle rect = MakeRectangle(X0, Y0, X1, Y1);
if ((rect.Width > 0) && (rect.Height > 0))
{
MessageBox.Show(rect.ToString());
}
}
private Rectangle MakeRectangle(int x0, int y0, int x1, int y1)
{
return new Rectangle(
Math.Min(x0, x1),
Math.Min(y0, y1),
Math.Abs(x0 - x1),
Math.Abs(y0 - y1));
}
}
}