当前位置:   article > 正文

API(14)——用C#调用Windows API向指定窗口发送_setactivewindow怎样声明

setactivewindow怎样声明

一、调用Windows API

 C#下调用Windows API方法如下:
 1、引入命名空间:using System.Runtime.InteropServices;
 2、引用需要使用的方法,格式:[DllImport("DLL文件")]方法的声明;

  1.  [DllImport("user32.dll")]private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  2.  [DllImport("user32.dll")]private static extern bool SetForegroundWindow(IntPtr hWnd);
  3.  [DllImport("user32.dll")]private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
  4.  [DllImport("user32.dll")]private static extern int SendMessage(IntPtr hWnd,int Msg,int wParam,int lParam);
  5.  [DllImport("user32.dll")]private static extern bool SetCursorPos(int X, int Y);
  6.  [DllImport("user32.dll")]private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
  7.  [DllImport("user32.dll")]private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
  8.   [DllImport("user32.dll")]private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
  9.  //ShowWindow参数
  10.  private const int SW_SHOWNORMAL = 1;
  11.  private const int SW_RESTORE = 9;
  12.  private const int SW_SHOWNOACTIVATE = 4;
  13.  //SendMessage参数
  14.  private const int WM_KEYDOWN = 0X100;
  15.  private const int WM_KEYUP = 0X101;
  16.  private const int WM_SYSCHAR = 0X106;
  17.  private const int WM_SYSKEYUP = 0X105;
  18.  private const int WM_SYSKEYDOWN = 0X104;
  19.  private const int WM_CHAR = 0X102;

 //ShowWindow参数    //显示Window
 //SendMessage参数    //发送信息

二、找到目标窗口

1)、根据窗口的标题得到句柄

  1.  IntPtr myIntPtr = FindWindow(null,"窗口名"); //null为类名,可以用Spy++得到,也可以为空
  2.  ShowWindow(myIntPtr, SW_RESTORE); //将窗口还原
  3.  SetForegroundWindow(myIntPtr); //如果没有ShowWindow,此方法不能设置最小化的窗口

2)、遍历所有窗口得到句柄

1.定义委托方法CallBack,枚举窗口API(EnumWindows),得到窗口名API(GetWindowTextW)和得到窗口类名API(GetClassNameW)。

  1.  public delegate bool CallBack(int hwnd, int lParam);
  2.  [DllImport("user32")]public static extern int EnumWindows(CallBack x, int y);
  3.  [DllImport("user32.dll")]private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
  4.  [DllImport("user32.dll")]private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

2.调用EnumWindows遍历窗口

  1.  CallBack myCallBack = new CallBack(Recall);
  2.  EnumWindows(myCallBack, 0);

3.回调方法Recall

  1.  public bool Recall(int hwnd, int lParam)
  2.  {
  3.   StringBuilder sb = new StringBuilder(256);
  4.   IntPtr PW = new IntPtr(hwnd);
  5.   GetWindowTextW(PW,sb,sb.Capacity); //得到窗口名并保存在strName中
  6.   string strName = sb.ToString();
  7.   GetClassNameW(PW,sb,sb.Capacity); //得到窗口类名并保存在strClass中
  8.   string strClass = sb.ToString();
  9.   if (strName.IndexOf("窗口名关键字") >= 0 && strClass.IndexOf("类名关键字") >= 0)
  10.   {
  11.    return false; //返回false中止EnumWindows遍历
  12.   }
  13.   else
  14.   {
  15.    return true; //返回true继续EnumWindows遍历
  16.   }
  17.  }

3)、打开窗口得到句柄

1.定义设置活动窗口API(SetActiveWindow),设置前台窗口API(SetForegroundWindow)。

  1.  [DllImport("user32.dll")]static extern IntPtr SetActiveWindow(IntPtr hWnd);
  2.  [DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]static extern bool SetForegroundWindow(IntPtr hWnd);

2.打开窗口

  1.  Process proc = Process.Start(@"目标程序路径");
  2.  SetActiveWindow(proc.MainWindowHandle);
  3.  SetForegroundWindow(proc.MainWindowHandle);

三、向指定的窗口输入数据

1.利用发送消息API(SendMessage)向窗口发送数据。

  1.  InputStr(myIntPtr, _GameID); //输入游戏ID
  2.  SendMessage(myIntPtr, WM_SYSKEYDOWN, 0X09, 0); //输入TAB(0x09)
  3.  SendMessage(myIntPtr, WM_SYSKEYUP, 0X09, 0);
  4.  InputStr(myIntPtr, _GamePass); //输入游戏密码
  5.  SendMessage(myIntPtr, WM_SYSKEYDOWN, 0X0D, 0); //输入ENTER(0x0d)
  6.  SendMessage(myIntPtr, WM_SYSKEYUP, 0X0D, 0);
  7.  /// <summary>
  8.  /// 发送一个字符串
  9.  /// </summary>
  10.  /// <param name="myIntPtr">窗口句柄</param>
  11.  /// <param name="Input">字符串</param>
  12.  public void InputStr(IntPtr myIntPtr, string Input)
  13.  {
  14.   byte[] ch = (ASCIIEncoding.ASCII.GetBytes(Input));
  15.   for (int i = 0; i < ch.Length; i++)
  16.   {
  17.    SendMessage(PW, WM_CHAR, ch, 0);
  18.   }
  19.  }

2.利用鼠标和键盘模拟向窗口发送数据。

  1.  SetWindowPos(PW, (IntPtr)(-1), 0, 0, 0, 0, 0x0040 | 0x0001); //设置窗口位置
  2.  SetCursorPos(476, 177); //设置鼠标位置
  3.  mouse_event(0x0002, 0, 0, 0, 0); //模拟鼠标按下操作
  4.  mouse_event(0x0004, 0, 0, 0, 0); //模拟鼠标放开操作
  5.  SendKeys.Send(_GameID); //模拟键盘输入游戏ID
  6.  SendKeys.Send("{TAB}"); //模拟键盘输入TAB
  7.  SendKeys.Send(_GamePass); //模拟键盘输入游戏密码
  8.  SendKeys.Send("{ENTER}"); //模拟键盘输入ENTER

另:上面还提到了keybd_event方法,用法和mouse_event方法类似,作用和SendKeys.Send一样。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/418830?site
推荐阅读
相关标签
  

闽ICP备14008679号