- 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;
- namespace 消息机制获取
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- //int t;
- //internal class MyMessager : IMessageFilter
- //{
- // public bool PreFilterMessage(ref Message m)
- // {
- // //如果检测到有鼠标或则键盘的消息,则使计数为0.....
- // if (m.Msg == 0x0200 || m.Msg == 0x0201 || m.Msg == 0x0204 || m.Msg == 0x0207)
- // {
- // }
- // return false;
- // }
- //}
- // Constant value was found in the "windows.h" header file.
- private const int WM_ACTIVATEAPP = 0x001C;
- private bool appActive = true;
- String strMsg;
- [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
- protected override void WndProc(ref Message m)
- {
- strMsg = string.Format("Message:Msg_{0} HWnd_{1} LParam_{2} Result_{3} WParam_{4}", m.Msg, m.HWnd, m.LParam, m.Result, m.WParam);
- // Listen for operating system messages.
- switch (m.Msg)
- {
- // The WM_ACTIVATEAPP message occurs when the application
- // becomes the active application or becomes inactive.
- case WM_ACTIVATEAPP:
- // The WParam value identifies what is occurring.
- appActive = (((int)m.WParam != 0));
- // Invalidate to get new text painted.
- this.Invalidate();
- break;
- }
- base.WndProc(ref m);
- }
- //或重写ProcessCmdKey的方法
- protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
- {
- int WM_KEYDOWN = 256;
- int WM_SYSKEYDOWN = 260;
- label1.Text = string.Format("Message:{0} keys:{1}",msg.Msg,keyData.ToString());
- if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
- {
- switch (keyData)
- {
- case Keys.Escape:
- //this.Close();//esc关闭窗体
- break;
- }
- }
- return false;
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- this.Invoke((EventHandler)delegate
- {
- label2.Text = strMsg;
- });
- }
- }
- }
posted on 2016-02-16 16:26 阅读( ...) 评论( ...)