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


    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. }
  • 相关阅读:
    如何使用Dev C++调试(debug)c程序
    C内存对齐详解
    epics commands
    #include <errno.h>
    linux中tail命令
    source env then start eclipse
    c++ constructor with para
    如何访问虚拟机中的架设的Web服务器(解决方法)
    dcss_gui_handler
    atlsoap.h”: No such file or directory
  • 原文地址:https://www.cnblogs.com/VictorBlog/p/5193110.html
Copyright © 2020-2023  润新知