Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 模拟按键C_C# - Fatal编程技术网

C# 模拟按键C

C# 模拟按键C,c#,C#,我想在我的C程序中模拟F5按键。当IE打开时,我希望能够自动刷新我的网站 如何才能做到这一点?您可以使用Win32 API FindWindow或查找打开的浏览器的窗口句柄,然后使用调用。通常,最简单的方法是将窗口标题传递给FindWindowEx,让它为您找到相关的窗口句柄 如果您自己通过process对象启动浏览器进程,则可以使用process.MainWindowHandle而不是调用FindWindowEx 当您想开始使用其他窗口时,Spy++是一个非常有用的工具。它基本上允许您了解另一

我想在我的C程序中模拟F5按键。当IE打开时,我希望能够自动刷新我的网站

如何才能做到这一点?

您可以使用Win32 API FindWindow或查找打开的浏览器的窗口句柄,然后使用调用。通常,最简单的方法是将窗口标题传递给FindWindowEx,让它为您找到相关的窗口句柄

如果您自己通过process对象启动浏览器进程,则可以使用process.MainWindowHandle而不是调用FindWindowEx

当您想开始使用其他窗口时,Spy++是一个非常有用的工具。它基本上允许您了解另一个程序的UI元素层次结构。您还可以监视进入正在监视的窗口的所有消息。我有更多的信息

F5击键具有以下虚拟键代码:

const int VK_F5 = 0x74;
C中FindWindowEx的p/invoke签名为:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
您可以p/invoke将Win32 API SendMessage带入,如下所示:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
总而言之,在类中的某个地方有了上面的代码之后,您可以直接从C代码调用FindWindowEx。FindWindowEx将返回一个窗口句柄。一旦有了窗口句柄,就可以向窗口发送任何击键,或者调用窗口句柄上的许多其他Win32 API调用。甚至可以通过调用FindWindowEx来查找子窗口。例如,您甚至可以选择浏览器的编辑控件,然后更改其文本


如果所有其他操作都出错,并且您认为您正在向窗口发送正确的键,那么当您手动将焦点设置为浏览器并手动按F5时,可以使用spy++查看向窗口发送的消息。

模拟按F5键的另一种替代方法是在window Forms应用程序中托管控件。您可以使用该方法加载网页,然后使用标准,在计时器的每个刻度上,您只需重新导航到将重新加载网页的url。

以下是一个示例

static class Program
{
    [DllImport("user32.dll")]
    public static extern int SetForegroundWindow(IntPtr hWnd);

    [STAThread]
    static void Main()
    {
        while(true)
        {
            Process [] processes = Process.GetProcessesByName("iexplore");

            foreach(Process proc in processes)
            {
                SetForegroundWindow(proc.MainWindowHandle);
                SendKeys.SendWait("{F5}");
            }

            Thread.Sleep(5000);
        }
    }
}
一个更好的。。。不那么可怕

static class Program
{
    const UInt32 WM_KEYDOWN = 0x0100;
    const int VK_F5 = 0x74;

    [DllImport("user32.dll")]
    static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);

    [STAThread]
    static void Main()
    {
        while(true)
        {
            Process [] processes = Process.GetProcessesByName("iexplore");

            foreach(Process proc in processes)
                PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_F5, 0);

            Thread.Sleep(5000);
        }
    }
}

当您试图让页面回发时,不必强制按F5键,您可以基于JS事件调用回发,即使是mousemove或timer_tick,如果您希望它一直启动的话。使用处的代码作为参考。

向任何窗口发送模拟击键的最简单方法是使用.NET Framework的SendKeys.send方法

签出这篇非常直观的MSDN文章

特别是对于您的情况,如果您的浏览器窗口处于焦点位置,发送F5将只涉及以下代码行:

SendKeys.Send("{F5}");

简单的一个,在主

[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
主/方法内的代码:

string className = "IEFrame";
string windowName = "New Tab - Windows Internet Explorer";
IntPtr IE = FindWindow(className, windowName);
if (IE == IntPtr.Zero)
{
   return;
}
SetForegroundWindow(IE);
InputSimulator.SimulateKeyPress(VirtualKeyCode.F5);
注:

添加InputSimulator作为参考。下载

要查找类和窗口名,请使用WinSpy++。下载


简单、简短且无需窗口聚焦:

还有一个有用的虚拟密钥代码列表


使用鼠标事件或按键事件。他们说不要再使用它们了,但你根本不需要找到窗户

using System;
using System.Runtime.InteropServices;

public class SimulatePCControl
{

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void keybd_event(uint bVk, uint bScan, uint dwFlags, uint dwExtraInfo);

private const int VK_LEFT = 0x25;

public static void LeftArrow()
{
    keybd_event(VK_LEFT, 0, 0, 0);
}

}
此处列出了此项的虚拟密钥代码:

同样适用于鼠标:

using System.Runtime.InteropServices;
using UnityEngine;

public class SimulateMouseClick
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;

public static void Click()
{
    //Call the imported function with the cursor's current position
    uint X = (uint)0;
    uint Y = (uint)0;
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    Debug.LogError("SIMULATED A MOUSE CLICK JUST NOW...");
}

//...other code needed for the application
}

这是一个web应用程序还是桌面应用程序?在这种情况下,我认为Bondy先生已经一针见血了。请你更详细一些,我真的不知道如何使用Win32 API。指向SO线程的链接非常完美。但也可能这段代码有帮助-@Dani:I添加了更多信息,但您可以使用lang:c搜索谷歌代码,以获得更详细的示例。这尤其是更好的帮助了我,+1。请参阅我的答案,以查看一个更复杂的示例,该示例还涉及修改键。SendKeys未定义并引用System.Windows.Forms。
using System.Runtime.InteropServices;
using UnityEngine;

public class SimulateMouseClick
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;

public static void Click()
{
    //Call the imported function with the cursor's current position
    uint X = (uint)0;
    uint Y = (uint)0;
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    Debug.LogError("SIMULATED A MOUSE CLICK JUST NOW...");
}

//...other code needed for the application
}