C#調用WINDOWS API 樣本

來源:互聯網
上載者:User

標籤:

一、調用Windows API。
 C#下調用Windows API方法如下:
 1、引入命名空間:using System.Runtime.InteropServices;
 2、引用需要使用的方法,格式:[DllImport("DLL檔案")]方法的聲明;
 [DllImport("user32.dll")]private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 [DllImport("user32.dll")]private static extern bool SetForegroundWindow(IntPtr hWnd);
 [DllImport("user32.dll")]private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
 [DllImport("user32.dll")]private static extern int SendMessage(IntPtr hWnd,int Msg,int wParam,int lParam);
 [DllImport("user32.dll")]private static extern bool SetCursorPos(int X, int Y);
 [DllImport("user32.dll")]private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
 [DllImport("user32.dll")]private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
  [DllImport("user32.dll")]private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
 //ShowWindow參數
 private const int SW_SHOWNORMAL = 1;
 private const int SW_RESTORE = 9;
 private const int SW_SHOWNOACTIVATE = 4;
 //SendMessage參數
 private const int WM_KEYDOWN = 0X100;
 private const int WM_KEYUP = 0X101;
 private const int WM_SYSCHAR = 0X106;
 private const int WM_SYSKEYUP = 0X105;
 private const int WM_SYSKEYDOWN = 0X104;
 private const int WM_CHAR = 0X102;

二、找到目標視窗
1)、根據視窗的標題得到控制代碼
 IntPtr myIntPtr = FindWindow(null,"視窗名"); //null為類名,可以用Spy++得到,也可以為空白
 ShowWindow(myIntPtr, SW_RESTORE); //將視窗還原
 SetForegroundWindow(myIntPtr); //如果沒有ShowWindow,此方法不能設定最小化的視窗
2)、遍曆所有視窗得到控制代碼
1 定義委託方法CallBack,枚舉視窗API(EnumWindows),得到視窗名API(GetWindowTextW)和得到視窗類別名API(GetClassNameW)
 public delegate bool CallBack(int hwnd, int lParam);
 [DllImport("user32")]public static extern int EnumWindows(CallBack x, int y);
  [DllImport("user32.dll")]private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
 [DllImport("user32.dll")]private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
2 調用EnumWindows遍曆視窗
 CallBack myCallBack = new CallBack(Recall);
 EnumWindows(myCallBack, 0);
3 回調方法Recall
 public bool Recall(int hwnd, int lParam)
 {
  StringBuilder sb = new StringBuilder(256);
  IntPtr PW = new IntPtr(hwnd);

  GetWindowTextW(PW,sb,sb.Capacity); //得到視窗名並儲存在strName中
  string strName = sb.ToString();

  GetClassNameW(PW,sb,sb.Capacity); //得到視窗類別名並儲存在strClass中
  string strClass = sb.ToString();

  if (strName.IndexOf("視窗名關鍵字") >= 0 && strClass.IndexOf("類名關鍵字") >= 0)
  {
   return false; //返回false中止EnumWindows遍曆
  }
  else
  {
   return true; //返回true繼續EnumWindows遍曆
  }
 }
3)、開啟視窗得到控制代碼
1 定義設定使用中視窗API(SetActiveWindow),設定前台視窗API(SetForegroundWindow)
 [DllImport("user32.dll")]static extern IntPtr SetActiveWindow(IntPtr hWnd);
 [DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]static extern bool SetForegroundWindow(IntPtr hWnd);
2 開啟視窗
 Process proc = Process.Start(@"目標程式路徑");
 SetActiveWindow(proc.MainWindowHandle);
 SetForegroundWindow(proc.MainWindowHandle);

三、向指定的視窗輸入資料
1 利用發送訊息API(SendMessage)向視窗發送資料
 InputStr(myIntPtr, _GameID); //輸入遊戲ID
 SendMessage(myIntPtr, WM_SYSKEYDOWN, 0X09, 0); //輸入TAB(0x09)
 SendMessage(myIntPtr, WM_SYSKEYUP, 0X09, 0);
 InputStr(myIntPtr, _GamePass); //輸入遊戲密碼
 SendMessage(myIntPtr, WM_SYSKEYDOWN, 0X0D, 0); //輸入ENTER(0x0d)
 SendMessage(myIntPtr, WM_SYSKEYUP, 0X0D, 0);

 /// <summary>
 /// 發送一個字串
 /// </summary>
 /// <param name="myIntPtr">視窗控制代碼</param>
 /// <param name="Input">字串</param>
 public void InputStr(IntPtr myIntPtr, string Input)
 {
  byte[] ch = (ASCIIEncoding.ASCII.GetBytes(Input));
  for (int i = 0; i < ch.Length; i++)
  {
   SendMessage(PW, WM_CHAR, ch, 0);
  }
 }
2 利用滑鼠和鍵盤類比向視窗發送資料
 SetWindowPos(PW, (IntPtr)(-1), 0, 0, 0, 0, 0x0040 | 0x0001); //設定視窗位置
 SetCursorPos(476, 177); //設定滑鼠位置
 mouse_event(0x0002, 0, 0, 0, 0); //類比滑鼠按下操作
 mouse_event(0x0004, 0, 0, 0, 0); //類比滑鼠放開操作
 SendKeys.Send(_GameID);   //類比鍵盤輸入遊戲ID
 SendKeys.Send("{TAB}"); //類比鍵盤輸入TAB
 SendKeys.Send(_GamePass); //類比鍵盤輸入遊戲密碼
 SendKeys.Send("{ENTER}"); //類比鍵盤輸入ENTER

另:上面還提到了keybd_event方法,用法和mouse_event方法類似,作用和SendKeys.Send一樣。

C#調用WINDOWS API 樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.