|
-
Feb 8th, 2011, 12:58 AM
#1
Thread Starter
Lively Member
[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.
-
Feb 8th, 2011, 05:14 AM
#2
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.
-
Feb 8th, 2011, 07:06 AM
#3
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
-
Feb 8th, 2011, 08:39 AM
#4
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
-
Feb 8th, 2011, 08:47 AM
#5
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.
-
Feb 8th, 2011, 01:10 PM
#6
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|