Simulate mouse clicks through Win API to make C # Java interaction (PART.1 C # part)

Source: Internet
Author: User
The first part is C:
Call:

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", "Save (& S )"));
Int I = NativeWin32.ClickOnWindow (q );
MessageBox. Show (I. ToString ());

The Tab page is found step by step from the form. When the button is selected, the click is triggered.
The final return value I will reflect the level of successfully searched.

PINVOKE encapsulation: 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 = "find1_wex")]
Public static extern IntPtr find1_wex (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 ();

// Check the form to obtain the entire form
ParenthWnd = FindWindow (q. ClassName, q. WindowCaption );
// Determine whether the form is valid
If (! ParenthWnd. Equals (IntPtr. Zero ))
{
ShowWindow (ParenthWnd, SW_SHOWNORMAL );
SetForegroundWindow (ParenthWnd); // activate the form
Result ++;

While (queue. Count> 0)
{
Q = (QueueItem) queue. Dequeue ();
ChildhWnd = find1_wex (ParenthWnd, (IntPtr) 0, q. ClassName, q. WindowCaption );
If (! ChildhWnd. Equals (IntPtr. Zero ))
{
If (q. IsParent)
ParenthWnd = ChildhWnd;
Result ++;
} Else
Break;
}

// Get the Button? Click Event
If (! ChildhWnd. Equals (IntPtr. Zero) & queue. Count = 0)
{
SendMessage (ChildhWnd, WM_CLICK, (IntPtr) 0, "0 ");
}
}
 
Return result;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.