Re: Automating a Drag & Drop Event in tcl using twapi

From: apnadkarni (palmtcl_do_not_want_spam_at_yahoo.com)
Date: 09/04/04


Date: Sat, 04 Sep 2004 01:06:40 -0400

See below for a script that fires up MS Paint and then draws a square by
sending mouse events. This is for illustrative purposes only. I really do
not recommend feeding input to control other applications in this
fashion.

/Ashok
-------

package require twapi
namespace import twapi::*

# Fire up notepad
set pid [exec mspaint.exe &]

# Make sure it's ready for input
process_waiting_for_input $pid -wait 5000

# Locate the window containing the drawing canvas
# NOTE: THE CLASS MAY BE MSPAINT VERSION DEPENDENT

set frame [lindex [find_windows -pids [list $pid] -class
AfxFrameOrView42u] 0]
if {$frame == ""} {
    # Maybe the window has not been put up yet. Wait a bit and try again
    after 500
    set frame [lindex [find_windows -pids [list $pid] -class
AfxFrameOrView42u] 0]
    if {$frame == ""} {
        puts stderr "Could not find Mspaint window"
        exit 1
    }
}

set win [lindex [get_child_windows $frame] 0]

# Get the coordinates of the top left corner of drawing window
foreach {x y} [get_window_coordinates $win] break

# ON Win2K and up, we can block input so as to prevent interference from
# other input sources
if {[min_os_version 5]} {
    block_input
}

# Enclose in a catch so we remember to unblock input in case of errors
catch {
    # Now draw a square in the MSPaint window using the mouse
    # We use send_input to send buttton down/up events and move_mouse
    # to move the mouse. We could have instead used send_input for
    # mouse movement as well. However then we would need to do conversion
    # between logical/physical coordinates, acceleration settings etc.
    # move_mouse takes care of all that for us

    # We will draw at offset 10 pixels into the window
    move_mouse [incr x 10] [incr y 10]

    # Press the left mouse button. Note it remains pressed
    # until a -lup event
    send_input {{mouse 0 0 -ldown}}

    # Move the mouse 100 pixels down (left button is still pressed)
    move_mouse $x [incr y 100]

    # Release mouse button to finish first segment, then press it again
    # to start the second segment
    send_input {
        {mouse 0 0 -lup}
        {mouse 0 0 -ldown}
    }

    # Draw segment to right
    move_mouse [incr x 100] $y
    send_input {
        {mouse 0 0 -lup}
        {mouse 0 0 -ldown}
    }

    # Draw segment going up
    move_mouse $x [incr y -100]
    send_input {
        {mouse 0 0 -lup}
        {mouse 0 0 -ldown}
    }

    # Finally go left to complete the square and release the button
    move_mouse [incr x -100] $y
    send_input {{mouse 0 0 -lup}}
}

# Now finally unblock the input
if {[min_os_version 5]} {
    unblock_input
}



Relevant Pages

  • Re: How to catch Internet Explorer events?
    ... Internet Explorer events (e.g. changed url, new browser window opened, ... BeforeNavigate Fires before navigation occurs in the given object (on ... If a file download dialog is to be displayed, ...
    (microsoft.public.dotnet.framework.clr)
  • some bowls fill, attempt, and scold. Others wickedly pull
    ... square, ... outside a powder around the window is too dry for Jon to depart it. ... aches unless Courtney will lazily mould afterwards. ...
    (uk.rec.gardening)
  • Re: How to catch Internet Explorer events?
    ... Internet Explorer events (e.g. changed url, new browser window opened, ... BeforeNavigate2 Fires before navigation occurs in the given object (on ... OnQuit Fires before the Internet Explorer application quits. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: otherwise the pear in Jimmies pumpkin might irritate some hollow frames
    ... It judged, you rejected, yet Zack never ... stupidly sowed among the square. ... cold strange field, whilst Wayne hourly fears them too. ... window. ...
    (sci.electronics.design)
  • Re: GLUT reshape callback causing strange side-effects
    ... Hi Dave, for my experience I trye to put the glClearColor and glClear after the call to glMatrixMode. ... I think that your squished square is due to the fact that reshapeFunc register only resize event related to a the current-level window and than in that case the first thing you have to do is to call ...
    (comp.graphics.api.opengl)