2009年6月28日 星期日

在 WinForm 鎖住鍵盤,不能輸入任何訊息

在Form中鎖住鍵盤,僅能透過滑鼠或其他輸入設備來與電腦進行互動

1. 新增一 Windows 應用程式專案

2. 修改 Program.cs
把 main 改為
static void Main()
{
RButtonMessageFilter filter = new RButtonMessageFilter();
Application.AddMessageFilter(filter);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

Application.RemoveMessageFilter(filter);
}
3. 新增一 Class##CONTINUE##
using System.Windows.Forms;
public class RButtonMessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
const int WM_KEYDOWN = 0x100;

switch (m.Msg)
{
case WM_KEYDOWN:
{
return true;
}
default:
{
return false;
}
}
}
}

沒有留言:

張貼留言