Drag and Drop controls and overlapping between - c#

I'm developing a small tool where you can drag and drop controls with the mouse, such as buttons, comboboxes, groupboxes, etc...
As the mouse moves, the control moves too, so the user can get a preview of the position of that control. The problem comes when, the control being dragged is going from one container to another one. Usually resulting in that control moving correctly but just under the second container, and therefore, not visible for the user. Let's say that we have GroupBox A and GroupBox B, and the user wants to move Button from A to B. When the action starts, the button is visible while the movement is inside the bounds of A, but when the mouse gets into B, the Buttons just appears to be in a lower layer of the interface and it becomes invisible.
I've tried to fix it with .BringToFront() and .SendToBack() without results. I'm having some flickering problems also trying to create a bitmap to create that preview even using the methods recomended here to solve it.
Is there a way of fixing it without having to use bitmaps that solves the layer problem?

Related

Move controls between multiple containers in a single form

The questions says it all.
How can I move a control, say a PictureBox between multiple panels, or betwween a panel and a flow layout panel.
I'm aware I can drag and drop controls between multiple panels and such, however this is does not make the control visually movable between the containers. The mouse only changes to a diferent cursor and after you drag to the other control and release the mouse button the control appears on the other container. I require the control to be visually movable.
Can someone provide a simple example, so I can extract the idea to apply to my situation.
NOTE: Runtime of course.
Assuming you need it runtime:
You can save the control as bitmap using Control.SaveToBitmap method
Create cursor from image.
Set the current cursor which we created from control.
Once drag and drop completed reset the cursor.
Let's take a simple example of dragging a button around.
Suppose you have two types of container controls:
1) an X-Y layout
2) a flow layout (assume left to right)
When you click to drag the button, record the x-offset and y-offset from the click to the top left corner of the control. As well, record the index of the control within the Controls collection.
As the mouse moves, first check if the mouse has changed container controls.
If so, then remove the button from its current parent and add it to the new parent.
If the button is added to a flow control, then you need to calculate the new index. To do this, calculate the distance from the mouse to the closest edge of a bounding box of all other controls. Then if the mouse is left of the center of that control, insert to that control's index minus 1, otherwise insert to the right of that control (index + 1).
If the button is added to an X-Y layout, then the index doesn't matter that much. You can simple just set the button's location relative to the mouse plus the x-offset, and y-offset.
As the mouse is dragging, you will need to force the controls to refresh. I think calling Invalidate() on the container control should be sufficient.
This should give you the basic idea that you could use to start coding something.

Display Menubar On Mouse Release

I have displayed an image in a Windows form. The user can draw a rectangle on top of this. When the user releases the mouse after drawing the image, I need to display a few buttons, similarly to showing a tooltip.
So far, I have:
Created a new WinForm named Toolbar
Removed titlebar
Added tooltip control on the WinForm
Added 4 buttons
Being an ASP.NET web developer I perceive the following items as missing:
Handle MouseUp event
Get co-ordinates of the mouse release location (say x1, y1)
Render my Toolbar Winform with top position as (x1, y1)
Let the respective buttons handle their responsibilities in their event handlers
Can you please help me validate my approach and show some pointers for the code?
You can use ContextMenuStrip (info from MSDN)
With 4 Items and just show it on MouseUp event with:
contextMenuStrip1.Show(Cursor.Position);
Two approaches:
This. You create a Popup form, which will be automatically hidden similar to popup menu and can host controls (buttons).
Display (draw) buttons inside your control, on top of its content (on top of graphics which you drawn), process mouse clicks, perform operations accordingly. Most difficult will be to draw nice looking buttons, to example, by using VisualStyleRenderer (xp-style).
Do not aks big question (containing many small one), rather try something, if it doesn't work or you are not satisfied with results, then come here and ask question (while also telling what you are trying to do). This way you will get help very quickly.

How to create moveable "free floating" panel that can freely move over the canvas and is partially transparent

The scenario is that i want the user to create a shape in a small panel that opens (the added shape can later be placed on the canvas), but for a better reference, i want the user to be able to move the semi-transparent panel somewhere on the canvas and then draw with the accurate reference.
Please tell me:
Which panel type to use
How to make it moving by clicking the mouse on the move button (not the whole panel as dragging will be used for drawing lines) and move it around.
How to make it semi transparent.
How to make it appear and disappear (this should be pretty simple)
How to somehow limit its movement inside the canvas so it cannot move on the ribbon
And I really really hope there will be something built-in in WPF that i'll be able to use, and i will not have to do it the hard way i.e. create a rectangle, and do customized hit testing on it to allow the user to draw on top of that rectangle, make that rectangle transparent, and add graphics items for the buttons and controls on that rectangle "panel".
I am asking this because i have never seen such feature in any Windows application and i have no idea what to use for this purpose and how to implement it. The closest thing to what i want is in Adobe Acrobat Pro, which is the small preview of the page that appears when i double click with the middle mouse button. It doesn't move, nor it is transparent or can be drawn upon, but scale and shape wise i want something similar.
You should be able to place a second Canvas inside of your main canvas, and place whatever UserControl you'd like with your "view" inside of it.
You'll have to handle the mouse click/drag for moving it around yourself, but otherwise, it should be very straightforward.

C# WinForms Move elements between panels

I need to create a form with two panels:
1. Destination
2. Source
On the source panel there will be picture boxes. I need to be able to move it from source to point at destination panel with mouse.
I have a problem connected with different coordinates of the panels.
Please, help with advice or an idea what to do.
Moving those controls requires changing their Parent property. That's not easy to do, there is no good time to do this while the user is dragging with the mouse. You'll also get the effect of the panel clipping the control, you cannot display it on both with half of the control on one and the other half on the other panel. And yes, you have to change the Location property of the control when you change the parent or it will jump.
Punt the problem, don't use two panels. It only has to look like a panel, easily done by drawing one in the form's Paint method (or OnPaint override, better). Use e.Graphics.DrawRectangle or FillRectangle.

In WPF, how can I capture mouse on a Canvas and still find what controls the cursor is hovering over?

I have a custom Canvas control (inherited from Canvas) overlaid over a large area of User Controls. The idea is to draw paths between user controls (i.e. connector lines).
To capture mouse movement, I call Mouse.Capture(theCanvas) on MouseDown. This works beautifully, but the user controls under the canvas obviously no longer receive mouse events. Mouse.DirectlyOver always shows the canvas, so I can't really fake it by peeking at the current position and seeing which user control it's over.
So, I still need the Canvas for drawing paths, but how can I solve this one of the following ways:
Peek under the Canvas and see what the topmost control is right under it?
Get this MouseDown -> Track MouseMoves -> MouseUp workflow to work on the canvas without mouse captures?
Any other ideas welcome...
I'd agree that those are your two options. If you want to only forward some clicks to your usercontrols, then go with option 1, and hit test the controls under the canvas.
If you need your usercontrols to behave as though there is nothing covering them (textboxes, buttons etc), then i'd recommend using the PreviewMouseMove event on the user control's parent, as this can pick up and optionally "handle" events before the controls get at the event, but it won't block the event if you don't set handled to true

Categories

Resources