嘿,我有关键事件处理程序的一些问题。这是来源:C#检测所有窗口中的关键事件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public void Form1_KeyPessed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F2)
{
DrawSquare();
}
}
public void DrawSquare()
{
int line = Convert.ToInt16(textBox1.Text);
int time = Convert.ToInt16(textBox2.Text);
int x = MousePosition.X;
int y = MousePosition.Y;
Cursor.Position = new Point(x - line/2, y - line/2);
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
Thread.Sleep(time);
Cursor.Position = new Point(x - line/2, y + line/2);
Thread.Sleep(time);
Cursor.Position = new Point(x - line/2, y + line/2);
Thread.Sleep(time);
Cursor.Position = new Point(x + line/2, y - line/2);
Thread.Sleep(time);
Cursor.Position = new Point(x - line/2, y - line/2);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
Thread.Sleep(time);
Cursor.Position = new Point(x, y);
}
}
}
现在它只绘制广场,当我按F2在窗体中,但我希望它可以在所有窗口上工作。 我还需要什么? (这是有点完美形状的自动抽屉)
有关[mouse_event'文档](http://msdn.microsoft.com/zh-cn/library/ms646260.aspx)的注意事项。它说:**这个功能已被取代。请使用['SendInput'](http://msdn.microsoft.com/zh-cn/library/ms646310.aspx)** – 2011-03-27 16:05:01
为什么要删除我添加的标签?更多标签有助于对您的问题进行分类,以便其他人可以找到它。它还帮助回答问题的人找到它。他们通常会遵循他们熟悉的特定标签。 – 2011-03-28 04:19:50