Jump to content

_SendMessage/_WinAPI_PostMessage and notepad


Recommended Posts

Trying to learn how to use _SendMessage /_WinAPI_PostMessage with different Windows Messages list like
000c => WM_SETTEXT
000d => WM_GETTEXT
000e => WM_GETTEXTLENGTH
0300 => WM_CUT
0301 => WM_COPY
0302 => WM_PASTE
0303 => WM_CLEAR

Trying to get length of string but it not work....

#include <SendMessage.au3>
#include <WinAPISysWin.au3>

__TestRun()
Func __TestRun()
    Run("notepad.exe")
    WinWait("[CLASS:Notepad]", "", 10)
    Local $o_hWnd = WinGetHandle("[CLASS:Notepad]")
    If @error Then
       ConsoleWrite("An error occurred when trying to retrieve the window handle of Notepad." & @CRLF)
       Exit
    EndIf
    Local $o_SheetHandle = ControlGetHandle($o_hWnd, '', 'Edit1')
    ConsoleWrite("Notepad (hWnd) => " & $o_hWnd & @Tab & "Edit1 (hWnd) => " &$o_SheetHandle & @CRLF)

    Send("ABCD")
    ;~ now how i retrieve text length using _SendMessage
    Local $WM_GETTEXTLENGTH = 0x000e
    _SendMessage($o_SheetHandle, $WM_GETTEXTLENGTH, 0, 0)
    Local $oo_Msg = @error
    ConsoleWrite("_SendMessage: " & $oo_Msg & @CRLF)

    MsgBox(0, "", "Click to continue.....")
    WinClose($o_hWnd)
EndFunc

 

it's bit confusing as WM_PASTE work with _SendMessage but not with _WinAPI_PostMessage :think:

ClipPut("ABCD")
_SendMessage($o_SheetHandle, $WM_PASTE, 0, 0)       ;~ working
_WinAPI_PostMessage($o_SheetHandle, $WM_PASTE, 0, 0)    ;~ not working
ClipPut("")

 

or this code using _WinAPI_PostMessage work but _SendMessage not work why?

Local $VK_RETURN = 0x0D
Local $WM_KEYDOWN = 0x0100
Local $WM_KEYUP = 0101

_SendMessage($o_SheetHandle, $WM_KEYDOWN, $VK_RETURN, 0)    ;~ not working
_SendMessage($o_SheetHandle, $WM_KEYUP, $VK_RETURN, 0)      ;~ not working

_WinAPI_PostMessage($o_SheetHandle, $WM_KEYDOWN, $VK_RETURN, 0) ;~ working
_WinAPI_PostMessage($o_SheetHandle, $WM_KEYUP, $VK_RETURN, 0)   ;~ working

 

So it will helpful if someone knowledgeable could write example/note on AutoIt Example Scripts section about 
_SendMessage _WinAPI_PostMessage  :D

Link to comment
Share on other sites

1 hour ago, jugador said:

Trying to get length of string but it not work....

 

 

$Length = _SendMessage($o_SheetHandle, $WM_GETTEXTLENGTH, 0, 0)
    Local $oo_Msg = @error
    ConsoleWrite("_SendMessage: " & $oo_Msg & @CRLF)
    ConsoleWrite("Length:" & $Length &@CRLF)

 

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

Link to comment
Share on other sites

@ad777 thanks

1)  Now _WinAPI_PostMessage with $WM_PASTE work just have to add sleep(100)

_WinAPI_PostMessage($o_SheetHandle, $WM_PASTE, 0, 0)
Sleep(100)

2)  and for  _SendMessage and $VK_RETURN to work have to use $WM_CHAR instead of $WM_KEYDOWN

_SendMessage($o_SheetHandle, $WM_CHAR, $VK_RETURN, 0)

3)  WM_SETTEXT

Local $WM_SETTEXT = 0x000c
Local $o_Text = "ABCDEF"
Local $t_struct = DllStructCreate("wchar[" & StringLen($o_Text) + 1 & "]")
DllStructSetData($t_struct, 1, $o_Text)
_SendMessage($o_SheetHandle, $WM_SETTEXT, 0, DllStructGetPtr($t_struct))
$t_struct = 0

4) WM_GETTEXT

Local $WM_GETTEXT = 0x000d
Local $o_Text = "ABCDEF"
Send($o_Text)

Local $o_MaxCharacters = StringLen($o_Text) + 1
Local $t_struct = DllStructCreate("wchar[" & $o_MaxCharacters & "]")
_SendMessage($o_SheetHandle, $WM_GETTEXT, $o_MaxCharacters, DllStructGetPtr($t_struct))
ConsoleWrite(DllStructGetData($t_struct, 1) & @CRLF)
$t_struct = 0

5) Ctrl + P

;~ keybd_event by @Bilgus
Local $WM_KEYDOWN = 0x0100
Local $WM_KEYUP = 0x0101
Local $VK_P = 0x50
DllCall('user32.dll', 'int', 'keybd_event', 'byte', 0xA2, 'dword', 0x1D, 'dword', 0x0, 'int', 0)
_WinAPI_PostMessage($o_SheetHandle, $WM_KEYDOWN, $VK_P, 0)
_WinAPI_PostMessage($o_SheetHandle, $WM_KEYUP, $VK_P, 0)
DllCall('user32.dll', 'int', 'keybd_event', 'byte', 0xA2, 'dword', 0x1D, 'dword', 0x0002, 'int', 0)

 

For WM_COMMAND( 0x0111 ) this may help....
https://www.autoitscript.com/forum/topic/205640-guictrlmenuclick/

Edited by jugador
Link to comment
Share on other sites

  • 2 weeks later...

@LarsJ  and  @junkew

to retrieves the command identifier associated with the button we use _GUICtrlToolbar_IndexToCommand
it worked well with notepad++ but failed if try on different software

so is it possible to retrieves the command identifier associated with the button of toolbar using MSAA or UI Automation code 
or have to use Winspector / spy++

Edited by jugador
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...