WndProc函數(轉)

來源:互聯網
上載者:User

標籤:style   blog   color   io   ar   for   art   div   sp   

WndProc函數作用:

主要用在攔截並處理系統訊息和自訂訊息

比如:
windows程式會產生很多訊息,比如你單擊滑鼠,移動視窗都會產生訊息。這個函數就是預設的訊息處理函數。你可以重載這個函數來制定自己的訊息處理流程.

在CS中,可以重寫WndProc函數,來捕捉所有發生有視窗訊息。

這樣,我們就可以"篡改"傳入的訊息,而人為的讓視窗改變行為。

代碼執行個體:

 public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private Demo demo= null;        private void Form1_Load(object sender, EventArgs e)        {            demo= new Demo(this.Handle.ToInt32());        }        private void button1_Click(object sender, EventArgs e)        {            demo.Test();        }        protected override void WndProc(ref Message m)        {            if (m.Msg == Demo.MY_MSG_BEGIN)            {                MessageBox.Show("類Demo for迴圈開始.");            }            else if (m.Msg == Demo.MY_MSG_END)            {                MessageBox.Show("類Demo for迴圈結束.");            }            base.WndProc(ref m);        }    }
   public class Demo    {        private int m_hWnd = 0;        public Demo(int hWnd)        {            m_hWnd = hWnd;        }        private const int WM_USER = 0x0400;        public static int MY_MSG_BEGIN = WM_USER + 100;        public static int MY_MSG_END = WM_USER + 101;        [DllImport("User32.DLL")]        public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);        public void Test()        {            SendMessage(m_hWnd, MY_MSG_BEGIN, 0, 0);            for (int i = 0; i < 100000; i++)            {                Application.DoEvents();            }            SendMessage(m_hWnd, MY_MSG_END, 0, 0);        }    }

 

WndProc函數(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.