programming tools for Windows applications development
  Home  |   SwiftForth Archive  |   SwiftX Archive  |

snipped: PROCESS>HWND translates a process handle to its window handle.

From: Charles Melice <3d_at_forthcad.com>
Date: Mon, 10 Jul 2006 17:01:44 +0200

\ The existing >PROCESS word takes an executable and returns its
\ process handle.
\ ---------------------------------------------------------------
\ PROCESS>HWND translates a process handle to its window handle.

Function: GetProcessId ( handle -- id )
Function: GetWindowThreadProcessId ( hWnd lpdwProcessId -- thread-id )
Function: GetWindowTextLength ( hWnd -- nChar )

: PROCESS>HWND ( hproc -- hwndMain|0 )
    \ Waits until the given process is waiting for user input.
    dup 2000 WaitForInputIdle IF drop 0 EXIT THEN

    \ Translate the given process handle to process id.
    GetProcessId dup -EXIT ( target_pid)

    \ Get the first window handle.
    0 0 FindWindow \ test_hwnd
    cell r-alloc \ 'test_pid
    locals| 'test_pid test_hwnd target_pid |

    \ Loop until we find the target or we run out of windows.
    BEGIN test_hwnd WHILE
        \ See if this window has a parent. If not,
        \ it is a top-level window.
        test_hwnd GetParent 0=
        IF \ See if this window have the target process id.
            test_hwnd 'test_pid GetWindowThreadProcessId drop
            'test_pid @ target_pid =
            IF ( This is the target.) test_hwnd EXIT THEN
        THEN
        \ Examine the next window.
        test_hwnd GW_HWNDNEXT GetWindow to test_hwnd
    REPEAT 0 ;

\ ---------------------------------------------------------------
\ Test - Automate Notepad.

: RUN-NOTEPAD ( -- hEdit )
    z" NOTEPAD.EXE" >PROCESS ?dup -EXIT
    PROCESS>HWND dup -EXIT GW_CHILD GetWindow ;

: >NOTEPAD ( zStr hNotepad -- )
    dup GetWindowTextLength locals| len hNotepad text |
    hNotepad EM_SETSEL -1 len SendMessage drop
    hNotepad EM_REPLACESEL 0 text SendMessage drop ;

: <ZCRLF> ( addr -- ) <CRLF> char+ ;

: TEST ( -- )
    RUN-NOTEPAD dup -EXIT
    100 0 DO
        z" This is a text line." over >NOTEPAD
        <ZCRLF> over >NOTEPAD
    LOOP
    drop ;

----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe" to sftalk-request_at_forth.com
For list command help, send subject "help" to sftalk-request_at_forth.com
Message archives are located at http://www.forth.com/archive/sftalk
----------------------------------------------------------------------
This list is a forum for SwiftForth users. For product support and bug
reports, please send email to support_at_forth.com
----------------------------------------------------------------------
Received on Mon Jul 10 2006 - 08:02:46 PDT

This archive was generated by hypermail 2.2.0 : Wed Nov 19 2008 - 03:04:27 PST