Windows Develop Bookmark and Share   
 index > Windows Forms General > Show content while dragging
 

Show content while dragging

I have a form1 with datagridview1 and form2 with datagridview2.User wants to drag rows from datagridview2 on form2 to datagridview1 on form1. Its working ok, BUT. needs to be shown row with contents when dragging ... simular like when dragging images in browser opera or firefox (i dont remember which one).Needs help. (i can do this in one form. when dragging source and target in same form, but can do this with two forms)

Lucifer L.  Monday, September 28, 2009 6:48 PM
Hi Lucifer,

We can hook the MouseUp event to reset the cursor. The sample project below shows how to hook mouse events fired anywhere:
http://www.codeproject.com/KB/cs/netwin32hooks.aspx

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 30, 2009 9:19 AM
Hi Lucifer,

The project I provided in the last reply containes a component MouseHookComponent which includes an event MouseMove. You can handle that event to trace the mouse moving.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, October 05, 2009 7:32 AM
You could get the rectangle which contains the row to be dragged, and call Control.DrawToBitmap with that rectangle. Once you have the bitmap, create a blank form, and remove the borders, so it is a plain rectangle. Set the background image to the newly created bitmap, and resize it accordingly. You could also set the forms opacity to 50%.

While the mouse is dragging, you can keep the form at the mouse's position, and then close it once the operation is complete.
Diggsey  Monday, September 28, 2009 7:06 PM
Thanks for reply...
Ido that. thats working in half :) , and i change the code with this... i change the cursor of forms when dragging in to that bitmap from rectangle of row..its working to. But i can't set cursor back to the default or in Your case dispose the form... reason is - > user can drag the row on other place in form. in this case cursor stays the bitmap.. i can't setcursor default on every control in two forms MouseUp event..
Lucifer L.  Tuesday, September 29, 2009 6:31 PM
Hi Lucifer,

We can hook the MouseUp event to reset the cursor. The sample project below shows how to hook mouse events fired anywhere:
http://www.codeproject.com/KB/cs/netwin32hooks.aspx

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 30, 2009 9:19 AM
thanks for help Aland Li.... i still have small problems,i couldtryto solve.here is my code

Public Class Cls_GhostDrag
    Inherits Form

    Private mouseOffset As Point
    Private IsMouseDown As Boolean
    'Private Check As Boolean = False


    Public Sub New()
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Me.BackgroundImageLayout = ImageLayout.None
        Me.ShowInTaskbar = False
        Me.TopMost = True
        Me.StartPosition = FormStartPosition.Manual
        Me.MinimumSize = New Size(10, 10)
        IsMouseDown = False
        Me.AllowDrop = True

    End Sub

    Private Function CreateScreenShotFromForm(ByVal rct As Rectangle, ByVal Par As Form) As System.Drawing.Bitmap
        Dim g As Graphics = Par.CreateGraphics()
        Try
            Dim hBitmap As New Bitmap(rct.Width, rct.Height, g)
            Dim hGraphics As Graphics = Graphics.FromImage(hBitmap)
            hGraphics.CopyFromScreen(Par.Location.X + rct.Left, Par.Location.Y + rct.Top, 0, 0, New Size(rct.Width, rct.Height))
            Return hBitmap
        Catch
            If (Not g Is Nothing) Then g.Dispose()
        Finally
            If (Not g Is Nothing) Then g.Dispose()
        End Try
        Return Nothing
    End Function

    Public Sub ShowGhost(ByVal grid As Xceed.Grid.GridControl, ByVal frm As Form, ByVal pnt As Point)

        Dim rect As Rectangle = New Rectangle( _
                          grid.Left + grid.CurrentRow.ClientRectangleToGrid(grid.CurrentRow.ClientRectangle).Left, _
                          grid.Top + grid.CurrentRow.ClientRectangleToGrid(grid.CurrentRow.ClientRectangle).Top, _
                          grid.CurrentRow.ClientRectangle.Width, _
                          grid.CurrentRow.ClientRectangle.Height)
        Dim bitmap As Bitmap = CreateScreenShotFromForm(rect, frm)
        mouseOffset = pnt
        Me.BackgroundImage = bitmap
        Me.Size = New Size(rect.Width, rect.Height)
        Me.Location = New Point(frm.Left + rect.Left, frm.Top + rect.Top)
        IsMouseDown = True
        Me.Opacity = 0.7
        Me.Show()
        Me.BringToFront()
        Me.Focus()

    End Sub

    Private Sub Cls_GhostDrag_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver
        'Because Form_MouseMove not rises when draging on it
        If IsMouseDown Then
            Me.Location = New Point(Control.MousePosition.X - mouseOffset.X, Control.MousePosition.Y - mouseOffset.Y)
        End If
    End Sub

    Private Sub Cls_GhostDrag_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        ' thsi code snippet is not working ( needs Cls_GhostDrag_DragDrop event )
        If e.Button = MouseButtons.Left Then
            IsMouseDown = False
            Me.Dispose()
        End If
    End Sub

    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        'Cls_GhostDrag
        '
        Me.ClientSize = New System.Drawing.Size(649, 20)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "Cls_GhostDrag"
        Me.Opacity = 0.7
        Me.Text = "Row"
        Me.ResumeLayout(False)

    End Sub
End Class



and after this the row was dropped on this control (cls_GhostDrag) and not on the grid,because mouse always on itwhen dragging)))

Lucifer L.  Wednesday, September 30, 2009 6:37 PM
Hi Lucifer,

The project I provided in the last reply containes a component MouseHookComponent which includes an event MouseMove. You can handle that event to trace the mouse moving.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, October 05, 2009 7:32 AM

You can use google to search for other answers

Custom Search

More Threads

• Advanced search for person nr in file?
• custom combobox control
• Data bound combobox
• Adding Controls
• Re: modal form will not close
• How to clear an image from a PictureBox
• Winform Appearance in XP versus Vista
• Increasing the size of some controls
• unsolved mysteries: rich text box troubles VS.NET2003
• Remembering startup position on 2nd monitor.