攔截系統資訊取的滑鼠或鍵盤操作
本來以為只能透過 WndProc,原來還有其他做法,學起來,.....
{
public partial class Form1 : Form
{
MessageFilter mf = new MessageFilter();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Application.AddMessageFilter(mf);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.RemoveMessageFilter(mf);
}
}
public class MessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
switch (m.Msg)
{
case 513:
MessageBox.Show("LEF_BUTTON!", "SYS");
return true;
case 516:
MessageBox.Show("RIGHT_BUTTON!", "SYS");
return false;
default:
return true;
}
}
}
}
namespace mouse_area
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
Cursor.Clip = new Rectangle(this.Location,this.Size);
}
private void button2_Click(object sender, EventArgs e)
{
Screen[] screens = Screen.AllScreens;
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Clip = screens[0].Bounds;
}
private void Form1_Load(object sender, EventArgs e)
{
this.AllowDrop = true;
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
for(int i=0;i<files.Length;i++)
{
listBox1.Items.Add(files[i]);
}
}
}
}
}