From: Mike Williams on
"RB Smissaert" <bartsmissaert(a)blueyonder.co.uk> wrote in message
news:ujR59UKJIHA.1620(a)TK2MSFTNGP03.phx.gbl...

> Had a go with PostMessage, but no success.
> What I need is the line number (at the mouse-down
> position) in a multi-line textbox when that textbox
> gets a right-click.

I haven't been following this thread because I've been quite busy with
others, but is this the kind of thing you're looking for?

Mike

Option Explicit
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
Private Const EM_CHARFROMPOS = &HD7
Private Const EM_LINEFROMCHAR = &HC9

Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, x As Single, y As Single)
Dim myPoints As Long
Dim charpos As Long, lineNumber As Long
Dim x2 As Long, y2 As Long
x2 = ScaleX(x, vbTwips, vbPixels)
y2 = ScaleY(y, vbTwips, vbPixels)
myPoints = y2 * &H10000 + x2
charpos = SendMessage(Text1.hwnd, _
EM_CHARFROMPOS, 0&, ByVal myPoints) _
And &HFFFF&
lineNumber = SendMessage(Text1.hwnd, _
EM_LINEFROMCHAR, ByVal charpos, ByVal 0&)
Caption = lineNumber
End Sub



From: Mike Williams on
"RB Smissaert" <bartsmissaert(a)blueyonder.co.uk> wrote in message
news:ujR59UKJIHA.1620(a)TK2MSFTNGP03.phx.gbl...

> What I need is the line number . . .

By the way, the code I've just posted gets the line number (zero based) and
displays it in the Form's Caption, but looking again at the thread it looks
like you're really after the character number. That's okay though because
the code I posted gets both. The character number end up in the variable
charpos, also zero based.

Mike


From: Larry Serflaten on

"RB Smissaert" <bartsmissaert(a)blueyonder.co.uk> wrote
> Had a go with PostMessage, but no success.
> What I need is the line number (at the mouse-down position) in a multi-line
> textbox
> when that textbox gets a right-click.
> Has anybody done this successfully?

A more 'correct' route to kill the context menu would be to subclass the control.

See: http://vbnet.mvps.org/code/subclass/contextmenu.htm

Try this:

Public Function WindowProc(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Select Case uMsg
Case WM_RBUTTONDOWN, WM_RBUTTONUP
If uMsg = WM_RBUTTONDOWN Then
uMsg = WM_LBUTTONDOWN
Else
uMsg = WM_LBUTTONUP
End If
End Select

WindowProc = CallWindowProc(defWndProc, _
hwnd, _
uMsg, _
wParam, _
lParam)

End Function


From: RB Smissaert on
I need the line number and this code seems to work perfect now:

Private Sub Text1_MouseDown(Index As Integer, _
Button As Integer, _
Shift As Integer, _
x As Single, _
y As Single)

Dim lHwnd As Long
Dim myPoints As Long
Dim charpos As Long
Dim lineNumber As Long
Dim x2 As Long
Dim y2 As Long

If m_ReturnLineNumber And Button = 2 Then

'to avoid getting the right-mouse poput
SendMessage Me.hwnd, WM_LBUTTONDOWN, 0, 0

lHwnd = Text1.hwnd

x2 = ScaleX(x, vbTwips, vbPixels)
y2 = ScaleY(y, vbTwips, vbPixels)

myPoints = y2 * &H10000 + x2

charpos = SendMessage(lHwnd, _
EM_CHARFROMPOS, 0&, ByVal myPoints) _
And &HFFFF&
lineNumber = SendMessage(lHwnd, _
EM_LINEFROMCHAR, ByVal charpos, ByVal 0&)

s_RetVal = lineNumber + 1
Me.Hide
End If

End Sub


Thanks for getting me on the right track. I think the essential thing was
that I don't need to send a mouse-down with coordinates.


RBS

"Mike Williams" <mikea(a)whiskyandCoke.com> wrote in message
news:uZDfxLLJIHA.1164(a)TK2MSFTNGP02.phx.gbl...
> "RB Smissaert" <bartsmissaert(a)blueyonder.co.uk> wrote in message
> news:ujR59UKJIHA.1620(a)TK2MSFTNGP03.phx.gbl...
>
>> What I need is the line number . . .
>
> By the way, the code I've just posted gets the line number (zero based)
> and displays it in the Form's Caption, but looking again at the thread it
> looks like you're really after the character number. That's okay though
> because the code I posted gets both. The character number end up in the
> variable charpos, also zero based.
>
> Mike
>
>

From: RB Smissaert on
Yes, that looks better and will try that.

RBS


"Larry Serflaten" <serflaten(a)usinternet.com> wrote in message
news:uyKCwQLJIHA.588(a)TK2MSFTNGP05.phx.gbl...
>
> "RB Smissaert" <bartsmissaert(a)blueyonder.co.uk> wrote
>> Had a go with PostMessage, but no success.
>> What I need is the line number (at the mouse-down position) in a
>> multi-line
>> textbox
>> when that textbox gets a right-click.
>> Has anybody done this successfully?
>
> A more 'correct' route to kill the context menu would be to subclass the
> control.
>
> See: http://vbnet.mvps.org/code/subclass/contextmenu.htm
>
> Try this:
>
> Public Function WindowProc(ByVal hwnd As Long, _
> ByVal uMsg As Long, _
> ByVal wParam As Long, _
> ByVal lParam As Long) As Long
>
> Select Case uMsg
> Case WM_RBUTTONDOWN, WM_RBUTTONUP
> If uMsg = WM_RBUTTONDOWN Then
> uMsg = WM_LBUTTONDOWN
> Else
> uMsg = WM_LBUTTONUP
> End If
> End Select
>
> WindowProc = CallWindowProc(defWndProc, _
> hwnd, _
> uMsg, _
> wParam, _
> lParam)
>
> End Function
>
>