How to implement mouse dragging in Visual Basic? - vb.net

I need to create a quick-n-dirty knob control in Visual Basic 2005 Express, the value of which is incremented/decremented by "grabbing" it with the mouse and moving the cursor up/down. Because the knob itself doesn't move, I need to keep tracking the mouse movement outside of the rectangle of the control.
I use a Label with an ImageList to implement this (I have a list of 127 bitmaps representing the knob in various positions).
Which events should I react to?

You need the control to handle three events: Mouse Down, Mouse Move and Mouse Up. On the Mouse Down event, you will need to capture the mouse. This means the mouse messages are sent to the control that has the capture. In the mouse move event, if the input is captured then update the displayed image depending on the amount the mouse moved. In the mouse up event, release the capture if the input is captured.
The boolean jjnguy suggests is unnecessary as the Capture property of a Control is readable so it's possible to determine if the capture has been set.

Your problem will be to determine which bitmap you have to display based upon the coordinates the mouse reports in the mouse_move event. You'll need to perform some magic to transform the coordinates and come up with a value that you can use to pick the right image.
It doesn't sound too complicated, just a little bit of trial and error in the math. Skizz has already show you how to capture the events.

Related

How to listen active dragging event of an UWP UIElement?

How to track/listen active dragging event of UI Element in UWP. I added the flat CanDrag=true to enable the drag feature. Using the DragStarting and DragLeave can identify the start and end of drag. But want to get the X,Y coordinate of the UIElement on dragging. Any recommendation to achieve that? I dont want to use RenderTransform for this.
I don't think it's possible in standard Drag'n'Drop flow.
There're few options but they look like a crutch.
You may use manipulations and subscribe to Delta event. That's where RenderTransform comes in.
You may start some time loop and get visual position each frame. (The most crutchy idea)
Try to play wih IsHitTestVisible and subscribe to PointMove of some FrameworkElement behind.
Subscribe to PointEntered and PointExited to the elements you dragging over so they could fire events.
I prefer the first one. It's simple and it's plane.

mouse coordinates in MouseHover event?

I know how to retrieve the mouse coordinate in a PictureBox.Click event though e
In a PictureBox.MouseHover, e does not return such information.
How do I get the mouse coordinates in a MouseHover event ? Is there a way ?
Thanks in advance.
Control.MouseHover "occurs when the mouse pointer rests on the control."
A typical use of MouseHover is to display a tool tip when the mouse pauses on a control within a specified area around the control (the "hover rectangle"). The pause required for this event to be raised is specified in milliseconds by the MouseHoverTime property.
So this event is not raised only whenever the mouse is over the control - there is a delay associated. So the position is somewhat irrelevant, as the mouse could have moved somewhat during that delay.
Do you really need to be using this event? As Dan-o mentioned, MouseMove passes a MouseEventArgs which does provide the coordinates, as you request. It may be the right option, depending on what exactly you're trying to do.
To get the mouse position at any time though, you can use the Cursor.Position property. This will give you the screen coordinates of the cursor. From here, you can call the Control.PointToClient method, to get the coordinates relative to a particular Control.

Microsoft Visio Dynamic Connectors

I'm creating a bunch of flowcharts for data mapping through this database I'm working with, however it's getting quite messy and I was looking for a solution. Right now I have a ton of lines connecting all the variables together, but I was wondering if there is a way to add mouse over events to a text object or something so that when you hover over it, it display a connecting arrow to whatever it is related to; and removing your mouse from the object would make the connector disappear.
Does visio have any sort of support for this type of action in macros or in the ShapeSheet?
I don't know of any mouseover event in Visio, but there is a mouse move event. So every time the mouse moves you find out if the mouse is over one of your shapes, and if so then draw your arrow, if not, delete any existing arrow(s).
Finding the shapes under the mouse, quickly, may be a challenge. I don't know of any Visio routine to get the shapes that occupy an x, y point. You can draw a circle and use the SpatialNeighbors routine to find other shapes at that point.

Rectangles & Parsing in 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.

allowing user to resize trackbar

i would like to allow the user to play around with the size of the trackbar in vb.net. is there an easy way to do this?
i want to clarify that i would like the user to be able to just resize the trackbar by dragging it just like in design mode.
You can assign a new size of the trackbar like this:
TrackBar1.Size = New Point(newwidth, newheight)
One way to handle the user interface you mentioned would be to use mousemove, mouseup, and mousedown events of the trackbar.
When you get a mousemove trackbar event with the left mouse button up, you could change the cursor depending on whether it's near an edge (left-right arrows for left or right edge, up-down arrows for top or bottom edge), near a corner (diagonal arrows), or elsewhere in the middle of the trackbar (4 arrows).
When you get a mousedown trackbar event, save the location. If it's near an edge or corner, you will be stretching the edge or the corner of the trackbar. If it's in the central area, you'll be moving it (if that's an option).
When you get a mousemove trackbar event with the left button down, move and/or resize the trackbar using the trackbar size and location properties. Compare the current location with the one you saved at the mousedown event, and stretch or move that distance. Perform the resize and/or relocate according to the current operation as defined by the location in the previous mousedown event (edge stretch, corner stretch, or move). You can either draw a rectangle or resize the trackbar at this point, whichever looks better to you.
When you get a mouseup event, finalize the operation by setting the new trackbar size and location.