Results 1 to 13 of 13

Thread: (Resolved by Serge) Gurus - Em_CharFromPos

  1. #1

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464

    (Resolved by Serge) Gurus - Em_CharFromPos

    Is EM_CHARFROMPOS available in Windows 2000? I am using this code:
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    2. Private Const EM_CHARFROMPOS& = &HD7
    3. Public Type POINTAPI
    4.         x As Long
    5.         y As Long
    6. End Type
    7. Public Function WordUnderCursor(txtBox As TextBox, x As Single, y As Single) As String
    8.     Dim mPosAs POINTAPI
    9.     Dim cPos As Integer
    10.     mPos.x = x / Screen.TwipsPerPixelX
    11.     mPos.y = y / Screen.TwipsPerPixelY
    12.     cPos = SendMessage(txtBox.hWnd, EM_CHARFROMPOS, 0&, mPos)
    13.     etc.......
    cPos is always returned as -1. Also I cannot find EM_CHARFROMPOS in the API Viewer.
    Does anyone have any ideas?

    TIA

    -TiPeRa
    Last edited by TiPeRa; Nov 9th, 2001 at 03:53 PM.
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    it's in Win2k alright...

  3. #3

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    The code works for a RichTextBox, but I can't get it to work for a TextBox
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  4. #4

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    Hmmm...Well...
    According to MSDN:
    Edit controls: The low word of lParam contains the horizontal coordinate. The high word contains the vertical coordinate.
    So I now have this:
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    2. Private Const EM_CHARFROMPOS& = &HD7
    3. Public Type POINTAPI
    4.         x As Long
    5.         y As Long
    6. End Type
    7. Public Function WordUnderCursor(txtBox As TextBox, x As Single, y As Single) As String
    8.     Dim mPosAs POINTAPI
    9.     Dim cPos As Integer
    10.     mPos.x = x / Screen.TwipsPerPixelX
    11.     mPos.y = y / Screen.TwipsPerPixelY
    12.     cPos = SendMessage(txtBox.hWnd, EM_CHARFROMPOS, 0&, LParam(pt.x, pt.y)
    13.     etc.......
    14. End Function
    15. Function LoWord(ByVal Num As Long) As Integer
    16.     If Num And &H8000& Then
    17.         LoWord = Num Or &HFFFF0000
    18.     Else
    19.         LoWord = Num And &HFFFF&
    20.     End If
    21. End Function
    22. Public Function LParam(wLow As Long, wHigh As Long) As Long
    23.    LParam = LoWord(wLow) Or (&H10000 * LoWord(wHigh))
    24. End Function
    But it still doesn't work
    Please help me
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  5. #5

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    This sort of works:
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    2. Private Const EM_CHARFROMPOS& = &HD7
    3. Public Type POINTAPI
    4.         x As Long
    5.         y As Long
    6. End Type
    7. Public Function WordUnderCursor(txtBox As TextBox, x As Single, y As Single) As String
    8.     Dim mPosAs POINTAPI
    9.     Dim cPos As Integer
    10.     mPos.x = x / Screen.TwipsPerPixelX
    11.     mPos.y = y / Screen.TwipsPerPixelY
    12.     cPos = SendMessage(txtBox.hWnd, EM_CHARFROMPOS, 0&, [B]pt.x[/B] )
    13.     etc.......
    14. End Function
    But only for the top line
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  6. #6
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    remind me what you're trying to do again...

  7. #7
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    yes please
    It might be helpfull if you could describe what the goal is of the procedure

  8. #8

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    I am trying to get the character that appears underneath the cursor, the code at the top works for RichTexBoxes but not TextBoxes.
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  9. #9
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Check out this post

  10. #10

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    Thanks for replying

    But...
    I found that before and as you are only passing the x value it will not work for a multiline text box.

    Thanks anyway.
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  11. #11
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Well, if you have a multilined textbox, then its fairly simple to make it to work with that:
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lparam As Any) As Long
    2. Private Type POINTL
    3.         x As Long
    4.         y As Long
    5. End Type
    6. Private Const EM_CHARFROMPOS = &HD7
    7.  
    8. Function MakeDWord(LOWORD As Integer, HIWORD As Integer) As Long
    9.       MakeDWord = (HIWORD * &H10000) Or (LOWORD And &HFFFF&)
    10. End Function
    11.  
    12. Function LOWORD(DWord As Long) As Integer
    13.     If DWord And &H8000& Then
    14.         LOWORD = DWord Or &HFFFF0000
    15.     Else
    16.         LOWORD = DWord And &HFFFF&
    17.     End If
    18. End Function
    19.  
    20. Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    21.     Dim pt As POINTL
    22.     Dim lngRet As Long
    23.     Dim lngVal As Long
    24.    
    25.     pt.x = x / Screen.TwipsPerPixelX
    26.     pt.y = y / Screen.TwipsPerPixelY
    27.    
    28.     lngVal = MakeDWord(CInt(pt.x), CInt(pt.y))
    29.    
    30.     lngRet = SendMessage(Text1.hwnd, EM_CHARFROMPOS, 0&, ByVal lngVal)
    31.     lngRet = LOWORD(lngRet)
    32.     Label1.Caption = lngRet
    33. End Sub

  12. #12

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    You are a GOD.
    145 views and one correct answer, yours.
    Thankyou, thankyou, thankyou.
    I have finally been able tp track down the bug in MY code:
    VB Code:
    1. cPos = SendMessage(txtBox.hWnd, EM_CHARFROMPOS, 0&, LParam(pt.x, pt.y)
    should be:
    VB Code:
    1. cPos = SendMessage(txtBox.hWnd, EM_CHARFROMPOS, 0&, [b]ByVal[/b] LParam(pt.x, pt.y)
    Thankyou again.
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  13. #13
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    You're welcome.

    It's amazing how exiting people get when they get something resolved.

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