2011-07-09 94 views
0

我需要模擬遊戲窗口中的按鍵。我嘗試發送密鑰「A」,但它不起作用:PostMessage在Windows 7 x64中無效

[DllImport("user32.dll", SetLastError = true)] 
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

[DllImport("USER32.DLL")] 
public static extern bool SetForegroundWindow(IntPtr hWnd); 

[DllImport("user32.dll")] 
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); 

private void button1_Click(object sender, EventArgs e) 
{ 
    IntPtr hWnd = FindWindow(null, "Game Name"); // it's work! 

    if (hWnd == IntPtr.Zero) 
    { 
     MessageBox.Show("Game is not running"); 
     return; 
    } 

    SetForegroundWindow(hWnd); // it's work too and now I have active window of game 
    System.Threading.Thread.Sleep(3000); 

    const int WM_KEYDOWN = 0x0100; 
    PostMessage(hWnd, WM_KEYDOWN, (IntPtr)Keys.A, IntPtr.Zero);  // don't work ;-(
} 

回答

3

你會得到什麼錯誤?遊戲是否以管理員身份運行?您可能被UIPI阻止。

+0

我沒有收到任何錯誤。在Spy ++鍵盤上的日誌是空的。我不認爲這是因爲其他類似的程序可以在沒有問題的情況下使用這個遊戲。 –

+0

對不起,這是我的錯。我忘了用管理員權限運行Visual Studio –