Share via


VB2010 - how declare SendMessage() API function?

Question

Sunday, April 16, 2017 5:57 PM

i have seen that SendMessage() have several overloads. so i'm confused on how declare it.

see my actual code:

#Region "Interop-Defines"
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure CHARFORMAT
        Public cbSize As Integer
        Public dwMask As UInteger
        Public dwEffects As UInteger
        Public yHeight As Integer
        Public yOffset As Integer
        Public crTextColor As Integer
        Public bCharSet As Byte
        Public bPitchAndFamily As Byte
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
        Public szFaceName As Char()

        ' CHARFORMAT2 from here onwards.
        Public wWeight As Short
        Public sSpacing As Short
        Public crBackColor As Integer
        Public LCID As Integer
        Public dwReserved As UInteger
        Public sStyle As Short
        Public wKerning As Short
        Public bUnderlineType As Byte
        Public bAnimation As Byte
        Public bRevAuthor As Byte
    End Structure

    <DllImport("user32", CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As Integer, ByRef lp As CHARFORMAT) As Integer
    End Function

    Private Const WM_USER As Integer = &H400
    Private Const EM_GETCHARFORMAT As Integer = WM_USER + 58
    Private Const EM_SETCHARFORMAT As Integer = WM_USER + 68
    Private Const SCF_SELECTION As Integer = &H1
    Private Const SCF_WORD As Integer = &H2
    Private Const SCF_ALL As Integer = &H4

#Region "CHARFORMAT2 Flags"
    Private Const CFE_BOLD As UInt32 = &H1
    Private Const CFE_ITALIC As UInt32 = &H2
    Private Const CFE_UNDERLINE As UInt32 = &H4
    Private Const CFE_STRIKEOUT As UInt32 = &H8
    Private Const CFE_PROTECTED As UInt32 = &H10
    Private Const CFE_LINK As UInt32 = &H20
    Private Const CFE_AUTOCOLOR As UInt32 = &H40000000
    Private Const CFE_SUBSCRIPT As UInt32 = &H10000
    ' Superscript and subscript are 
    Private Const CFE_SUPERSCRIPT As UInt32 = &H20000
    ' mutually exclusive 
    Private Const CFM_SMALLCAPS As Integer = &H40
    ' (*) 
    Private Const CFM_ALLCAPS As Integer = &H80
    ' Displayed by 3.0 
    Private Const CFM_HIDDEN As Integer = &H100
    ' Hidden by 3.0 
    Private Const CFM_OUTLINE As Integer = &H200
    ' (*) 
    Private Const CFM_SHADOW As Integer = &H400
    ' (*) 
    Private Const CFM_EMBOSS As Integer = &H800
    ' (*) 
    Private Const CFM_IMPRINT As Integer = &H1000
    ' (*) 
    Private Const CFM_DISABLED As Integer = &H2000
    Private Const CFM_REVISED As Integer = &H4000
    Private Const CFM_BACKCOLOR As Integer = &H4000000
    Private Const CFM_LCID As Integer = &H2000000
    Private Const CFM_UNDERLINETYPE As Integer = &H800000
    ' Many displayed by 3.0 
    Private Const CFM_WEIGHT As Integer = &H400000
    Private Const CFM_SPACING As Integer = &H200000
    ' Displayed by 3.0 
    Private Const CFM_KERNING As Integer = &H100000
    ' (*) 
    Private Const CFM_STYLE As Integer = &H80000
    ' (*) 
    Private Const CFM_ANIMATION As Integer = &H40000
    ' (*) 
    Private Const CFM_REVAUTHOR As Integer = &H8000
    Private Const CFM_BOLD As UInt32 = &H1
    Private Const CFM_ITALIC As UInt32 = &H2
    Private Const CFM_UNDERLINE As UInt32 = &H4
    Private Const CFM_STRIKEOUT As UInt32 = &H8
    Private Const CFM_PROTECTED As UInt32 = &H10
    Private Const CFM_LINK As UInt32 = &H20
    Private Const CFM_SIZE As UInt32 = &H8000000
    Private Const CFM_COLOR As UInt32 = &H40000000
    Private Const CFM_FACE As UInt32 = &H20000000
    Private Const CFM_OFFSET As UInt32 = &H10000000
    Private Const CFM_CHARSET As UInt32 = &H8000000
    Private Const CFM_SUBSCRIPT As UInt32 = CFE_SUBSCRIPT Or CFE_SUPERSCRIPT
    Private Const CFM_SUPERSCRIPT As UInt32 = CFM_SUBSCRIPT
    Private Const CFU_UNDERLINENONE As Byte = &H0
    Private Const CFU_UNDERLINE As Byte = &H1
    Private Const CFU_UNDERLINEWORD As Byte = &H2
    ' (*) displayed as ordinary underline 
    Private Const CFU_UNDERLINEDOUBLE As Byte = &H3
    ' (*) displayed as ordinary underline 
    Private Const CFU_UNDERLINEDOTTED As Byte = &H4
    Private Const CFU_UNDERLINEDASH As Byte = &H5
    Private Const CFU_UNDERLINEDASHDOT As Byte = &H6
    Private Const CFU_UNDERLINEDASHDOTDOT As Byte = &H7
    Private Const CFU_UNDERLINEWAVE As Byte = &H8
    Private Const CFU_UNDERLINETHICK As Byte = &H9
    Private Const CFU_UNDERLINEHAIRLINE As Byte = &HA
    ' (*) displayed as ordinary underline 
#End Region
#End Region

for now i don't get any results. but before i was getting an error on SendMessage() function, because of it's declaration:

<DllImport("user32", CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As Integer, ByRef lp As CHARFORMAT) As Integer
    End Function

can anyone advice me?

All replies (7)

Sunday, April 16, 2017 6:21 PM ✅Answered

I don't know how you are using SendMessage, but you may want to check the below link:

http://vbcity.com/forums/t/138126.aspx

Paul ~~~~ Microsoft MVP (Visual Basic)


Sunday, April 16, 2017 6:23 PM ✅Answered

The API is here with several overloads

http://www.pinvoke.net/default.aspx/user32/SendMessage.html

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Sunday, April 16, 2017 6:10 PM | 1 vote

<DllImport("User32", SetLastError:=True)>
    Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    End Function

Sunday, April 16, 2017 6:18 PM

<DllImport("User32", SetLastError:=True)>
    Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    End Function'...............SendMessage(Me.Handle, EM_GETCHARFORMAT, SCF_SELECTION, RTFformat)

error message:

"Value of type 'scrollbars.PlusRichTextBox.CHARFORMAT' cannot be converted to 'System.IntPtr'."


Sunday, April 16, 2017 6:26 PM

For example :

<DllImport("User32.dll", EntryPoint:="SendMessageW", SetLastError:=True)>
    Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    End Function

(tested  on Windows 10)


Sunday, April 16, 2017 6:34 PM

finally it's fixed.

but let ask anotherthing for close these topic:

how can i test the flags consts?

see these 'if':

If ((RTFformat.dwEffects And CFE_SUBSCRIPT) = CFE_SUBSCRIPT) Then
            RTFformat.dwEffects = Not CFE_SUBSCRIPT
        End If

is these correct for test if the CFE_SUBSCRIPT is on dwEffects flags combinations?

https://msdn.microsoft.com/en-us/library/windows/desktop/bb787883(v=vs.85).aspx

"

dwEffects
Type: DWORD

A set of bit flags that specify character effects. Some of the flags are included only for compatibility with Microsoft Text Object Model (TOM) interfaces; the rich edit control stores the value but does not use it to display text.

This member can be a combination of the following values......."

how can i test flags on VB2010?


Thursday, April 20, 2017 11:55 AM

i was doing something wrong. 1st we need to know the actual flags:

Public Sub SelectionSubScript()
        Dim RTFformat As New CHARFORMAT
        With RTFformat
            .cbSize = Marshal.SizeOf(RTFformat)
            .dwMask = CFM_SUPERSCRIPT Or CFM_SUBSCRIPT
        End With

        SendMessage(Me.Handle, EM_GETCHARFORMAT, SCF_SELECTION, RTFformat)
        If ((RTFformat.dwEffects And CFE_SUPERSCRIPT) = CFE_SUPERSCRIPT) Then RTFformat.dwEffects = RTFformat.dwEffects Xor CFE_SUPERSCRIPT

        If ((RTFformat.dwEffects And CFE_SUBSCRIPT) = CFE_SUBSCRIPT) Then
            RTFformat.dwEffects = RTFformat.dwEffects Xor CFE_SUBSCRIPT
        Else
            RTFformat.dwEffects = RTFformat.dwEffects Or CFE_SUBSCRIPT
        End If
        SendMessage(Me.Handle, EM_SETCHARFORMAT, SCF_SELECTION, RTFformat)
    End Sub

'or' is for combination flags.

'xor' is for delete 1 flag from combination flags.

thanks to all for all