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

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

PINVOKE封装:
None.gif using  System;
None.gif
using  System.Runtime.InteropServices; 
None.gif
None.gif
namespace  RemoteCam
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class QueueItem
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public string ClassName;
InBlock.gif        
public string WindowCaption;
InBlock.gif        
public bool IsParent=true;
InBlock.gif        
public QueueItem(string className,string windowCaption)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ClassName
=className;
InBlock.gif            WindowCaption
=windowCaption;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public QueueItem(string className,string windowCaption,bool isParent)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ClassName
=className;
InBlock.gif            WindowCaption
=windowCaption;
InBlock.gif            IsParent
=isParent;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class NativeWin32
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//Get Text / Set Text /Click
InBlock.gif
        public const int WM_GETTEXT = 0x000D
InBlock.gif        
public const int WM_SETTEXT = 0x000C
InBlock.gif        
public const int WM_CLICK = 0x00F5
InBlock.gif        
public const int SW_MAXIMIZE = 0x0003;
InBlock.gif        
public const int SW_SHOWNORMAL = 0x0001;
InBlock.gif
InBlock.gif        [DllImport(
"User32.dll",EntryPoint="FindWindow")] 
InBlock.gif        
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
InBlock.gif 
InBlock.gif        [DllImport(
"user32.dll",EntryPoint="FindWindowEx")] 
InBlock.gif        
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 
InBlock.gif 
InBlock.gif        [DllImport(
"User32.dll",EntryPoint="SendMessage")] 
InBlock.gif        
public static extern int SendMessage(IntPtr hwnd, int Msg, IntPtr wParam, string lParam); 
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll",EntryPoint="SetForegroundWindow")]
InBlock.gif        
public static extern void SetForegroundWindow(IntPtr hwnd);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll",EntryPoint="ShowWindow")]
InBlock.gif        
public static extern void ShowWindow(IntPtr hwnd,int size);
InBlock.gif
InBlock.gif
InBlock.gif        
public static int ClickOnWindow(System.Collections.Queue queue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int result = -1;
InBlock.gif            IntPtr ParenthWnd 
= new IntPtr(0); 
InBlock.gif            IntPtr ChildhWnd 
= new IntPtr(0); 
InBlock.gif 
InBlock.gif            QueueItem q 
= (QueueItem)queue.Dequeue();
InBlock.gif
InBlock.gif            
//查到窗体,得到整个窗体 
InBlock.gif
            ParenthWnd = FindWindow(q.ClassName,q.WindowCaption); 
InBlock.gif            
//判断这个窗体是否有效 
InBlock.gif
            if (!ParenthWnd.Equals(IntPtr.Zero)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                ShowWindow(ParenthWnd,SW_SHOWNORMAL);
InBlock.gif                SetForegroundWindow(ParenthWnd);
//激活窗体
InBlock.gif
                result++;
InBlock.gif
InBlock.gif                
while(queue.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    q 
= (QueueItem)queue.Dequeue();
InBlock.gif                    ChildhWnd 
= FindWindowEx(ParenthWnd,(IntPtr)0,q.ClassName,q.WindowCaption); 
InBlock.gif                    
if (!ChildhWnd.Equals(IntPtr.Zero)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif                        
if(q.IsParent)
InBlock.gif                            ParenthWnd
=ChildhWnd;
InBlock.gif                        result
++;
ExpandedSubBlockEnd.gif                    }
else
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                }

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

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


转载于:https://www.cnblogs.com/crabo/archive/2006/12/13/591127.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值