通过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; 
        }

    }

}



你可能感兴趣的:(java)