Csharp/C Sharp/Event/Message
Cast event sender to a control
<source lang="csharp"> 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); }
}
</source>
Register Form window message filter
<source lang="csharp">
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); } }
</source>