還是直接上乾貨吧
程式還挺簡潔的,不過不要小看它,功能可不小!
強調一下,拖動工具得到座標和表單標題的做法,在C#開發中的實現,有可能僅此一家哦。因為本碼農搜遍百度都沒找到,有此功能的都是C++開發的。所以這個創意在C#應用完全是本人獨有。
核心使用功能展示:
這個功能主要目的還是為了獲取到應用程式的控制程式碼,有了控制程式碼可以把表單設定為最上層顯示。(不被其它表單遮蓋)
座標不用設定,就是剛才滑鼠鬆開的位置,停頓時間是單擊後停多少時間,進行下一個操作的時間。
#region API及成員變數
/// <summary>
/// 根據座標獲取視窗控制程式碼
/// </summary>
/// <param name="point">座標</param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(Point point);
public delegate bool EnumChildWindow(IntPtr WindowHandle, string num);
/// <summary>
/// 傳遞訊息給記事本
/// </summary>
/// <param name="hWnd"></param>
/// <param name="Msg"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(int hWnd);
[DllImport("User32.dll")]
public static extern int EnumChildWindows(IntPtr WinHandle, EnumChildWindow ecw, string name);
[DllImport("User32.dll")]
public static extern int GetWindowText(IntPtr WinHandle, StringBuilder Title, int size);
[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr WinHandle, StringBuilder Type, int size);
[DllImport("user32")]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int pid);
[DllImport("user32")]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
//查詢表單控制元件
public int iSecond = 30;
public delegate bool CallBack(int hwnd, int lParam);
public RECT rectMain = new RECT();
private string typeName;
private IntPtr mainHwnd;
public IntPtr ip;
private string BSType = "Chrome_WidgetWin_1";
bool Flag = false;
int X;
int Y;
int times;
private IntPtr mainWindowHandle;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int X; //最左座標
public int Y; //最上座標
public int Height; //最右座標
public int Width; //最下座標
}
/// <summary>
/// 查詢控制程式碼
/// </summary>
/// <param name="hwndParent"></param>
/// <param name="hwndChildAfter"></param>
/// <param name="lpszClass"></param>
/// <param name="lpszWindow"></param>
/// <returns></returns>
[DllImport("User32.DLL")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32")]
public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
public const uint WM_SETTEXT = 0x000C;
public System.Diagnostics.Process Proc;
public System.Windows.Forms.Timer myTimer;
public List<Opt> optList = new List<Opt>();
public Queue<Opt> optQueue = new Queue<Opt>();
#endregion
public void MoveTo(int x1, int y1, int x2, int y2)
{
float k = (float)(y2 - y1) / (float)(x2 - x1);
float b = y2 - k * x2;
for (int x = x2; x != x1; x = x + Math.Sign(x1 - x2))
{
//MoveTo(x1,y1,x,(k*x+b));
SetCursorPos(x, (int)(k * x + b));
Thread.Sleep(3);
}
}
程式碼如下(範例):
private void btnStart_Click(object sender, EventArgs e)
{
foreach (string strLine in richTextBox1.Lines)
{
string[] strInt = strLine.Split(new string[] { "," }, StringSplitOptions.None);
if (strInt.Length <= 2)//小於2個引數:一個Point由X,Y組成
{
continue;
}
int x = int.Parse(strInt[0]);
int y = int.Parse(strInt[1]);
int times = int.Parse(strInt[2]);
optList.Add(new Opt(x, y, times));
}
foreach (Opt opt in optList)
{
optQueue.Enqueue(opt);
}
IntPtr hnd = FindWindow(null, txtTitle.Text);
IntPtr ip = hnd;
lblMessage.Text = ip.ToString();
iSecond = 1;
//myTimer.Interval = 1000 * iSecond;
myTimer.Interval = 1000 * 60 * iSecond;
myTimer.Enabled = false;
FindWindowClass.TopMostWindow.SetTopomost(ip);
SetForegroundWindow((int)ip);
if (optQueue.Count > 0)
{
Opt opt = optQueue.Dequeue();
X = opt.x;
Y = opt.y;
times = opt.Times;
System.Timers.Timer t = new System.Timers.Timer();//範例化
t.Elapsed += new System.Timers.ElapsedEventHandler(CallBack2);
t.AutoReset = false;
t.Interval = 1000 * times;
t.Enabled = true;
}
}
private void CallBack2(object sender, EventArgs e)
{
MoveTo(X, Y, MousePosition.X, MousePosition.Y);
mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), X, Y, 0, IntPtr.Zero);
//Thread.Sleep(200);
mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), X, Y, 0, IntPtr.Zero);
if (optQueue.Count > 0)
{
Opt opt = optQueue.Dequeue();
X = opt.x;
Y = opt.y;
times = opt.Times;
System.Timers.Timer t = new System.Timers.Timer();//範例化
t.Elapsed += new System.Timers.ElapsedEventHandler(CallBack2);
t.AutoReset = false;
t.Interval = 1000 * times;
t.Enabled = true;
}
else
{
foreach (Opt opt1 in optList)
{
optQueue.Enqueue(opt1);
}
Opt opt = optQueue.Dequeue();
X = opt.x;
Y = opt.y;
times = opt.Times;
System.Timers.Timer t = new System.Timers.Timer();//範例化
t.Elapsed += new System.Timers.ElapsedEventHandler(CallBack2);
t.AutoReset = false;
t.Interval = 1000 * times;
t.Enabled = true;
}
}
private void btnStartBrowser_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFile.Text)) return;
try
{
// 瀏覽器程式啟動執行緒
Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = txtFile.Text;
Proc.StartInfo.Arguments = txtNetAddr.Text; //瀏覽器開啟URL引數
Proc.StartInfo.UseShellExecute = false;
Proc.StartInfo.RedirectStandardInput = true;
Proc.StartInfo.RedirectStandardOutput = true;
Proc.Start();
}
catch
{
Proc = null;
}
}
掛機程式的開發本身是存在著侷限性的。往往只能針對某一個或某一類應用程式。但只要你注重思考和挖掘,不管什麼樣的應用程式,總能找到對應的點開發出相應的掛機程式。要想用一個掛機程式一招吃遍天下是不可行的。