我发现framework对系统底层的东西封装的太严实了,但是如果使用消息处理的话可以提高程序的灵活性,应用的好还可以提高性能,所以使用必要的底层技术还有必要的。
消息处理在以前的开发工具中应用是非常广泛的,下面我就写举了一个简单的处理消息的例子,基本上说明了C#中的消息处理方法:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace WinMsg
- {
- public partial class Form1 : Form
- {
- const int WM_Lbutton = 0x201; //定义了鼠标的左键点击消息
- public Form1()
- {
- InitializeComponent();
- }
-
- [DllImport("User32.dll", EntryPoint = "SendMessage")]
- private static extern int SendMessage(
- int hWnd, // handle to destination window
- int Msg, // message
- int wParam, // first message parameter
- int lParam // second message parameter
- );
- protected override void DefWndProc(ref Message m)
- {
-
- switch (m.Msg)
- {
- case WM_Lbutton:
- MessageBox.Show("点左键");
- return;
- default:
- base.DefWndProc(ref m);
- return;
- }
- }
- }
- }
复制代码