Results 1 to 4 of 4

Thread: Mouse down issue with transparency

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    20

    Mouse down issue with transparency

    Hi, I have a simple WPF drawing tools program (Line, box, ellipse, erase). My problem is that when I add transparency to the window thats being drawn on, it starts to have issues recognizing the mouse down event. With the drawing tools i've made, you don't click-drag-release to draw, you just click-drag-click. It's only the second mousedown having this issue. Any advice?

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Mouse down issue with transparency

    If a control is transparent, it won't catch Mouse Down events (because the user actually mouse downed on what he/she can see, which is the layer beneath).

    Instead of transparent, give it a very faint opacity - either set the opacity to 1 instead of 0, or use an Alpha channel value of 1 rather than 0, depending on how you've put transparency in in the first place.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    20

    Re: Mouse down issue with transparency

    I've done this, and it worked well. Now my problem is that when I'm drawing a line or a rectangle, the click is hitting the line instead of the form underneath, and is not finishing the draw.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    20

    Re: Mouse down issue with transparency

    This is my current code for a box tool. For some reason the offset is staying drawn until I start to draw a new box.

    Code:
     Private Sub Window_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseMove
            Dim tX, tY As Integer
            tX = e.GetPosition(Me).X
            tY = e.GetPosition(Me).Y
            If clickCount = 1 Then
                x2 = tX - 1
                y2 = tY - 1
            End If
            If clickCount = 1 Then
    
                Select Case CurrentTool
     Case "Box"
                        Dim BoxCanvas As New Canvas
    
                        Static LastBox As Rectangle
                        Static LastCanvas As Canvas
                        Dim myBox As New Rectangle
                        myBox.Stroke = Brushes.Red
                        BoxCanvas.Children.Remove(LastBox)
                        DrawCanvas.Children.Remove(LastCanvas)
    
                        Dim myWidth As Integer
                        Dim myHeight As Integer
                        If x2 < x1 Then
                            myWidth = x1 - (x2 - 2)
                            Canvas.SetLeft(BoxCanvas, x2 - 2)
                        Else
                            Canvas.SetLeft(BoxCanvas, x1)
                            myWidth = x2 - x1
                        End If
                        If y2 < y1 Then
                            Canvas.SetTop(BoxCanvas, y2 - 3)
                            myHeight = y1 - (y2 - 3)
                        Else
                            Canvas.SetTop(BoxCanvas, y1)
                            myHeight = y2 - y1
                        End If
                        myBox.Width = myWidth
                        myBox.Height = myHeight
                        myBox.HorizontalAlignment = Windows.HorizontalAlignment.Left
                        myBox.VerticalAlignment = Windows.VerticalAlignment.Top
                        myBox.StrokeThickness = 2
                        LastBox = New Rectangle
                        LastBox = myBox
                        LastCanvas = New Canvas
                        LastCanvas = BoxCanvas
                        BoxCanvas.Children.Add(myBox)
                        DrawCanvas.Children.Add(BoxCanvas)
    That is the drawing the user sees while they are still making the box. The following code is what actually commits the drawing and leaves it there.

    Code:
    Private Sub CommitBox()
            Dim BoxCanvas As New Canvas
    
            Static LastBox As Rectangle
            Static LastCanvas As Canvas
            Dim myBox As New Rectangle
            myBox.Stroke = Brushes.Red
            BoxCanvas.Children.Remove(LastBox)
            DrawCanvas.Children.Remove(LastCanvas)
    
            Dim myWidth As Integer
            Dim myHeight As Integer
            If x2 < x1 Then
                myWidth = x1 - (x2 - 2)
                Canvas.SetLeft(BoxCanvas, x2 - 2)
            Else
                Canvas.SetLeft(BoxCanvas, x1)
                myWidth = x2 - x1
            End If
            If y2 < y1 Then
                Canvas.SetTop(BoxCanvas, y2 - 3)
                myHeight = y1 - (y2 - 3)
            Else
                Canvas.SetTop(BoxCanvas, y1)
                myHeight = y2 - y1
            End If
            myBox.Width = myWidth
            myBox.Height = myHeight
            myBox.HorizontalAlignment = Windows.HorizontalAlignment.Left
            myBox.VerticalAlignment = Windows.VerticalAlignment.Top
            myBox.StrokeThickness = 2
            BoxCanvas.Children.Add(myBox)
            DrawCanvas.Children.Add(BoxCanvas)
        End Sub

    Any suggestions? Kind of lost here.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width