Results 1 to 6 of 6

Thread: [RESOLVED] SetCursorPos

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Posts
    95

    Resolved [RESOLVED] SetCursorPos

    Been struggling with this for 3 hours, is really frustrating, any help would be much appreciated. All I wanna do is move the mouse to the selected text in my richtext control.

    Code:
    Dim CaretLoc As POINTAPI
    GetCaretPos CaretLoc
    SetCursorPos CaretLoc.X * Screen.TwipsPerPixelX, CaretLoc.Y * Screen.TwipsPerPixelY
    doesn't put the mouse anywhere near where I want, i've taken out the screen stuff too, changed it to divide, neither helped.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SetCursorPos

    To convert from Twips to Pixels you divide by TwipsPerPixel, however there is no need to do it at this stage - because it seems that GetCaretPos and SetCursorPos both use pixels.

    This part of the documentation for GetCaretPos seems to be the important point:
    The caret position is always given in the client coordinates of the window that contains the caret.
    This isn't overly clear, but I suspect that it means the position is relative to the top-left of the control, rather than the top-left of the screen as you seem to have assumed.

    Remove the TwipsPerPixel parts, and try it out with a few different window positions to see if the amount it is wrong by is the same as the top/left of the control.

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: SetCursorPos

    If unsure, it might be useful to stick a temporary label on your form to display the returned Caret position, just to clearly show what you are passing to SetCursorPos(). You can use ClientToScreen() to convert client coordinates to screen coordinates.
    Last edited by Milk; Feb 8th, 2011 at 07:07 AM. Reason: added link
    W o t . S i g

  4. #4
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: SetCursorPos

    May not be what you want, but why not use a modular variable
    to keep the selection change start position, as in below:
    Code:
    Option Explicit
    Private SelPos As Long
    Private Sub Form_Load()
     Dim i As Long
     For i = 1 To 10
      With RichTextBox1  'assign some text
       .SelLength = Len(.Text)
       .SelText = "A Line of Text" & vbCrLf
      End With
     Next
    End Sub
    Private Sub Command1_Click() 'set the cursor to the start of selected text
     With RichTextBox1
      .SelStart = SelPos
      .SetFocus
     End With
    End Sub
    Private Sub RichTextBox1_SelChange() 'save the position
     With RichTextBox1
      SelPos = .SelStart
     End With
    End Sub

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: SetCursorPos

    GetCaretPos uses client coordinates, SetCursorPos uses screen coordinates.
    So, this may do the trick... as suggested by Milk
    Code:
    ' here's the other API
    Private Declare Function ClientToScreen Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
    
    
    ' your adjusted code
        GetCaretPos CaretLoc 
        ClientToScreen RichTextBox1.hwnd, CaretLoc 
        SetCursorPos CaretLoc.x, CaretLoc.y
    Note that an app shares only 1 caret. You'll need to know which control has the caret, if any. Should check the return value of GetCaretPos also.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Posts
    95

    Re: [RESOLVED] SetCursorPos

    Thanks guys! That got it work, specifically LaVolpe's code. I love this forum

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