Csharp/C Sharp/Event/Message

Материал из .Net Framework эксперт
Версия от 11:34, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Cast event sender to a control

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class InstantiateHelloWorld
{
     public static void Main()
     {
          Form form   = new Form();
          form.Text   = "Instantiate " + form.Text;
          form.Paint += new PaintEventHandler(MyPaintHandler);
   
          Application.Run(form);
     }
     static void MyPaintHandler(object objSender, PaintEventArgs pea)
     {
          Form     form = (Form)objSender;
          Graphics graphics = pea.Graphics;
   
          graphics.DrawString("Hello from InstantiateHelloWorld!", 
                          form.Font, Brushes.Black, 0, 100);
     }
}


Register Form window message filter

  using System;
  using System.Windows.Forms;
  using Microsoft.Win32;
  public class MyMessageFilter : IMessageFilter 
  {
    public bool PreFilterMessage(ref Message m) 
    {
      // Intercept the left mouse button down message.
      if (m.Msg == 513) 
      {
        Console.WriteLine("WM_LBUTTONDOWN is: " + m.Msg);
        return true;
      }
      return false;
    }
  }
  public class mainForm : System.Windows.Forms.Form
  {
    private MyMessageFilter msgFliter = new MyMessageFilter();
    public mainForm()
    {
      GetStats();
      Application.ApplicationExit += new EventHandler(Form_OnExit);
      Application.AddMessageFilter(msgFliter);    
    }
    [STAThread]
    static void Main() 
    {
      Application.Run(new mainForm());
    }
    private void GetStats()
    {
      Console.WriteLine(Application.rupanyName+ "  Company:");
      Console.WriteLine(Application.ProductName+ " App Name:");
      Console.WriteLine(Application.StartupPath);
    }
    // Event handlers.
    private void Form_OnExit(object sender, EventArgs evArgs) 
    {
      Console.WriteLine("Exit", "This app is dead...");
      Application.RemoveMessageFilter(msgFliter);
    }
  }