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

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

通过Win API 模拟鼠标点击,使C# Java交互 (PART.1 C#部分)
首先是C# 部分:
   调用:
  
System.Collections.Queue q = new Queue();
            q.Enqueue(
new QueueItem("ThunderRT6FormDC","Spy++ Lite V2.1"));
            q.Enqueue(
new QueueItem("SSTabCtlWndClass",""));
            q.Enqueue(
new QueueItem("ThunderRT6CommandButton","保存(&S)"));
            
int i= NativeWin32.ClickOnWindow(q);
            MessageBox.Show(i.ToString());
以上逐级从窗体找到Tab页, 在遭到按钮, 最终将触发点击.
最终的返回值I将体现当前已查找成功的级别.

PINVOKE封装:
using System;
using System.Runtime.InteropServices; 

namespace RemoteCam
{
    
public class QueueItem
    
{
        
public string ClassName;
        
public string WindowCaption;
        
public bool IsParent=true;
        
public QueueItem(string className,string windowCaption)
        
{
            ClassName
=className;
            WindowCaption
=windowCaption;
        }

        
public QueueItem(string className,string windowCaption,bool isParent)
        
{
            ClassName
=className;
            WindowCaption
=windowCaption;
            IsParent
=isParent;
        }

    }

    
public class NativeWin32
    
{
        
//Get Text / Set Text /Click
        public const int WM_GETTEXT = 0x000D; 
        
public const int WM_SETTEXT = 0x000C; 
        
public const int WM_CLICK = 0x00F5; 
        
public const int SW_MAXIMIZE = 0x0003;
        
public const int SW_SHOWNORMAL = 0x0001;

        [DllImport(
"User32.dll",EntryPoint="FindWindow")] 
        
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
 
        [DllImport(
"user32.dll",EntryPoint="FindWindowEx")] 
        
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 
 
        [DllImport(
"User32.dll",EntryPoint="SendMessage")] 
        
public static extern int SendMessage(IntPtr hwnd, int Msg, IntPtr wParam, string lParam); 

        [DllImport(
"user32.dll",EntryPoint="SetForegroundWindow")]
        
public static extern void SetForegroundWindow(IntPtr hwnd);

        [DllImport(
"user32.dll",EntryPoint="ShowWindow")]
        
public static extern void ShowWindow(IntPtr hwnd,int size);


        
public static int ClickOnWindow(System.Collections.Queue queue)
        
{
            
int result = -1;
            IntPtr ParenthWnd 
= new IntPtr(0); 
            IntPtr ChildhWnd 
= new IntPtr(0); 
 
            QueueItem q 
= (QueueItem)queue.Dequeue();

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

                
while(queue.Count>0)
                
{
                    q 
= (QueueItem)queue.Dequeue();
                    ChildhWnd 
= FindWindowEx(ParenthWnd,(IntPtr)0,q.ClassName,q.WindowCaption); 
                    
if (!ChildhWnd.Equals(IntPtr.Zero)) 
                    
{ 
                        
if(q.IsParent)
                            ParenthWnd
=ChildhWnd;
                        result
++;
                    }
else
                        
break;
                }


                
//得到了Button ? 触发它的Click事件 
                if (!ChildhWnd.Equals(IntPtr.Zero) && queue.Count==0) 
                
{ 
                    SendMessage(ChildhWnd,WM_CLICK,(IntPtr)
0,"0");
                }
 
            }
 
 
            
return result; 
        }

    }

}



posted @ 2006-12-13 16:24  craboYang  阅读(1110)  评论(0)  编辑  收藏  举报
刷新评论刷新页面返回顶部
Copyright © 2023 craboYang
Powered by .NET 7.0 on Kubernetes