捕获键盘和鼠标的消息机制

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace 消息机制获取
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void Form1_Load(object sender, EventArgs e)
  18. {
  19. }
  20. //int t;
  21. //internal class MyMessager : IMessageFilter
  22. //{
  23. // public bool PreFilterMessage(ref Message m)
  24. // {
  25. // //如果检测到有鼠标或则键盘的消息,则使计数为0.....
  26. // if (m.Msg == 0x0200 || m.Msg == 0x0201 || m.Msg == 0x0204 || m.Msg == 0x0207)
  27. // {
  28. // }
  29. // return false;
  30. // }
  31. //}

 

  1. // Constant value was found in the "windows.h" header file.
  2. private const int WM_ACTIVATEAPP = 0x001C;
  3. private bool appActive = true;
  4. String strMsg;
  5. [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
  6. protected override void WndProc(ref Message m)
  7. {
  8. strMsg = string.Format("Message:Msg_{0} HWnd_{1} LParam_{2} Result_{3} WParam_{4}", m.Msg, m.HWnd, m.LParam, m.Result, m.WParam);
  9. // Listen for operating system messages.
  10. switch (m.Msg)
  11. {
  12. // The WM_ACTIVATEAPP message occurs when the application
  13. // becomes the active application or becomes inactive.
  14. case WM_ACTIVATEAPP:
  15. // The WParam value identifies what is occurring.
  16. appActive = (((int)m.WParam != 0));
  17. // Invalidate to get new text painted.
  18. this.Invalidate();
  19. break;
  20. }
  21. base.WndProc(ref m);
  22. }
  23. //或重写ProcessCmdKey的方法
  24. protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
  25. {
  26. int WM_KEYDOWN = 256;
  27. int WM_SYSKEYDOWN = 260;
  28. label1.Text = string.Format("Message:{0} keys:{1}",msg.Msg,keyData.ToString());
  29. if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
  30. {
  31. switch (keyData)
  32. {
  33. case Keys.Escape:
  34. //this.Close();//esc关闭窗体
  35. break;
  36. }
  37. }
  38. return false;
  39. }
  40. private void timer1_Tick(object sender, EventArgs e)
  41. {
  42. this.Invoke((EventHandler)delegate
  43. {
  44. label2.Text = strMsg;
  45. });
  46. }

 

  1. }
  2. }
posted on 2016-02-16 16:26 影子博客 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/VictorBlog/p/5193110.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值