2012-04-19 65 views
0

我試圖模擬在C#中的按鍵和我得到這些錯誤:C#PostMessage的必要庫是什麼?

Error 2 The name 'WM_KEYDOWN' does not exist in the current context c:\users\frk\documents\visual studio 2010\Projects\HaxBot3\HaxBot3\Form1.cs 28 52 HaxBot3 
Error 5 The name 'WM_KEYDOWN' does not exist in the current context c:\users\frk\documents\visual studio 2010\Projects\HaxBot3\HaxBot3\Form1.cs 29 52 HaxBot3 
Error 8 The name 'WM_KEYDOWN' does not exist in the current context c:\users\frk\documents\visual studio 2010\Projects\HaxBot3\HaxBot3\Form1.cs 30 52 HaxBot3 
Error 9 The name 'VK_RIGHT' does not exist in the current context c:\users\frk\documents\visual studio 2010\Projects\HaxBot3\HaxBot3\Form1.cs 30 64 HaxBot3 
Error 3 The name 'VK_CONTROL' does not exist in the current context c:\users\frk\documents\visual studio 2010\Projects\HaxBot3\HaxBot3\Form1.cs 28 64 HaxBot3 
Error 6 The name 'VK_ALT' does not exist in the current context c:\users\frk\documents\visual studio 2010\Projects\HaxBot3\HaxBot3\Form1.cs 29 64 HaxBot3 
Error 1 The name 'PostMessage' does not exist in the current context c:\users\frk\documents\visual studio 2010\Projects\HaxBot3\HaxBot3\Form1.cs 28 17 HaxBot3 
Error 4 The name 'PostMessage' does not exist in the current context c:\users\frk\documents\visual studio 2010\Projects\HaxBot3\HaxBot3\Form1.cs 29 17 HaxBot3 
Error 7 The name 'PostMessage' does not exist in the current context c:\users\frk\documents\visual studio 2010\Projects\HaxBot3\HaxBot3\Form1.cs 30 17 HaxBot3 

,這是給出了錯誤代碼:

public static void Forward() 
     { 
      Process[] processes = Process.GetProcessesByName("test"); 

      foreach (Process proc in processes) 
      { 
       PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_CONTROL, 0); 
       PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_ALT, 0); 
       PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_RIGHT, 0); 
      } 
     }//Fprward 

我想我要補充的東西與using System.(something)但什麼?感謝幫助。

+1

它們應該是windows消息值。如果你想要一個友好的枚舉,你必須自己寫,或者在網上找一個。 – asawyer 2012-04-19 21:18:39

回答

2

你需要自己定義這些。

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

public const uint WM_KEYDOWN = 0x0100; 
public const uint VK_RIGHT = 27; 
public const uint VK_CONTROL = 11; 
public const uint VK_ALT = 12; 
1

它在User32.Dll中。你需要自己指定它。 Visit this PInvoke.net page on PostMessage for more information

下面是一個示例類,假設Forward()在類MyClass中。

public static class MyClass 
{ 
    public static void Forward() 
    { 
    /* snip */ 
    } 

    [return: MarshalAs(UnmanagedType.Bool)] 
    [DllImport("user32.dll", SetLastError = true)] 
    static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam); 
}