C#模拟键盘按键和鼠标移动还有输入文字(后台)

金鑫0425 2011-02-12 10:09:41
后台的可能需要 获取句柄把?
怎么获取呢?(需要详细代码):
键盘按键:比如“像当前窗口里面发送F1”;
鼠标移动 100,100 坐标处;
本人是初学者
...全文
1121 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
QingNiaoT07 2011-05-22
  • 打赏
  • 举报
回复
a87738382
请问你是北大青鸟哪个校区的
fanbingyuan 2011-02-12
  • 打赏
  • 举报
回复
金鑫0425 2011-02-12
  • 打赏
  • 举报
回复
那么 键盘按键怎么弄?
金鑫0425 2011-02-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bdmh 的回复:]

比如鼠标模拟
C# code

uint MOUSEEVENTF_MOVE = 0x0001;
uint MOUSEEVENTF_LEFTDOWN = 0x0002;
uint MOUSEEVENTF_LEFTUP = 0x0003;

[DllImport("user32.dll")]
private stat……
[/Quote]

System.Runtime.InteropServices.DllImport 改成这句就对了
金鑫0425 2011-02-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bdmh 的回复:]

比如鼠标模拟
C# code

uint MOUSEEVENTF_MOVE = 0x0001;
uint MOUSEEVENTF_LEFTDOWN = 0x0002;
uint MOUSEEVENTF_LEFTUP = 0x0003;

[DllImport("user32.dll")]
private stat……
[/Quote]


这个怎么使用啊? [DllImport("user32.dll")] 这句话为什么报错啊?
wangwenbincom 2011-02-12
  • 打赏
  • 举报
回复
这个好用
bdmh 2011-02-12
  • 打赏
  • 举报
回复
比如鼠标模拟

uint MOUSEEVENTF_MOVE = 0x0001;
uint MOUSEEVENTF_LEFTDOWN = 0x0002;
uint MOUSEEVENTF_LEFTUP = 0x0003;

[DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags,uint dx,uint dy,uint dwData,uint dwExtraInfo);

private void button1_Click(object sender, EventArgs e)
{
//在坐标100,100处模拟左键按下
mouse_event(MOUSEEVENTF_LEFTDOWN, 100, 100, 0, 0);
//模拟移动,移动到坐标200,200处
mouse_event(MOUSEEVENTF_MOVE, 200, 200, 0, 0);
//模拟左键抬起,完成整个的模拟过程
mouse_event(MOUSEEVENTF_LEFTUP, 200, 200, 0, 0);
}

FindWindow是通过窗体标题找到窗体句柄
金鑫0425 2011-02-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
用api,获取句柄用FindWindow,模拟鼠标用mouse_event,模拟键盘用keybd_event
[/Quote]

能不能给我详细点的代码? 麻烦您了 我本新手 北大青鸟C#课程 才学到第四章.....
bdmh 2011-02-12
  • 打赏
  • 举报
回复
用api,获取句柄用FindWindow,模拟鼠标用mouse_event,模拟键盘用keybd_event
elittle 2011-02-12
  • 打赏
  • 举报
回复
楼上很给力
wuyq11 2011-02-12
  • 打赏
  • 举报
回复
根据SPY++中取得的类名(或标题名)及层次关系
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

private void button1_Click(object sender, EventArgs e)
{
IntPtr hWnd1 = FindWindow(null, "主窗口标题");
SetForegroundWindow(hWnd1);
IntPtr hWnd2 = FindWindowEx(hWnd1, IntPtr.Zero, null, "控件文本");
IntPtr hWnd3 = FindWindowEx(hWnd1, IntPtr.Zero, "控件类名", null);
System.Text.StringBuilder str = new System.Text.StringBuilder(255);
SendMessage(hWnd2 , 0xD, str.Capacity, str);
MessageBox.Show(str.ToString());
}
SendKeys.Send
鼠标
[DllImport("user32.dll")]
private static extern void mouse_event
(
UInt32 dwFlags, // motion and click options
UInt32 dx, // horizontal position or change
UInt32 dy, // vertical position or change
UInt32 dwData, // wheel movement
IntPtr dwExtraInfo // application-defined information
);

private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;



public void Click()
{
m_Browser.Visible = true;

Point pp = new Point(CurrentTask.X, CurrentTask.Y);

Point screenPoint = m_MainForm.PointToScreen(pp);
screenPoint.X += m_Browser.Left;
screenPoint.Y += m_Browser.Top;

Cursor.Position = screenPoint;

mouse_event(MOUSEEVENTF_LEFTDOWN, (UInt32)pp.X, (UInt32)pp.Y, 0, new IntPtr());

for (int i = 0; i < 19; i++)
{
Thread.Sleep(20);
Application.DoEvents();
}

mouse_event(MOUSEEVENTF_LEFTUP, (UInt32)pp.X, (UInt32)pp.Y, 0, new IntPtr());

m_Browser.Visible = false;
m_State = ClickerState.Clicked;

m_CurrentTask = null;

Thread.Sleep(100);

//The element the browser clicked is not a link
if(m_State != ClickerState.Navigating)
{
m_State = ClickerState.WaitingTask;
Navigate(m_Manager.GetTask());
}
}

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧