• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
老杨随笔-重用与重构的艺术
专注于RIA架构设计,无止境的重构,无限制的重用
博客园    首页    新随笔    联系   管理    订阅  订阅

通过Win API 模拟鼠标点击,使C# Java交互 (PART.2 Java部分)

Java部分:
   调用:
  
ArrayList<QueueItem> li = new ArrayList<QueueItem>();
        li.add(
new QueueItem("WindowsForms10.Window.8.app3","采集系统"));
        li.add(
new QueueItem("WindowsForms10.BUTTON.app3","选项"));
        
int result = NativeWin32.ClickOnWindow(li);
        System.
out.print(result);

PINVOKE封装采用 NativeCall [ http://johannburkard.de/software/nativecall/ ]

QueueItem,窗体查找时的节点定义.
public class QueueItem
{
    
public String ClassName;
    
public String WindowCaption;
    
public boolean IsParent=true;
    
public QueueItem(String className,String windowCaption)
    
{
        ClassName
=className;
        WindowCaption
=windowCaption;
    }

    
public QueueItem(String className,String windowCaption,boolean isParent)
    
{
        ClassName
=className;
        WindowCaption
=windowCaption;
        IsParent
=isParent;
    }

}


API

import com.eaio.nativecall.IntCall;

public class NativeWin32 {
    
    
public static Integer FindWindow(String sClassName,String sWindowName)
    
{
        IntCall iCall 
= new IntCall("user32", "FindWindowW");
        
int iRet = iCall.executeCall(
                
new Object[] {
                        sClassName,
                        sWindowName}
);
        iCall.destroy();
        
return new Integer(iRet);
    }

    
public static Integer FindWindowEx(Integer hwndParent,Integer hwndChildAfter,String sClassName,String sWindowName)
    
{
        IntCall iCall 
= new IntCall("user32", "FindWindowExW");
        
int iRet = iCall.executeCall(
                
new Object[] {
                        hwndParent,
                        hwndChildAfter,
                        sClassName,
                        sWindowName}
);
        iCall.destroy();
        
return new Integer(iRet);
    }

    
    
private static final int WM_GETTEXT = 0x000D; 
    
private static final int WM_SETTEXT = 0x000C; 
    
private static final int WM_CLICK = 0x00F5; 
    
private static final int SW_MAXIMIZE = 3;
    
private static final int SW_SHOWNORMAL = 1;
    
private static Integer SendMessage(Integer hwnd,int msg,Integer wParam,String lParam)
    
{
        IntCall iCall 
= new IntCall("user32", "SendMessageW");
        
int iRet = iCall.executeCall(
                
new Object[] {
                        hwnd,
                        msg,
                        wParam,
                        lParam}
);
        iCall.destroy();
        
return new Integer(iRet);
    }

    
public static void SetForegroundWindow(Integer hwnd)
    
{
        IntCall iCall 
= new IntCall("user32", "SetForegroundWindow");
        iCall.executeCall(
                
new Object[] {
                        hwnd}
);
        iCall.destroy();
    }

    
public static void ShowWindow(Integer hwnd,int size)
    
{
        IntCall iCall 
= new IntCall("user32", "ShowWindow");
        iCall.executeCall(
                
new Object[] {
                        hwnd,
                        size}
);
        iCall.destroy();
    }

    
    
public static int ClickOnWindow(java.util.ArrayList<QueueItem> queue)
    
{
        
int result = -1;
        Integer ParenthWnd 
= new Integer(0); 
        Integer ChildhWnd 
= new Integer(0); 

        QueueItem q 
= queue.get(result+1);

        
//查到窗体,得到整个窗体 
        ParenthWnd = FindWindow(q.ClassName,q.WindowCaption); 
        
//判断这个窗体是否有效 
        if (ParenthWnd!=0) 
        
{ 
            ShowWindow(ParenthWnd,SW_SHOWNORMAL);
            SetForegroundWindow(ParenthWnd);
//激活窗体
            result++;

            
while(result+1<queue.size())
            
{
                q 
= queue.get(result+1);
                ChildhWnd 
= FindWindowEx(ParenthWnd,0,q.ClassName,q.WindowCaption); 
                
if (ChildhWnd!=0) 
                
{ 
                    
if(q.IsParent)
                        ParenthWnd
=ChildhWnd;
                    result
++;
                }
else
                    
break;
            }


            
//得到了Button ? 触发它的Click事件 
            if (ChildhWnd!=0 && queue.size()==result+1) 
            
{ 
                SendMessage(ChildhWnd,WM_CLICK,
0,"0");
            }
 
        }
 

        
return result; 
    }

}
posted @ 2006-12-13 16:28  craboYang  阅读(3056)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3