如何在鼠标悬停时获得水平线和垂直线?

发布于 2024-11-15 18:38:49 字数 1037 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂归处 2024-11-22 18:38:50

tafa的答案有效。

我只添加了两个内容:

  1. 在 MouseDown 方法中,您应该
    还可以使用以下代码。所以
    一旦你点击地图上的某个地方
    即使没有,你也会得到一个矩形
    移动鼠标。

    矩形 = new 矩形(0, 0, eX, eY);
    
  2. 在 MouseMove 方法中,我将使用以下代码,以便在移动鼠标时不创建新的矩形:

    rect.Width = eX;  
    矩形.高度 = eY;
    

tafa's Answer works.

I only got two additions:

  1. 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.

    rect = new Rectangle(0, 0, e.X, e.Y);
    
  2. In the MouseMove method I would use the following code, to not create new Rectangles whenever you move the mouse:

    rect.Width = e.X;  
    rect.Height = e.Y;
    
太傻旳人生 2024-11-22 18:38:50

需要两个简单的改变;

而不是 this.Invalidate() 调用 pictureBox1.Invalidate()

在 mouseMove 方法中生成矩形;

rect = new Rectangle(0, 0, e.X, e.Y);

因为您希望左上角 (0,0) 是矩形的另一个角。

Two simple changes required;

Instead of this.Invalidate() call pictureBox1.Invalidate().

In the mouseMove method generate rectangle as;

rect = new Rectangle(0, 0, e.X, e.Y);

Since you want the top left corner (0,0) be the other corner of the rectangle.

苍白女子 2024-11-22 18:38:50

我建议创建您自己的画布控件([编辑:继承自 PictureBox]),并启用双缓冲。这将减少绘图时的闪烁。

public partial class DrawingCanvas : PictureBox
{
    public DrawingCanvas()
    {
        InitializeComponent();
        SetStyle(
              ControlStyles.AllPaintingInWmPaint | 
              ControlStyles.OptimizedDoubleBuffer |
              ControlStyles.ResizeRedraw, true);
    }

    private Point start = new Point(0, 0);
    private Point end = new Point(0, 0);

    protected override void OnMouseDown(MouseEventArgs e)
    {
        start = e.Location;
        end = e.Location;
        Invalidate();
        base.OnMouseDown(e);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        // This makes sure that the left mouse button is pressed.
        if (e.Button == MouseButtons.Left)
            end = e.Location;

        Invalidate();
        base.OnMouseMove(e);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        int top = start.Y < end.Y ? start.Y : end.Y;
        int left = start.X < end.X ? start.X : end.X;
        int width = end.X - start.X; if (width < 0) width = -width;
        int height = end.Y - start.Y; if (height < 0) height = -height;
        Rectangle rect = new Rectangle(left, top, width, height);

        using (Pen pen = new Pen(Color.Red, 2))
        {
            e.Graphics.DrawRectangle(pen, rect);
        }
    }
}

请注意,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.

public partial class DrawingCanvas : PictureBox
{
    public DrawingCanvas()
    {
        InitializeComponent();
        SetStyle(
              ControlStyles.AllPaintingInWmPaint | 
              ControlStyles.OptimizedDoubleBuffer |
              ControlStyles.ResizeRedraw, true);
    }

    private Point start = new Point(0, 0);
    private Point end = new Point(0, 0);

    protected override void OnMouseDown(MouseEventArgs e)
    {
        start = e.Location;
        end = e.Location;
        Invalidate();
        base.OnMouseDown(e);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        // This makes sure that the left mouse button is pressed.
        if (e.Button == MouseButtons.Left)
            end = e.Location;

        Invalidate();
        base.OnMouseMove(e);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        int top = start.Y < end.Y ? start.Y : end.Y;
        int left = start.X < end.X ? start.X : end.X;
        int width = end.X - start.X; if (width < 0) width = -width;
        int height = end.Y - start.Y; if (height < 0) height = -height;
        Rectangle rect = new Rectangle(left, top, width, height);

        using (Pen pen = new Pen(Color.Red, 2))
        {
            e.Graphics.DrawRectangle(pen, rect);
        }
    }
}

Note that DrawRectangle doesn't accept negative width or height, so you must take care of this in the OnPaint 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 a Panel control to your Form, open the Form.Designer.cs file, and replace all Panel references with your DrawingCanvas (or whatever name you choose).

When setting the background image, use the BackgroundImage property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文