Mình cũng đang quan tâm, về API and hooking. Hy vọng các bạn đi trước có thể giúp đỡ. Hay 1 tài liệu nào dễ hiểu. Thanks nhiều.
Chào các bạn!
Hiện tại mình đang viết 1 chương trình tạm gọi là Auto bấm phím cho game. Đã viết được nhưng do kiến thức về C# còn hạn hẹp nên vẫn chưa biết cách sendkey cho 1 cửa sổ được chỉ định.
Nay mình muốn nhờ các bạn hướng dẫn giúp mình cách bắt cửa sổ game và sendkey cho duy nhất cửa sổ đó cả khi minimize.
Cảm ơn các bạn và mong được giúp đỡ.
Hiện mình có search qua các topic hỗ trợ vấn đề này nhưng nhìn chung khá sơ xài. Vậy mong các bạn rành C# hướng dẫn giúp mình vấn đề này nhé. Cảm ơn các bạn.
Mình cũng đang quan tâm, về API and hooking. Hy vọng các bạn đi trước có thể giúp đỡ. Hay 1 tài liệu nào dễ hiểu. Thanks nhiều.
Bạn tham khảo đoạn mã bên dưới nhé:
1. Tìm cửa sổ game bằng API FindWindow
2. Gửi key bằng SendKeys
Ở ví dụ dưới, send key cho calculator của windows.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
// Send a series of key presses to the Calculator application.
private void button1_Click(object sender, EventArgs e)
{
// Get a handle to the Calculator application. The window class
// and window name were obtained using the Spy++ tool.
IntPtr calculatorHandle = FindWindow("CalcFrame", "Calculator");
// Verify that Calculator is a running process.
if (calculatorHandle == IntPtr.Zero)
{
MessageBox.Show("Calculator is not running.");
return;
}
// Make Calculator the foreground application and send it
// a set of calculations.
SetForegroundWindow(calculatorHandle);
SendKeys.SendWait("111");
SendKeys.SendWait("*");
SendKeys.SendWait("11");
SendKeys.SendWait("=");
}
}
}
Visual C# Code:
namespace WindowsFormsApplication12 { { { InitializeComponent(); } { } // sử dụng hàm API FindWindow để lấy handle của cửa sổ. [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] // Hàm này dùng để active 1 cửa sổ [DllImport("USER32.DLL")] // Send a series of key presses to the Calculator application. { // Get a handle to the Calculator application. The window class // and window name were obtained using the Spy++ tool. // mình để chú thích nguyên văn tiếng anh, cái tham số CalcFrame là class của cửa sổ, Calculator là tiêu đề của cửa sổ, nếu cung cấp tham số là null thì sẽ lấy handle của desktop, hàm này trả về 1 con trỏ IntPtr calculatorHandle = FindWindow("CalcFrame", "Calculator"); // Verify that Calculator is a running process. { MessageBox.Show("Calculator is not running."); } // Make Calculator the foreground application and send it // a set of calculations. // active cửa sổ có handle = calculatorHandle SetForegroundWindow(calculatorHandle); // gửi phím đến cửa sổ đó SendKeys.SendWait("111"); SendKeys.SendWait("*"); SendKeys.SendWait("11"); SendKeys.SendWait("="); // phía trên chỉ là code send key đến cái công cụ tính toán thoai. Còn rất nhiều hàm API thao tác với cửa sổ bạn google đi sẽ rất hữu ích. } } }