[英]Having an issue with mouseClick event in WinForms PictureBox
So, I'm currently trying to develop a small PaintBrush-like application as part of a college assigment, it's supposed to be made in a Windows Form and using C# as a language. 因此,我目前正在尝试开发一个类似于PaintBrush的小型应用程序,作为大学课程的一部分,该应用程序应该在Windows Form中制作,并使用C#作为语言。 So far I managed to code one of my teacher's algorithms to draw a line. 到目前为止,我设法编码了我老师的一种算法来画一条线。 It's supposed to work in the following manner, once I click the lineButton it should give me a prompt to click on two points in the PictureBox and once both have been selected I should click on the button again to plot the line, not very elegant or user friendly I know, but this is my first experience with UI design and handling events so I figured I'd keep it as simple as I can in order to give more focus to the pixel coloring algorithms themselves. 它应该按以下方式工作,一旦我单击lineButton,它应该会提示我单击PictureBox中的两个点,并且一旦选择了两个点,我都应再次单击该按钮以绘制线条,不是很优雅或我知道用户友好,但这是我第一次进行UI设计和处理事件,因此我认为我会尽可能简化,以便将更多注意力放在像素着色算法本身上。 It works as intended to draw one line (with a few minor glitches) but when I click it a second time it won't select two points in the PictureBox again. 它的工作原理是画一条线(有一些小故障),但是当我第二次单击它时,它将不会再次在PictureBox中选择两个点。 Here is my code 这是我的代码
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 CGExercicios
{
public partial class MyPaint : Form
{
Bitmap drawArea;
Color cor;
private int drawX;
private int drawY;
private Point lineStart;
private Point lineEnd;
private bool mousePressed;
private bool mouseInDrawArea;
private bool pencilTool;
private bool lineTool;
private bool pointsSelected;
private int numOfClicks; //numero de vezes que a imagem é clicada
public MyPaint()
{
InitializeComponent();
drawArea = new Bitmap(imagem.Size.Width, imagem.Size.Height);
pencilTool = false;
lineTool = false;
cor = Color.Black; //cor padrao será a cor preta
numOfClicks = 0;
pointsSelected = false;
}
private void sobreToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void selectColor_Click(object sender, EventArgs e)
{
paletaCores.ShowDialog();
cor = paletaCores.Color;
}
private void imagem_MouseMove(object sender, MouseEventArgs e)
{
if (mouseInDrawArea) {
posX.Text = "X: " + e.X;
posY.Text = "Y: " + e.Y;
/* Implementação da ferramenta do lápis */
if (pencilTool)
{
if (mousePressed)
{
drawX = e.X;
drawY = e.Y;
try
{
drawArea.SetPixel(drawX, drawY, cor);
imagem.Image = drawArea;
}
catch (System.ArgumentOutOfRangeException ecpt)
{}
}
}
}
}
private void imagem_MouseDown(object sender, MouseEventArgs e)
{
mousePressed = true;
}
private void imagem_MouseUp(object sender, MouseEventArgs e)
{
mousePressed = false;
}
private void pencilButton_Click(object sender, EventArgs e)
{
if (!pencilTool)
pencilTool = true;
else
pencilTool = false;
}
private void imagem_MouseLeave(object sender, EventArgs e)
{
mouseInDrawArea = false;
posX.Text = "X:-";
posY.Text = "Y:-";
}
private void imagem_MouseEnter(object sender, EventArgs e)
{
mouseInDrawArea = true;
}
private void lineButton_Click(object sender, EventArgs e)
{
/* Implementação do desenho das retas */
if (!lineTool)
{
lineTool = true;
numOfClicks = 0;
if(lineTool)
MessageBox.Show("Clique no ponto inicial e depois no ponto final e pressione o botao novamente");
}
else
{
if (pointsSelected)
{
drawLine();
}
else
MessageBox.Show("Pontos não selecionados");
}
}
private void drawLine()
{
int dx = lineEnd.X - lineStart.Y;
int dy = lineEnd.Y - lineStart.Y;
int steps;
double x, y; /* Armazenamento real */
int ix, iy; /* Armazenamento inteiro para plotagem */
double xincr, yincr;
x = (double)lineStart.X;
y = (double)lineStart.Y;
drawArea.SetPixel(lineStart.X, lineStart.Y, cor);
steps = Math.Max(Math.Abs(dx), Math.Abs(dy));
xincr = (double)dx / steps;
yincr = (double)dy / steps;
for (int i = 0; i < steps; i++)
{
x += xincr;
ix = (int)Math.Round(x);
y += yincr;
iy = (int)Math.Round(y);
drawArea.SetPixel(ix, iy, cor);
}
imagem.Image = drawArea;
resetLineDraw();
}
private void resetLineDraw()
{
DebugLabel.Text = "";
pointsSelected = false;
lineTool = false;
numOfClicks = 0;
imagem.MouseClick -= imagem_MouseClick;
}
private void imagem_MouseClick(object sender, MouseEventArgs e)
{
/* Captura os pontos inicias e finais se a ferramenta de desenho
* de retas esta habilitada */
if (lineTool && !pointsSelected)
{
if (numOfClicks == 1) imagem.MouseClick -= imagem_MouseClick;
Point tmp = new Point(e.X, e.Y);
if (numOfClicks == 0)
{//Selecionou o primeiro ponto
lineStart = tmp;
DebugLabel.Text = "Primeiro ponto selecionado";
}
else
{//Selecionou o segundo ponto
lineEnd = tmp;
pointsSelected=true;
DebugLabel.Text = "Segundo ponto selecionado";
}
numOfClicks++;
}
}
}
}
"imagem" is the PictureBox in question and the As you can see, I've tried resetting the variables utilized in it's mouseClick handler but the problem still persists. 如您所见,“ imagem”是有问题的PictureBox,并且我尝试过重置其mouseClick处理程序中使用的变量,但问题仍然存在。 Is there a way to perhaps to "reset" the PictureBox so I can choose two points in it again? 是否有办法“重置” PictureBox,以便我可以再次选择其中的两个点? If not, then what am I doing wrong to cause the issue? 如果不是,那么我在做错什么导致问题呢?
As far as i can see you are clearing the event imagem.MouseClick and never set it back again. 据我所知,您正在清除事件imagem.MouseClick,再也不会将其重新设置。
You have to reset the event when you click the button eg 单击按钮时必须重置事件,例如
imagem.MouseClick += imagem_MouseClick
For some reason you have decided to remove the MouseClick handler for the imagem
picturebox. 由于某些原因,您决定删除imagem
处理程序。 See below please, 请看下面
private void resetLineDraw()
{
DebugLabel.Text = "";
pointsSelected = false;
lineTool = false;
numOfClicks = 0;
// this removes the click handler from the picture box.
// so, if you click on it anymore, nothing will happen.
//--> imagem.MouseClick -= imagem_MouseClick;
}
private void imagem_MouseClick(object sender, MouseEventArgs e)
{
/* Captura os pontos inicias e finais se a ferramenta de desenho
* de retas esta habilitada */
if (lineTool && !pointsSelected)
{
// same here
// --> if (numOfClicks == 1) imagem.MouseClick -= imagem_MouseClick;
Point tmp = new Point(e.X, e.Y);
if (numOfClicks == 0)
{//Selecionou o primeiro ponto
lineStart = tmp;
DebugLabel.Text = "Primeiro ponto selecionado";
}
else
{//Selecionou o segundo ponto
lineEnd = tmp;
pointsSelected=true;
DebugLabel.Text = "Segundo ponto selecionado";
}
numOfClicks++;
}
}
remove those lines I commented and it should probably work.. at least that seems to be the most obvious problem. 删除我评论的那些行,它应该可以工作..至少这似乎是最明显的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.