如何在鼠标悬停时获得水平线和垂直线?
NET 编程。我有一张地图的图像,其边界上有纬度和经度。现在,当我在地图上拖动鼠标时,我应该得到一个矩形,其中一个顶点位于鼠标点处,另一个顶点位于地图的左上角,这样我就可以精确地定位具有纬度和经度的像素。要清楚的是,当我们在屏幕上拖动鼠标时,它应该显示为矩形。我有一个部分工作的代码,
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// "e.X" and "e.Y" are used to get MousePositionX and MousePositionY
rect = new Rectangle(e.X, e.Y, 0, 0);
this.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 = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
}
this.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
我不知道问题到底出在哪里但可以画一个矩形在运行时。请任何人帮助我更改代码。
NET programming.I have a image of a map with latitudes and longitudes on its borders .now when i drag the mouse on the map i should get a rectangle with one vertex at the mouse point and opposite one at the top left corner of map ,so that i can exactly pin pointedly locate the pixel with latitude and longitude.to be clear it should appear as a rectangle when we drag mouse on the screen.I have a code which is partially working
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// "e.X" and "e.Y" are used to get MousePositionX and MousePositionY
rect = new Rectangle(e.X, e.Y, 0, 0);
this.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 = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
}
this.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
I don't know where the problem exactly is but could draw a rectangle at run time.any one please help me with changes in the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
tafa的答案有效。
我只添加了两个内容:
在 MouseDown 方法中,您应该
还可以使用以下代码。所以
一旦你点击地图上的某个地方
即使没有,你也会得到一个矩形
移动鼠标。
在 MouseMove 方法中,我将使用以下代码,以便在移动鼠标时不创建新的矩形:
tafa's Answer works.
I only got two additions:
In the MouseDown Method you should
also use the following code. So
once you click somewhere in the map
you get a rectangle even when not
moving the mouse.
In the MouseMove method I would use the following code, to not create new Rectangles whenever you move the mouse:
需要两个简单的改变;
而不是
this.Invalidate()
调用pictureBox1.Invalidate()
。在 mouseMove 方法中生成矩形;
因为您希望左上角 (0,0) 是矩形的另一个角。
Two simple changes required;
Instead of
this.Invalidate()
callpictureBox1.Invalidate()
.In the mouseMove method generate rectangle as;
Since you want the top left corner (0,0) be the other corner of the rectangle.
我建议创建您自己的画布控件([编辑:继承自 PictureBox]),并启用双缓冲。这将减少绘图时的闪烁。
请注意,
DrawRectangle
不接受负宽度或高度,因此您必须在OnPaint
方法中处理此问题。将此控件添加到项目后,编译该项目。您的自定义控件应位于 Visual Studio 中“工具箱”窗口的顶部。如果不是,您可以右键单击工具箱并选择
Choose Items...
将画布添加到工具箱,或者简单地将Panel
控件添加到您的工具箱中。Form
,打开Form.Designer.cs
文件,并将所有Panel
引用替换为DrawingCanvas
(或其他任何内容)您选择的名称)。设置背景图像时,请使用
BackgroundImage
属性。I would suggest creating your own canvas control ([Edit: which inherits from PictureBox]), with double buffering enabled. This will reduce flicker while drawing.
Note that
DrawRectangle
doesn't accept negative width or height, so you must take care of this in theOnPaint
method.Once you've added this control to your project, compile the project. Your custom control should be available on top of the Toolbox window in Visual Studio. If it isn't, you can either right click the Toolbox and select
Choose Items...
to add your canvas to the toolbox, or simply add aPanel
control to yourForm
, open theForm.Designer.cs
file, and replace allPanel
references with yourDrawingCanvas
(or whatever name you choose).When setting the background image, use the
BackgroundImage
property.