2010/02/08

sub GetTreeViewItemRect of Win32::GuiTest functions

Win32::GuiTest is a powerful tool for Windows GUI testing. I love it! This is a small function which allow GuiTest to send Mouse Events to the item inside TreeCtrl.




#!/usr/bin/perl
## Initially generated by Win32-GuiTest Recorder (v1.03)

## Pragmas/Directives
use strict;
use warnings;

use 5.010;

## Imports
use Win32::GuiTest qw/:ALL/;

$Win32::GuiTest::debug = 0; # Set to "1" to enable debug output.

use constant {
    TVM_EXPAND        => 0x1100 + 2,
    TVM_GETITEMRECT   => 0x1100 + 4,
    TVM_GETNEXTITEM   => 0x1100 + 10,
    TVM_SELECTITEM    => 0x1100 + 11,
    TVM_GETITEM       => 0x1100 + 12,
    TVM_ENSUREVISIBLE => 0x1100 + 20,
    TVGN_ROOT   => 0,
    TVGN_NEXT   => 1,
    TVGN_CHILD  => 4,
    TVGN_CARET  => 9,
    TVE_EXPAND  => 2,
};

=item
    Helper function to go through Items inside TreeCtrl.
    It is as same as the internal function of Win32::GuiTest
    But it allows to use REGEX as the item name
=cut
sub xTVPathWalk
{
    my $hwnd     = shift;
    my $tvitem   = shift;
    my $text_buf = shift;
    my $hItem    = shift;
    my $max_buf  = shift;
    my $delay    = shift;
    my @parts    = @_;
    SendMessage( $hwnd, TVM_ENSUREVISIBLE(), 0, $hItem );
    while( $hItem != 0 ){
        my $str_long = pack( "L L L L L L L L L L",
            0x41, #mask (TVIF_TEXT | TVIF_CHILDREN)
            $hItem, #hItem
            0, #state
            0, #stateMask
            $text_buf->{ 'ptr' }, #pszText
            100, #cchTextMax
            0, #iImage
            0, #iSelectedImage
            0, #cChildren
            0  #lParam
        );

        WriteToVirtualBuffer( $tvitem, $str_long );
        SendMessage( $hwnd, TVM_GETITEM(), 0, $tvitem->{ 'ptr' } );
        my $text = ReadFromVirtualBuffer( $text_buf, $max_buf );
        $text =~ s/\0.+$//;
        my $struct = ReadFromVirtualBuffer( $tvitem, 40 );
        my @fields = unpack( "L10", $struct );
        my $titlere = $parts[0];
        if( $text =~ /$titlere/i ){
            SendMessage( $hwnd, TVM_EXPAND(), TVE_EXPAND(), $hItem );
            #
            # Give the node some time to expand...
            #
            select(undef, undef, undef, $delay) if $delay;
            if( @parts == 1 ){
                return $hItem;
            }
            if( $fields[ 8 ] ){
                my $hChild = SendMessage( $hwnd,
                                       TVM_GETNEXTITEM(),
                                       TVGN_CHILD(),
                                       $hItem );
                shift( @parts );
                return xTVPathWalk( $hwnd,
                                   $tvitem,
                                   $text_buf,
                                   $hChild,
                                   $max_buf,
                                   $delay,
                                   @parts );
            }
        }else{
            $hItem = SendMessage( $hwnd,
                                  TVM_GETNEXTITEM(),
                                  TVGN_NEXT(),
                                  $hItem );
        }
    }
    return 0;
}    

=item GetTreeViewItemRect
 Return the RECT of Item related to Screen.
 Parameters are as same parameters as SelTreeViewItem
=cut
sub GetTreeViewItemRect
{
    my $hwnd = shift;
    my $path = shift;
    my $max_buf = shift;
    my $delay = shift;

    if( !$max_buf ){
        $max_buf = 124;
    }
    if( !$delay ){
        $delay = 0.50;
    }
    
    my @parts = split( /\|/, $path );
 
    my $tvitem;
    eval{
        $tvitem = AllocateVirtualBuffer( $hwnd, 50 );
    };
    if( $@ ){
        die "Allocation failed with message ---> $@";
    }
    
    my $text_buf = AllocateVirtualBuffer( $hwnd, $max_buf );

    my $item_rect = AllocateVirtualBuffer( $hwnd, 16 );
 
    my $hItem = SendMessage( $hwnd, TVM_GETNEXTITEM(), TVGN_ROOT(), 0 );
    $hItem = xTVPathWalk( $hwnd,
                          $tvitem,
                          $text_buf,
                          $hItem,
                          $max_buf,
                          $delay,
                          @parts );
    SendMessage( $hwnd, TVM_SELECTITEM(), TVGN_CARET(), $hItem );

    my @rect = (0, 0, 0, 0);
    if ($hItem) {            # TVM_GETITEMRECT = 0x1104
        say "Got the item: $hItem";
        
        SendMessage( $hwnd, TVM_ENSUREVISIBLE(), 0, $hItem );

        WriteToVirtualBuffer( $item_rect, pack('L L L L', $hItem, 0, 0, 0));
        if (SendMessage ( $hwnd, TVM_GETITEMRECT(), 1, $item_rect->{ 'ptr' } ) ) {
            @rect = unpack('L4', ReadFromVirtualBuffer( $item_rect, 16 ) );
        }
    }
    FreeVirtualBuffer( $item_rect );
    FreeVirtualBuffer( $tvitem );
    FreeVirtualBuffer( $text_buf );

    @rect[0,1] = ClientToScreen( $hwnd, $rect[0], $rect[1] );
    @rect[2,3] = ClientToScreen( $hwnd, $rect[2], $rect[3] );

    return @rect;
}

=item SendMouseToTreeViewItem($hwndTreectrl, $path, $mouseevents)
    $hwndTrectrl - the HWND of TreeCtrl
    $path - full path of TreeCtrl item
    $mouseevents - mouse events, please refer to Win32::GuiTest::SendMouse
=cut
sub SendMouseToTreeViewItem {
    
    my $hwnd = shift;
    my $path = shift;

    my @rect = GetTreeViewItemRect($hwnd, $path);
    if ($rect[2] - $rect[0] != 0) {
        MouseMoveAbsPix($rect[0] + 1, $rect[1] + 1);
        SendMouse(shift);
    }
}



Here is a exameple. It will find the first `Windows Explorer' window,

my @windows = FindWindowLike(undef,undef,"ExploreWClass");
if (scalar @windows) {
SetForegroundWindow($windows[0]);
SetActiveWindow($windows[0]);
my ($treectl) = FindWindowLike($windows[0], undef, 'SysTreeView32');
if ($treectrl) {
SendMouseToTreeViewItem($treectl, 'desktop|my computer|c:|perl', '{leftclick}{leftclick}')
}
}
发表评论