c#如果bool不工作
本文关键字:工作 bool 如果 | 更新日期: 2023-09-27 18:28:16
编辑:所以我猜,根据投票结果,人们在重现问题时遇到了问题?这对我来说并不难,复制我包含的代码,阅读最初的预期和实际结果,运行程序,点击和拖动几下。我每次都能复制这个。这其实并不重要,因为我解决了这个问题,但必须等待它的答案。因此,如果暂停的管理员可以删除帖子或将我的答案标记为答案,那就太好了。我提供了指导方针所概述的一切,并可能提出"最小代码"的意见。它有了想要的和实际的结果,复制的步骤,我不知道如何在不进行屏幕录制的情况下让它变得更加清晰。如果你求同存异,不如你真的要求澄清一些事情,这样我就能看到错误了。
原件:
我有一个有趣的问题。我正在尝试在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));
}
}
}
好的,经过更多的研究,这似乎是一个已知的问题。多亏了这个页面,我已经解决了我的问题: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));
}
}
}