Results 1 to 7 of 7

Thread: draging a label without using mouse_move

  1. #1

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Exclamation

    ok I'm making a paint type app so I get to know line, circle, and so on but I have a wierd problem

    here is the form(I'm going to just past in the form as read by notepad, you copy paste to *.txt vb will create a form when you add the *.txt file (if it formats right in here))if you loud it run with it a start up form and try draw text you'll see what i mean
    the main code foor draw text is in the form mdown & mUp and the timer tmrText

    start from version
    sel all the way to bottom

    thanks

    VERSION 5.00
    Begin VB.Form frmDraw
    AutoRedraw = -1 'True
    BorderStyle = 1 'Fixed Single
    Caption = "Draw"
    ClientHeight = 5775
    ClientLeft = 150
    ClientTop = 720
    ClientWidth = 7695
    LinkTopic = "Form1"
    MaxButton = 0 'False
    ScaleHeight = 5775
    ScaleWidth = 7695
    StartUpPosition = 3 'Windows Default
    Begin VB.Timer tmrtext
    Interval = 1
    Left = 7080
    Top = 5280
    End
    Begin VB.Label lblDrawText
    BackStyle = 0 'Transparent
    Height = 255
    Left = 7440
    TabIndex = 0
    Top = 5520
    Visible = 0 'False
    Width = 255
    End
    Begin VB.Menu mnuFIle
    Caption = "&File"
    End
    Begin VB.Menu mnuEdit
    Caption = "&Edit"
    End
    Begin VB.Menu mnuDraw
    Caption = "&Draw"
    Begin VB.Menu mnuDrawBox
    Caption = "Draw &Box"
    End
    Begin VB.Menu mnuDrawCircle
    Caption = "Draw &Circle"
    End
    Begin VB.Menu mnuDrawLine
    Caption = "Draw &Line"
    End
    Begin VB.Menu mnuDrawText
    Caption = "Draw &Text"
    End
    End
    End
    Attribute VB_Name = "frmDraw"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit

    Dim Shape As String
    Dim XStart, YStart As Single
    Dim XPrevious, YPrevious As Single
    Dim PrintText As Boolean

    Private Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINT_API) As Long

    Private Type POINT_API
    x As Long
    y As Long
    End Type
    Private Sub Form_Load()
    PrintText = False
    End Sub

    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = 1 Then
    XStart = x
    YStart = y
    XPrevious = XStart
    YPrevious = YStart
    Me.DrawMode = 7
    Me.DrawStyle = 1
    Select Case Shape$
    Case "Line"
    Me.MousePointer = 2
    Case "Circle"
    Me.MousePointer = 2
    Case "Box"
    Me.MousePointer = 2
    Case "Text"
    If PrintText Then
    Dim DrawString As String
    Dim lX, lY
    Dim lX2, lY2 As Single
    Dim point As POINT_API
    GetCursorPos point
    lX = point.x
    lY = point.y
    lX2 = x
    lY2 = y
    DrawString = InputBox("Enter Text")
    If DrawString = "" Then
    Shape = ""
    Me.MousePointer = 0
    Exit Sub
    End If
    SetCursorPos lX, lY
    lblDrawText.Caption = DrawString
    lblDrawText.Width = TextWidth(DrawString)
    PrintText = False
    Me.MousePointer = 3
    With lblDrawText
    .ForeColor = Me.ForeColor
    .Visible = True
    .Left = lX / Me.Left
    .Top = lY / Me.Top
    tmrtext.Enabled = True
    End With
    Else
    tmrtext.Enabled = False
    Me.CurrentX = x
    Me.CurrentY = y
    Me.Print lblDrawText.Caption
    lblDrawText.Visible = False
    Me.MousePointer = 0
    Shape = ""
    Exit Sub
    End If
    End Select
    End If
    End Sub


    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    Select Case Shape$
    Case "Line"
    Me.MousePointer = 2
    Case "Circle"
    Me.MousePointer = 2
    Case "Box"
    Me.MousePointer = 2
    Case "Text"
    Me.MousePointer = 3
    End Select
    If Button = 1 Then
    Me.DrawMode = 10 'enable dotted line
    Select Case Shape$
    Case "Line"
    Me.Line (XStart, YStart)-(XPrevious, YPrevious)
    Me.Line (XStart, YStart)-(x, y)
    Me.MousePointer = 2
    Case "Circle"
    Me.Circle (XStart, YStart), Sqr((XPrevious - XStart) ^ 2 + (YPrevious - YStart) ^ 2)
    Me.Circle (XStart, YStart), Sqr((x - XStart) ^ 2 + (y - YStart) ^ 2)
    Me.MousePointer = 2
    Case "Box"
    Me.Line (XStart, YStart)-(XPrevious, YPrevious), , B
    Me.Line (XStart, YStart)-(x, y), , B
    Me.MousePointer = 2
    Case "Text"
    'lblDrawText.Left = x
    'lblDrawText.Top = y
    'Me.MousePointer = 3
    End Select
    XPrevious = x
    YPrevious = y
    End If
    End Sub


    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = 1 Then
    Me.DrawMode = 13
    Me.DrawStyle = 0
    Select Case Shape$
    Case "Line"
    Me.Line (XStart, YStart)-(x, y)
    Me.MousePointer = 0
    Shape = ""
    Case "Circle"
    Me.Circle (XStart, YStart), Sqr((x - XStart) ^ 2 + (y - YStart) ^ 2)
    Me.MousePointer = 0
    Shape = ""
    Case "Box"
    Me.Line (XStart, YStart)-(x, y), , B
    Me.MousePointer = 0
    Shape = ""
    Case "Text"
    End Select
    End If
    End Sub


    Private Sub mnuDrawBox_Click()
    Shape = "Box"
    End Sub


    Private Sub mnuDrawCircle_Click()
    Shape = "Circle"
    End Sub


    Private Sub mnuDrawLine_Click()
    Shape = "Line"
    End Sub


    Private Sub mnuDrawText_Click()
    Shape = "Text"
    ' Dim DrawString As String
    ' DrawString = InputBox("Enter Text")
    ' lblDrawText.Caption = DrawString
    ' lblDrawText.Width = TextWidth(DrawString)
    PrintText = True
    End Sub


    Private Sub tmrtext_Timer()
    Dim lX, lY
    Dim point As POINT_API
    GetCursorPos point
    lX = ScaleX(point.x, vbPixels, Me.ScaleMode)
    lY = ScaleY(point.y, vbPixels, Me.ScaleMode)
    ' Screen.TwipsPerPixelY
    lblDrawText.Left = lX 'ScaleX(lX, vbPixels, Me.ScaleMode) - ScaleX(Screen.TwipsPerPixelX, vbPixels, Me.ScaleMode)
    lblDrawText.Top = lY 'ScaleY(lY, vbPixels, Me.ScaleMode) - ScaleY(Screen.TwipsPerPixelY, vbPixels, Me.ScaleMode)
    End Sub


    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    I've changed your timer event slightly:
    Code:
    Private Sub tmrtext_Timer()
        Dim lX, lY
        Dim point As POINT_API
        Dim iDiffX%, iDiffY%
        
        iDiffX = Me.Width - Me.ScaleWidth - 5 * Screen.TwipsPerPixelX
        iDiffY = Me.Height - Me.ScaleHeight
        GetCursorPos point
        lX = ScaleX(point.x, vbPixels, Me.ScaleMode)
        lY = ScaleY(point.y, vbPixels, Me.ScaleMode)
        ' Screen.TwipsPerPixelY
        lblDrawText.Left = lX - Me.Left - iDiffX 
        lblDrawText.Top = lY - Me.Top - iDiffY
    End Sub
    The problem is that with GetCursorPos you get the mouse position counted from the upper left position of the screen and that may not be where your form is positioned.

    Good luck!

    [Edited by Joacim Andersson on 08-04-2000 at 04:01 AM]

  3. #3

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Thumbs up Thanks Joacim

    That did it. Where did you learn about the screen object and it relation to forms? The books I have are so vage about every thing they cover.... maybe you could recamend a good one....



    Thanks

    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  4. #4

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Question could you explain that

    I'm reading the change and not quite sure why it works. Could you explain it to me?



    Thanks agian
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well, there's a lot of good books out there. Some that I like is the "In a nutshell" series from O'Reilly.
    Have a look at http://www.oreilly.com
    They are "no nonsens just give me the facts" kind of books.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    The GetCursorPos gets the cursor position related to the screen and not to your form.
    Lets say that your form Top position is at 1000 twips that would imply that you have to subtract 1000 twips from the point.y position that you get from the call to GetCursorPos so you get the right position within your form.

    The problem is that your Labels top position is countet from the top of the inner border of your form and not from the very top position. You have to redraw your forms height of the title- and menu bar as well.

    This would be the form height minus the form scaleheight. And that's what you store in the iDiffY variable.

    In the iDiffX I added 5 extra pixels otherwise the label would have been just under the cursor and when you make a mouse click the mouse_down and mouse_up events would have been passed to your label and not your form.

    I hope this clarifies things for you.

    Best regards

  7. #7

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Wink It did

    Thanks
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

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