Rectangles & Parsing in 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.
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.
Thanks for the help!

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
And similar for the Y direction.
As for the negative numbers while drawing, you want to translate screen coordinates to client area coordinates. Have a look at ScreenToClient and ClientToScreen.

Related

ContextMenuStrip Location Point

How to open the ContextMenuStrip programmatically at the cursor on right mouse button down/clicked over PictureBox?
I need to open ContextMenuStrip only in some areas of an Image (Schedule) in a PictureBox, so I disconnected the PictureBox.ContextMenuStrip property from the ContextMenuStrip1 and I'm firing it manually. That means I need to provide a point, which appears to be surprisingly challanging.
My question is similar to "How to get location of ContextMenuStrip", but I need to account for both vertical and horizontal scrolling, which it doesn't (unless I messed something).
So when I'm trying something like this (in PictureBoxPlan.MouseDown):
Dim rpt As Point = Me.PointToClient(PictureBoxPlan.Parent.PointToScreen(e.Location))
...it has a fundamental flaw, because the e.Location is in context of Image, regardless how it is scrolled within the PictureBox. I tried to Form.MouseDown event, but it's not fired, when [right] clicked on a PictureBox.
Just for confirmation, I still need the point in PictureBox context to analyze whether the ContextMenuStrip should be shown or not and to lookup associated ID.
I am now trying to use some constants (may not work if windows "font size" is set to other than 100%) together with scroll bars values, which is not optimal at all.
OK, I found what is breaking the logic in the ContextMenuStrip.Show(Point) documentation:
Show(Point)
Positions the `ToolStripDropDown` relative to the specified screen location.
So the ContextMenuStrip, to my surprise, takes the screen coordinates, not the Form ones. I then just removed PointToClient and everything works as a charm, regardless of window position on the screen or scrollbars position of the Image container:
Dim rpt As Point = PictureBoxPlan.PointToScreen(New Point(e.Location.X, e.Location.Y))
No need to take into account PanelPlan.VerticalScroll.Value or PanelPlan.HorizontalScroll.Value.

Displaying overlayed QGraphicsItems

I have developed an application in PyQt5 that displays an image and allows the user to pan the image using the mouse and zoom using the mouse wheel. I now need to add the functionality to show popup text associated to specific parts of the displayed image. While painting the text directly on to the QGraphicsScene is a possibility, the range of zooms that are commonly used means that the text will be much too small when zoomed out, or much too large when zoomed in.
I would like to achieve a way of placing the QGraphicsTextItem widgets at a static location that is not affected by the pan and zoom. This way, they can be statically located around the perimiter of the QGrapicsView, and I can draw a line from the text box to the position in the scene. My problem is that I cannot find a way to place the text items so they are independant of the QGraphicsScene. I am thinking that I should have the text items external to the scene, and place the QGraphicsView and any text boxes in a parent QObject. I cannot figure out if this is the best method, and have had limited success with trying to connect a line from QGraphicsElipsesItem that marks the position on the image, to a QLabel in the parent QObject.
I hope I have made this clear enough. Please comment if it needs more clarification.

Psychtoolbox scale slider

I'm trying to make a slider for a simple scale where the user can see mouse movement ONLY in the horizontal axis (fixed y location on the horizontal scale).
In more detail: When the scale appears, I want the cursor to appear as a short vertical line (aka slider) in the center of the horizontal scale.
When the user moves the mouse, the slider should move accordingly on the horizontal axis (without reflecting any changes in the vertical axis, i.e. it should stay on the scale)
I'm stuck on both changing the appearance of the cursor to a vertical line slider and on limiting the cursor's movement to the horizontal axis.
Here's what I've tried:
I can successfully place the cursor with SetMouse.
I tried ShowCursor to change the appearance of the cursor, but this only has a few named options and the numbered ones are not portable across OSs ("mapping of numbers to shapes is operating system dependent"), which I need. Any other ideas on how to change the cursor to a vertical line slider?
As for limiting the movement to horizontal, I couldn't find any PTB functions that seem to do this. I did find some workarounds in Matlab to make user GUIs, but it seems these can't be used with PTB's screen. Any ideas would be great!
I'm a still very new to PTB so thank you so much for your help!
I wrote a function for exactly that called slideScale. If you want to see how it works see the test script.
The crucial thing for you is to create a loop, which runs until a click has been made and record the position of the cursor for instance with the function GetMouse(), which gives you the x- and y-coordinates of the cursor. Then, the only thing you basically need is to update the position of your vertical line using the x-coordinate you recorded with GetMouse() without changing the y-coordinates, for which you can just use a fixed value.

WinRT - ImageButton for transparent PNGs

I'm using XAML Toolkit ImageButton control to be able to create normal and pressed states for a button. Code is:
<toolkit:ImageButton NormalStateImageSource="ms-appx:///Assets/1_off.png"
PressedStateImageSource="ms-appx:///Assets/1_on.png"
Width="500"
Height="200">
</toolkit:ImageButton>
Issue I'm facing is, say I have a shape which isn't rectangle or square. For example I have PNGs for star and arrow object. Is there a way to set their boundary corresponding to shape? If not, please advice the best approach to handle such scenarios.
There are two options I tried
When clicking using mouse or touch - you'd check the last position of the pointer before click and see if the image in your button has a non-transparent pixel at that position.
Pros:
It's simpler than option 2.
You get most precise information
Cons:
You can't tell if a button got clicked with mouse, touch, pen, keyboard or by narrator using automation, so you could end up filtering out keyboard clicks just because the mouse cursor is a bit off. You could possibly use some heuristics like how long ago was the pointer move or pointer down event before the click event, but it's a bit hacky and might be unreliable.
Generate a vector path for your image and put it in the button template as a Path element with Fill="Transparent", then mark any other non-transparent or hit testable template elements (buttons, borders with Background="Transparent", etc.) as IsHitTestVisible="false".
Pros:
Doesn't break any input methods
Can be quite precise
For some shapes like a circle - the Path.Data could be quite simple or you could even use something like an Ellipse element instead
Cons:
You need to generate the path somehow
A complex path might adversely affect performance
A better solution overall in most cases is to leave the hit-testable area rectangular. The reason is - an arbitrary shape is a finicky and unreliable hit test target so it makes clicking your button more difficult. Using the default rectangular border or at most - an ellipse shape is a lot simpler and more usable.

Controls change place and form size changes

I have designed a form in VB.NET. At design time I have placed two buttons on it.
When I run it, the form size looks smaller and the buttons I have placed at the bottom are not visible. Also the alignment of the text and picture box is also different from what I set at design time.
Computer at which I am running the form is using a different resolution.
change the properties (F4) of the buttons: in ANCHOR put Bottom, Right
your buttons will be tied to the bottom and the right of the screen, instead of to the top, left, which is the default.
Grab the screen size at runtime with
Dim screen as System.Windows.Forms.Screen = System.Windows.Forms.Screen.PrimaryScreen
and using a scale factor depending on the current size (in design), scale the window to match. Check the coordinates of the buttons by hand to make sure they are not outside of the visible portion of the window.
You may not have to leave this feature in if you can debug it to the point that you know the exact resolution that you need.