You are using the EM_GETRECT message with the EM_GETMARGINS flag, which retrieves the formatting rectangle of the text in the rich text box, not the margins themselves -
MS Learn | EM_GETMARGINS[
^]
To get the left margin of your richtextbox, you should use the 'EM_GETMARGINS' message directly, and retrieve the values from your 'RECT' structure returned by the message -
Private Function GetSelectionMarginWidth() As Integer
Const EM_GETMARGINS As Integer = &HD4
Dim margins As New RECT()
Dim lParam As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(margins))
SendMessage(Me.Handle, EM_GETMARGINS, IntPtr.Zero, lParam)
margins = Marshal.PtrToStructure(lParam, GetType(RECT))
Marshal.FreeHGlobal(lParam)
Return margins.Left
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Public Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure