2016년 10월 12일 수요일

[C#강좌,닷넷강좌,실무중심교육◆탑크리에듀][C#강좌]ALT+F4(윈도우종료)키 막기,메시지필터링,IMessageFilter,Application.AddMessageFilter

[C#강좌]ALT+F4(윈도우종료)키 막기,메시지필터링,IMessageFilter,Application.AddMessageFilter

Application 클래스의 AddMessageFilter() 메소드는 응용프로그램에 메시지 필터를 등록하며 이 메소드의 매개변수로 IMessageFilter를 구현한 클래스의 인스턴스를 매개변수로 받으며 IMessageFilter의 PreFilterMessage를 구현해야 한다.

[예제]

using System.Windows.Forms;
using System;
namespace ConsoleApplication1
{   
    public class Program : Form
    {
        static void Main()
        {
            Application.AddMessageFilter(new AltF4Filter()); // Add a message filter
            Application.Run(new Program());
        }
    }

    public class AltF4Filter : IMessageFilter
    {
        public bool PreFilterMessage(ref Message m)
        {
            const int WM_SYSKEYDOWN = 0x0104;
            if (m.Msg == WM_SYSKEYDOWN)
            {
                bool alt = ((int)m.LParam & 0x20000000) != 0;
                if (alt && (m.WParam == new IntPtr((int)Keys.F4)))
                    Console.WriteLine("ALT+F4 Filtering됨.");
                    return true; // eat it!                
            }
            return false;
        }
    }
}

댓글 없음:

댓글 쓰기