Application.AddMessageFilter
using System;
using System.Threading;
using System.Windows.Forms;
public class BlockLeftMouseButtonMessageFilter : IMessageFilter
{
const int WM_LBUTTONDOWN = 0x201;
public bool PreFilterMessage(ref Message m) {
if(m.Msg == WM_LBUTTONDOWN)
{
Exception LeftButtonDownException;
LeftButtonDownException = new Exception("The left mouse button was pressed.");
Application.OnThreadException(LeftButtonDownException);
return true;
}
return false;
}
}
public class ApplicationEventHandlerClass
{
public void OnThreadException(object sender, ThreadExceptionEventArgs e)
{
Exception LeftButtonDownException;
LeftButtonDownException = e.Exception;
Console.WriteLine(LeftButtonDownException.Message);
}
}
public class MainForm : Form
{
public static void Main()
{
ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
MainForm MyForm = new MainForm();
BlockLeftMouseButtonMessageFilter MsgFilter = new BlockLeftMouseButtonMessageFilter();
Application.AddMessageFilter(MsgFilter);
Application.ThreadException += new ThreadExceptionEventHandler(AppEvents.OnThreadException);
Application.Run(MyForm);
}
public MainForm()
{
Text = "Application Exception Test";
}
}
Application Exit event
using System;
using System.Windows.Forms;
public class MainClass : System.Windows.Forms.Form
{
public MainClass()
{
Application.ApplicationExit += new EventHandler(Form_OnExit);
}
[STAThread]
static void Main()
{
Application.Run(new MainClass());
}
private void Form_OnExit(object sender, EventArgs evArgs)
{
Console.WriteLine("Exit");
}
}
Application Startup Path
using System;
using System.Collections.Generic;
using System.Windows.Forms;
static class MainClass
{
[STAThread]
static void Main()
{
Console.WriteLine( Application.StartupPath);
}
}
C:\Java_Dev\WEB\dev\CSharp
Get CompanyName, ProductName and StartupPath from Application
using System;
using System.Windows.Forms;
public class MainClass : System.Windows.Forms.Form
{
public MainClass()
{
Console.WriteLine("Company: " + Application.rupanyName);
Console.WriteLine("App Name: " + Application.ProductName);
Console.WriteLine("I live here: "+ Application.StartupPath);
}
[STAThread]
static void Main()
{
Application.Run(new MainClass());
}
}
Company: MainClass
App Name: MainClass
I live here: C:\Java_Dev\WEB\dev\CSharp
Working with Thread Exceptions
using System;
using System.Threading;
using System.Windows.Forms;
public class BlockLeftMouseButtonMessageFilter : IMessageFilter
{
const int WM_LBUTTONDOWN = 0x201;
public bool PreFilterMessage(ref Message m) {
if(m.Msg == WM_LBUTTONDOWN)
{
Exception LeftButtonDownException;
LeftButtonDownException = new Exception("The left mouse button was pressed.");
Application.OnThreadException(LeftButtonDownException);
return true;
}
return false;
}
}
public class ApplicationEventHandlerClass
{
public void OnThreadException(object sender, ThreadExceptionEventArgs e)
{
Exception LeftButtonDownException;
LeftButtonDownException = e.Exception;
Console.WriteLine(LeftButtonDownException.Message);
}
}
public class MainForm : Form
{
public static void Main()
{
ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
MainForm MyForm = new MainForm();
BlockLeftMouseButtonMessageFilter MsgFilter = new BlockLeftMouseButtonMessageFilter();
Application.AddMessageFilter(MsgFilter);
Application.ThreadException += new ThreadExceptionEventHandler(AppEvents.OnThreadException);
Application.Run(MyForm);
}
public MainForm()
{
Text = "Application Exception Test";
}
}