SendMessage to Watermarking Edit Controls

Labi 21 Reputation points
2021-03-06T10:24:15.29+00:00

Hello Guys,

I would like to write in an other application Watermarking (for example: Type here) search textbox .
75051-empty.jpg

I tried usung the sendMassage methods, but it was unsuccesful. If the textbox is unfocused I changed only the grayed watermarked text.
74918-empty2.jpg

If I clicked in the textbox with mouse after works well.
75004-empty3.jpg

I tried to make mouse events on this textbox, but not the best solution because sometimes some form over on this textbox. I tried to activate the control, but not worked. I tested the SendKey method too.

Could you help me?

Thanks,
Istvan

Here is the code:

     Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer   
    Private Declare Function SendKey Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, wMsg As Int32, wParam As Byte, lParam As Int32) As Int32  
           
    Private Const WM_SETTEXT As Integer = &HC   
    Private Const WM_ACTIVATE As Integer = &H6   
    Private Const WA_ACTIVE As Integer = &H1   
    Const WM_CHAR = &H102   
    Const WM_KEYDOWN As Integer = &H100   
    Const WM_KEYUP As Integer = &H101   
      
         
      
    Sub SendTextToTextbox()   
      
         Dim iHwndChild3 As IntPtr = FindWindowEx(iHwndChild2a, IntPtr.Zero, "TMEditTextClass", vbNullString)   
      
         SendMessage(iHwndChild3, WM_ACTIVATE, 0, 0) SendMessage(iHwndChild3, WM_SETTEXT, IntPtr.Zero, "Text to send")   

         'Alternatively use SendKey methods   
        ' SendKeysO(iHwndChild3, 25, "Text to send" & vbLf)   
           
    End Sub  
  
  
 Public Sub SendKeysO(handle As IntPtr, delay As Integer, chars As String)  
            Dim Bytes As List(Of Byte) = System.Text.Encoding.ASCII.GetBytes(chars).ToList  
            For Each B As Byte In Bytes  
                If B = 10 Then  
                    SendKey(handle, WM_KEYDOWN, CByte(Keys.Enter), 0)  
                    SendKey(handle, WM_KEYUP, CByte(Keys.Enter), 1)  
                End If  
                SendKey(handle, WM_CHAR, B, 0)  
                Dim Sw As Stopwatch = Stopwatch.StartNew  
                Do  
                    Application.DoEvents()  
                Loop Until Sw.ElapsedMilliseconds >= delay  
            Next  
        End Sub  
      
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,580 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,826 Reputation points
    2021-03-06T14:12:11.373+00:00

    Another test (works for standard Edit controls, not sure for your control...) =>

           PostMessage(hWndDest, WM_LBUTTONDOWN, 0, IntPtr.Zero)
           PostMessage(hWndDest, WM_LBUTTONUP, 0, IntPtr.Zero)
           PostMessage(hWndDest, WM_CHAR, Keys.A, IntPtr.Zero)
    

    Declarations :

    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal msg As UInteger, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    End Function
    
    Public Const WM_CHAR = &H102
    Public Const WM_LBUTTONDOWN = &H201
    Public Const WM_LBUTTONUP = &H202
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Castorix31 81,826 Reputation points
    2021-03-06T11:18:26.467+00:00

    WM_SETTEXT works for me :

    75017-cuebanner.gif

    Dim hWndDest As IntPtr = IntPtr.Zero  
    hWndDest = &H511E8 ' Test on an Edit control handle with Cue Banner  
    SendMessage(hWndDest, WM_SETTEXT, 0, Marshal.StringToHGlobalUni("This is a test"))  
    

    Declarations :

    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>  
    Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer  
    End Function  
      
    Public Const WM_SETTEXT = &HC  
    
    0 comments No comments

  2. Labi 21 Reputation points
    2021-03-06T13:09:28.707+00:00

    Thanks.
    Not working :(

    I noticed that the textbox handle changes when a search is performed when I press an enter.
    When I click the box again, the handle is different. That is, the Watermark text handle is different and the normal typed text handle is different.
    Can I get somehow a normal text handle? I also only see WinSpy ++ when I click and type in it.


  3. Labi 21 Reputation points
    2021-03-06T13:55:00.947+00:00

    I think not.
    This is SAP Bussines One cliend search box

    0 comments No comments

  4. Labi 21 Reputation points
    2021-03-06T14:29:25.583+00:00

    THANKS!!
    :)
    WORKING