调用鼠标事件并向消息框提供输入时出现问题!
中实现一系列 3 个鼠标事件和一个绘制事件
我的问题似乎有点长,但我知道解决方案是对我的代码进行轻微修正。我正在表单 1 pictureBox1_MouseDown , pictureBox1_MouseMove, 图片Box1_Paint, pictureBox1_MouseUp。
代码应该这样工作:当我单击鼠标并拖动它并在所需的位置释放按钮时,应该会出现一个接受输入值的自定义消息框。所以我构建了一个显示为消息框的表单(form2)。这是我的表格1的代码,看到代码后你就明白问题所在了。
form1:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// "e.X" and "e.Y" are used to get MousePositionX and MousePositionY
rect = new Rectangle(0, 0, e.X, e.Y);
pictureBox1.Invalidate();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// This makes sure that the left mouse button is pressed.
if (e.Button == MouseButtons.Left)
{
// Draws the rectangle as the mouse moves
rect.Width = e.X;
rect.Height = e.Y;
}
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
private WindowsFormsApplication2.Form2 _form2 = new WindowsFormsApplication2.Form2();
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
DialogResult dialogresult = _form2.ShowDialog(this);
if (dialogresult == DialogResult.OK)
{
try
{
for (int i = 0; i < 2; i++)
{
l[i] = e.X;
m[i] = e.Y;
}
}
catch (Exception ex)
{
}
}
}
这里的问题是:
1)根据代码,当我们拖动鼠标时出现一个矩形,并且在我们离开鼠标按钮的地方出现一个消息框,现在这个消息框(form2)无法读取输入值我不知道如何将 form2 的 OK 和 CANCEl 按钮与 form1 连接。给出输入并按 OK 后,矩形应该消失,但这里没有发生。
2)我只需要输入两次,即我只执行前面的一系列操作(所有鼠标事件和绘画事件)两次,所以我相应地编写了FOR循环。但问题是消息框(form2)每次释放鼠标按钮都会被解雇。这会导致严重的问题,因为我必须在给出两个输入后实现 mousedoubleclick 事件。
请告诉我我必须做出的必要更正。 提前致谢。
My question seems a bit lengthy but I know the solution would be slight corrections to my code.I am implementing a sequence of 3 mouse events and a paint event in my form 1
pictureBox1_MouseDown,
pictureBox1_MouseMove,
pictureBox1_Paint,
pictureBox1_MouseUp.
the code should work this way: when i click the mouse and drag it and release the button at desired point then there should appear a custom message box which takes the input values.so i constructed a form(form2) which appears as message box. here is my code of form 1 ,you can understand the problem after seeing the code.
form1:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// "e.X" and "e.Y" are used to get MousePositionX and MousePositionY
rect = new Rectangle(0, 0, e.X, e.Y);
pictureBox1.Invalidate();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// This makes sure that the left mouse button is pressed.
if (e.Button == MouseButtons.Left)
{
// Draws the rectangle as the mouse moves
rect.Width = e.X;
rect.Height = e.Y;
}
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
private WindowsFormsApplication2.Form2 _form2 = new WindowsFormsApplication2.Form2();
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
DialogResult dialogresult = _form2.ShowDialog(this);
if (dialogresult == DialogResult.OK)
{
try
{
for (int i = 0; i < 2; i++)
{
l[i] = e.X;
m[i] = e.Y;
}
}
catch (Exception ex)
{
}
}
}
the problems here are:
1)According to the code when we drag the mouse a rectangle appears and the place where we leave the mouse button a message box appears,now this message box(form2) is unable to read the input values as i dont know how to connect the OK and CANCEl buttons of form2 with the form1.After giving inputs and pressing OK the rectangle should disappear,but here it is not happening.
2)I only need to give the input only two times,i.e i perform the previous sequence of actions(all the mouse events and paint event) only twice ,so i wrote FOR loop accordingly.But the problem is the message box(form2) get fired every time, i release the mouse button.This is causing a serious problem as i have to implement a mousedoubleclick event after giving the two inputs.
Any please give say me necessary corrections i have to make.
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我不确定“将 form2 的 OK 和 CANCEL 按钮与 form1 连接”是什么意思?
您不必“连接”任何东西。您只需要 Form2 上的两个按钮(“确定”和“取消”),并且在每个 _click 方法中,您需要
this.DialogResult = DialogResult.OK;
表示“确定”,this.DialogResult = DialogResult.Cancel;< /code> 取消。
然后继续执行表格 1 中的“if”。
这将删除矩形。我不知道这是否是一个好的解决方案,但它确实有效。
方法内的循环无法按您想要的方式工作:每次调用
pictureBox1_MouseUp
并到达 for 循环时,“i”将被设置为“0”,然后循环开始。您需要在 Form1 的构造函数中使用
int TwoClicks = 0
。并且您必须相应地更改pictureBox1_MouseUp
:请随意提出任何问题(如果我没有回答正确,请描述您的第一个问题)。
I am not sure what you mean with "connect the OK and CANCEL buttons of form2 with form1"?
You don't have to "connect" anything. All you need is two buttons on Form2 (OK and Cancel) and in each _click method you need
this.DialogResult = DialogResult.OK;
for OK andthis.DialogResult = DialogResult.Cancel;
for Cancel.Then you continue with your "if" in Form 1.
That will remove the Rectangle. I don't know if it's a good solution to do it, but it works.
Your Loop inside the Method can't work the way you want: every time you call
pictureBox1_MouseUp
and reach the for-loop 'i' will be set to '0' and then the loop starts.You need a
int TwoClicks = 0
in constructor of Form1. And you have to change thepictureBox1_MouseUp
accordingly:Feel free to ask any questions (and describe your first Problem, if I didn't get it right).