矩形 &在 vb.net 中解析 [英] Rectangles & Parsing in vb.net

查看:26
本文介绍了矩形 &在 vb.net 中解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点......一个两部分的问题.第一个比第二个重要得多,它们都在同一个项目中,并且在 vb.net 中.

This is kinda....a two part question. The first one is much more important than the second one, both of these are in the same project, and in vb.net.

如何限制由鼠标控制的矩形对象的边界,使其不能绘制在图片框之外?它是一种标准的套索控件,用户可以单击并拖动,它会从初始单击点到鼠标当前位置绘制一个框.起点是 (rectX,rectY),然后使用 rectDimX 和 rectDimY(设置宽度和高度)将框绘制到右下角,以查看鼠标发生了多少变化.基本上,它是您在 Windows 桌面上单击并拖动得到的结果.这里的问题是矩形可以绘制在它正在绘制的图片框之外,代码的下一部分尝试引用这个位置,然后失败并出现 OutOfMemory 异常.这引出了我的第二个问题:

How can I constrain the bounds of a rectangle object, which is controlled by a mouse, so it cannot be drawn outside a PictureBox? It is kindof a standard lasso control, the user can click and drag and it will draw a box from the initial click point to the mouse's current location. The starting point is at (rectX,rectY), and the box is drawn to the bottom right using rectDimX and rectDimY (to set the width and height) to see how much of a change has occurred with the mouse. Basically, its what you get with a click and drag on a Windows desktop. The issue here is that the rectangle is able to be drawn outside the PictureBox it is being drawn on, and the next part of the code attempts to reference this location, and then fails with an OutOfMemory exception. This leads me to my second question:

我怎样才能让矩形在第四象限以上绘制,这只是正数?如果它转到其他任何地方,它不会显示矩形,尽管它仍然具有正确的值.我知道我可以根据起始位置和鼠标位置对此进行四次编码,但这将是一个巨大的麻烦并且需要重写整个矩形代码.

How can I make the rectangle draw in more than the fourth quadrant, which is only positive numbers? If it goes anywhere else, it does not show the rectangle, though it does still have the correct values. I know i could code this four times based on starting location and mouse location, but that would be a huge hassle and a rewrite of the whole rectangle code.

是否有简单的解决方案?第一个是一个更大的麻烦,因为如果没有简单的方法,它将非常耗时.

Is there an easy solution for either of these? The first one is a much bigger hassle, as it will be very time consuming if there is no easy way.

感谢您的帮助!

推荐答案

对于问题的第一部分,即使用户将鼠标拖动到图片框的边缘之外,您也不必使用这些坐标你的绘画习惯.只需做类似的事情

For the first part of your question, even if the user drags the mouse beyond the edge of your picture box, you don't have to use those coordinates for your drawing routine. Simply do something like

If (DrawingPoint.X > PictureBox.Right)
   DrawingPoint.X = PictureBox.Right // Right-hand limit of picture box
End If

Y 方向类似.

对于绘图时的负数,您想将屏幕坐标转换为客户区坐标.看看 ScreenToClientClientToScreen.

As for the negative numbers while drawing, you want to translate screen coordinates to client area coordinates. Have a look at ScreenToClient and ClientToScreen.

这篇关于矩形 &在 vb.net 中解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆