作者:易庆华先生
*地址:湖南省岳阳县荣家湾镇城南村
*E-Mail: yqh2648@163.com jiayistudio@163.com
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
string lpClassName, lpWindowName;
StringBuilder lpString;
#region Import Dll
//窗体闪烁
[DllImport("user32.dll")]
static extern int FlashWindow(IntPtr hwnd,int bInvert);
//查找指定窗体
[DllImport("user32.dll", SetLastError = true,EntryPoint="FindWindowA")]
static extern int FindWindow(String lpClassName,String lpWindowName);
//查找指定窗体标题
[DllImport("user32.dll", SetLastError = true, EntryPoint = "GetWindowTextA")]
static extern int GetWindowText(int hwnd,StringBuilder lpString,int cch);
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(
int hwnd,
IntPtr lpdwProcessId);
//获取鼠标坐标
[DllImport("user32.dll")]
private static extern int GetCursorPos(
ref POINTAPI lpPoint);
[StructLayout(LayoutKind.Sequential)]
private struct POINTAPI
{
internal int x;
internal int y;
}
//设置鼠标坐标
[DllImport("user32.dll")]
private static extern int SetCursorPos(
int x,
int y);
[DllImport("User32.dll")]
public static extern Boolean EnumChildWindows(int hWndParent, Delegate lpEnumFunc, int lParam);
public delegate int Callback(int hWnd, int lParam);
#endregion
public int EnumChildGetValue(int hWnd, int lParam)
{
StringBuilder formDetails = new StringBuilder(256);
int txtValue;
string editText = "";
txtValue = GetWindowText(hWnd, formDetails, 256);
editText = formDetails.ToString().Trim();
listBox1.Items.Add("子窗体:"+editText);
return 1;
}
public Form1()
{
InitializeComponent();
listBox1.Items.Clear();
int sRc = FlashWindow(this.Handle, 5);
}
public void Insert_listbox()
{
StringBuilder lpString = new StringBuilder(50);
lpClassName = "#32770";
lpWindowName = "QQ用户登录";
int hwnd=FindWindow(lpClassName,lpWindowName);
listBox1.Items.Add("QQ登录窗体句柄:0x" +Convert.ToString(hwnd,16));
int lp=GetWindowText(hwnd, lpString, 40);
listBox1.Items.Add("窗体标题是:"+lpString.ToString());
int GetWindowThreadProcessId(hwnd,IntPtr.Zero);
listBox1.Items.Add("进程ID:0x"+Convert.ToString(id,16));
Callback myCallBack = new Callback(EnumChildGetValue);
EnumChildWindows(hwnd, myCallBack, 0);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
listBox1.Items.Clear();
Insert_listbox();
int sRc = FlashWindow(this.Handle, 5);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
try {
//显示鼠标坐标
POINTAPI pt = new POINTAPI();
GetCursorPos(ref pt);
this.Mouselabel.Text = "X:" + pt.x + "Y:" + pt.y;
}catch(Exception err)
{
MessageBox.Show(err.Message);
}
}
private void Auto_Mouse_Btn_Click(object sender, EventArgs e)
{
this.timer2.Enabled = true;
listBox1.Items.Add("鼠标自动移到中......");
}
private void timer2_Tick(object sender, EventArgs e)
{
Random random = new Random();
int x = random.Next(500);
int y = random.Next(458);
int sRC=SetCursorPos(x, y);
}
}
}