我是想做个简单的游戏挂机程序,就是向游戏发送快捷键消息的,如F10,F3等,希望高手能给出解答
问题要求:
1发送消息时不用获得目标窗体的焦点(如果你是用sendkeys或者SetActiveWindow,SetForegroundWindow等等的就不必回答了),也就是你可以边挂机边干别的,不用一直让游戏窗体显示。
2,钩子这个东西我懂得少,最好不要用钩子。
3,最好能用sendmessage,postmessage这类的API,或者能用keybd_event也行。
4,最好能给个发送按键消息的简单例子。
5,能把我下面的代码修改好了也行,我的代码是没有反应,似乎消息发过去不管用,不知道为什么。
//先声明下API
[DllImport("User32.dll", EntryPoint = "PostMessage")]
private static extern int PostMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
); [DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); private const int WM_KEYDOWN = 0x0100;
int t = 0;
//开始按钮
private void button1_Click(object sender, EventArgs e)
{
IntPtr ParenthWnd = new IntPtr(0);
ParenthWnd = FindWindow(null, "窗口标题");
if (ParenthWnd != IntPtr.Zero)
{
timer1.Start();
} else
{
MessageBox.Show("没有找到窗口");
}
}//定时器执行的具体内容
private void timer1_Tick(object sender, EventArgs e)
{
IntPtr ParenthWnd = new IntPtr(0);
ParenthWnd = FindWindow(null, "窗口标题");
Thread.Sleep(500);
t = t + 1;
if (t == 1)
{
PostMessage(ParenthWnd.ToInt32(), WM_KEYDOWN, 121, 0);
}
if (t == 2)
{
PostMessage(ParenthWnd.ToInt32(), WM_KEYDOWN, 114, 0);
}
if (t == 9)
{
t = 0;
}
}
问题要求:
1发送消息时不用获得目标窗体的焦点(如果你是用sendkeys或者SetActiveWindow,SetForegroundWindow等等的就不必回答了),也就是你可以边挂机边干别的,不用一直让游戏窗体显示。
2,钩子这个东西我懂得少,最好不要用钩子。
3,最好能用sendmessage,postmessage这类的API,或者能用keybd_event也行。
4,最好能给个发送按键消息的简单例子。
5,能把我下面的代码修改好了也行,我的代码是没有反应,似乎消息发过去不管用,不知道为什么。
//先声明下API
[DllImport("User32.dll", EntryPoint = "PostMessage")]
private static extern int PostMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
); [DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); private const int WM_KEYDOWN = 0x0100;
int t = 0;
//开始按钮
private void button1_Click(object sender, EventArgs e)
{
IntPtr ParenthWnd = new IntPtr(0);
ParenthWnd = FindWindow(null, "窗口标题");
if (ParenthWnd != IntPtr.Zero)
{
timer1.Start();
} else
{
MessageBox.Show("没有找到窗口");
}
}//定时器执行的具体内容
private void timer1_Tick(object sender, EventArgs e)
{
IntPtr ParenthWnd = new IntPtr(0);
ParenthWnd = FindWindow(null, "窗口标题");
Thread.Sleep(500);
t = t + 1;
if (t == 1)
{
PostMessage(ParenthWnd.ToInt32(), WM_KEYDOWN, 121, 0);
}
if (t == 2)
{
PostMessage(ParenthWnd.ToInt32(), WM_KEYDOWN, 114, 0);
}
if (t == 9)
{
t = 0;
}
}
http://www.cnblogs.com/Wiseman/archive/2005/12/01/288697.html
http://www.cnblogs.com/kokoliu/archive/2007/02/26/632097.html