![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
用C#winform开发一个全自动操作的一个小程序。
里面架构非常简单,就只有一个webBrowser控件和五六个按钮。这几个按钮是实现操作网页用的。比如向网页的文本框输入,点击,取值等用处。现在我想如何实现,这些按钮在运行...
里面架构非常简单,就只有一个webBrowser控件和五六个按钮。这几个按钮是实现操作网页用的。比如向网页的文本框输入,点击,取值等用处。现在我想如何实现,这些按钮在运行程序时就自动运行下去,不再用人工点击这几个按钮,另外,当网站弹出对话框时我怎么取出网页的对话框并让程序自动点击“是”还是“否”。
展开
3个回答
展开全部
其实你需要的是一个模拟按键和鼠标的操作
为何不直接讲呢?
模拟键盘的给你
//定义
{
[StructLayout(LayoutKind.Sequential)]
internal struct MOUSEINPUT {
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct KEYBDINPUT {
public short wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Explicit)]
internal struct Input {
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
internal struct HARDWAREINPUT {
public int uMsg;
public short wParamL;
public short wParamH;
}
internal class INPUT {
public const int MOUSE = 0;
public const int KEYBOARD = 1;
public const int HARDWARE = 2;
}
internal static class NativeMethods {
[DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
internal static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("User32.dll", EntryPoint = "SendInput", CharSet = CharSet.Auto)]
internal static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, Int32 cbSize);
[DllImport("Kernel32.dll", EntryPoint = "GetTickCount", CharSet = CharSet.Auto)]
internal static extern int GetTickCount();
[DllImport("User32.dll", EntryPoint = "GetKeyState", CharSet = CharSet.Auto)]
internal static extern short GetKeyState(int nVirtKey);
[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
}
}
//模拟过程
private void SendKeyDown(short key)
{
Input[] input = new Input[1];
input[0].type = INPUT.KEYBOARD;
input[0].ki.wVk = key;
input[0].ki.time = NativeMethods.GetTickCount();
if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
private void SendKeyUp(short key)
{
Input[] input = new Input[1];
input[0].type = INPUT.KEYBOARD;
input[0].ki.wVk = key;
input[0].ki.dwFlags = KeyboardConstaint.KEYEVENTF_KEYUP;
input[0].ki.time = NativeMethods.GetTickCount();
if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
//调用方法
short key = 65;
SendKeyDown(key);
SendKeyUp(key);
为何不直接讲呢?
模拟键盘的给你
//定义
{
[StructLayout(LayoutKind.Sequential)]
internal struct MOUSEINPUT {
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct KEYBDINPUT {
public short wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Explicit)]
internal struct Input {
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
internal struct HARDWAREINPUT {
public int uMsg;
public short wParamL;
public short wParamH;
}
internal class INPUT {
public const int MOUSE = 0;
public const int KEYBOARD = 1;
public const int HARDWARE = 2;
}
internal static class NativeMethods {
[DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
internal static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("User32.dll", EntryPoint = "SendInput", CharSet = CharSet.Auto)]
internal static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, Int32 cbSize);
[DllImport("Kernel32.dll", EntryPoint = "GetTickCount", CharSet = CharSet.Auto)]
internal static extern int GetTickCount();
[DllImport("User32.dll", EntryPoint = "GetKeyState", CharSet = CharSet.Auto)]
internal static extern short GetKeyState(int nVirtKey);
[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
}
}
//模拟过程
private void SendKeyDown(short key)
{
Input[] input = new Input[1];
input[0].type = INPUT.KEYBOARD;
input[0].ki.wVk = key;
input[0].ki.time = NativeMethods.GetTickCount();
if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
private void SendKeyUp(short key)
{
Input[] input = new Input[1];
input[0].type = INPUT.KEYBOARD;
input[0].ki.wVk = key;
input[0].ki.dwFlags = KeyboardConstaint.KEYEVENTF_KEYUP;
input[0].ki.time = NativeMethods.GetTickCount();
if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
//调用方法
short key = 65;
SendKeyDown(key);
SendKeyUp(key);
![](https://ecmc.bdimg.com/public03/b4cb859ca634443212c22993b0c87088.png)
2024-08-21 广告
```c#include int main() { int a, b; printf("请输入两个整数: "); scanf("%d %d", &a, &b); int sum = a + b; printf(...
点击进入详情页
本回答由MarsCode提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询