Csharp/C Sharp/Development Class/Application Event
Содержание
- 1 Application.Run
- 2 Handling Application Events: On Application Exit
- 3 Handling Application Events: On Idle
- 4 Handling Application Events: OnIdle, OnThreadException, OnThreadExit
- 5 Handling Application Events: On Thread Exit
- 6 Handling Application Events: Thread Exception
- 7 Installing a Message Filter
- 8 Removing an Installed Message Filter
Application.Run
<source lang="csharp">
using System.Windows.Forms;
class RunFormBetter {
public static void Main() { Form form = new Form(); form.Text = "My Very Own Form"; Application.Run(form); }
}
</source>
Handling Application Events: On Application Exit
<source lang="csharp"> using System; using System.Threading; using System.Reflection; using System.Windows.Forms;
public class HelloWorldForm : Form {
public HelloWorldForm() { Text = "Hello, WindowsForms!"; }
}
public class ApplicationEventHandlerClass {
public void OnApplicationExit(object sender, EventArgs e) { try { Console.WriteLine("The application is shutting down."); } catch(NotSupportedException) { } }
}
public class MainClass {
public static void Main() { HelloWorldForm FormObject = new HelloWorldForm(); ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass(); Application.ApplicationExit += new EventHandler(AppEvents.OnApplicationExit); Application.Run(FormObject); }
}
</source>
Handling Application Events: On Idle
<source lang="csharp"> using System; using System.Threading; using System.Reflection; using System.Windows.Forms;
public class HelloWorldForm : Form {
public HelloWorldForm() { Text = "Hello, WindowsForms!"; }
}
public class ApplicationEventHandlerClass {
public void OnIdle(object sender, EventArgs e) { Console.WriteLine("The application is idle."); }
}
public class MainClass {
public static void Main() { HelloWorldForm FormObject = new HelloWorldForm(); ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass(); Application.Idle += new EventHandler(AppEvents.OnIdle); Application.Run(FormObject); }
}
</source>
Handling Application Events: OnIdle, OnThreadException, OnThreadExit
<source lang="csharp">
using System; using System.Threading; using System.Reflection; using System.Windows.Forms; public class HelloWorldForm : Form {
public HelloWorldForm() { } public static void OnApplicationExit(object sender, EventArgs e) { try { Console.WriteLine("shutting down."); } catch (NotSupportedException) { } } public static void OnIdle(object sender, EventArgs e) { Console.WriteLine("idle."); } public static void OnThreadException(object sender, ThreadExceptionEventArgs e) { Console.WriteLine("caught!"); } public static void OnThreadExit(object sender, EventArgs e) { Console.WriteLine("thread is shutting down."); } public static void Main() { HelloWorldForm FormObject = new HelloWorldForm(); Application.ApplicationExit += new EventHandler(OnApplicationExit); Application.Idle += new EventHandler(OnIdle); Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException); Application.ThreadExit += new EventHandler(OnThreadExit); Application.Run(FormObject); }
}
</source>
Handling Application Events: On Thread Exit
<source lang="csharp"> using System; using System.Threading; using System.Reflection; using System.Windows.Forms;
public class HelloWorldForm : Form {
public HelloWorldForm() { Text = "Hello, WindowsForms!"; }
}
public class ApplicationEventHandlerClass {
public void OnThreadExit(object sender, EventArgs e) { Console.WriteLine("The application"s main thread is shutting down."); }
}
public class MainClass {
public static void Main() { HelloWorldForm FormObject = new HelloWorldForm(); ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass(); Application.ThreadExit += new EventHandler(AppEvents.OnThreadExit); Application.Run(FormObject); }
}
</source>
Handling Application Events: Thread Exception
<source lang="csharp"> using System; using System.Threading; using System.Reflection; using System.Windows.Forms;
public class HelloWorldForm : Form {
public HelloWorldForm() { Text = "Hello, WindowsForms!"; }
}
public class ApplicationEventHandlerClass {
public void OnThreadException(object sender, ThreadExceptionEventArgs e) { Console.WriteLine("an exception thrown from an application thread was caught!"); }
}
public class MainClass {
public static void Main() { HelloWorldForm FormObject = new HelloWorldForm(); ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass(); Application.ThreadException += new ThreadExceptionEventHandler(AppEvents.OnThreadException); Application.Run(FormObject); }
}
</source>
Installing a Message Filter
<source lang="csharp">
using System; using System.Windows.Forms;
public class BlockLeftMouseButtonMessageFilter : IMessageFilter {
const int WM_LBUTTONDOWN = 0x201; const int WM_LBUTTONUP = 0x202; public bool PreFilterMessage(ref Message m) { if(m.Msg == WM_LBUTTONDOWN) { Console.WriteLine("The left mouse button is down."); return true; } if(m.Msg == WM_LBUTTONUP) { Console.WriteLine("The left mouse button is up."); return true; } return false; }
}
public class MainForm : Form {
public static void Main() { MainForm MyForm = new MainForm(); BlockLeftMouseButtonMessageFilter MsgFilter = new BlockLeftMouseButtonMessageFilter(); Application.AddMessageFilter(MsgFilter); Application.Run(MyForm); } public MainForm() { Text = "Message Filter Test"; }
}
</source>
Removing an Installed Message Filter
<source lang="csharp">
using System; using System.Windows.Forms;
public class BlockLeftMouseButtonMessageFilter : IMessageFilter {
const int WM_LBUTTONDOWN = 0x201; public bool PreFilterMessage(ref Message m){ if(m.Msg == WM_LBUTTONDOWN) { Console.WriteLine("The left mouse button is down."); Application.RemoveMessageFilter(this); return true; } return false; }
}
public class MainForm : Form {
public static void Main() { MainForm MyForm = new MainForm(); BlockLeftMouseButtonMessageFilter MsgFilter = new BlockLeftMouseButtonMessageFilter(); Application.AddMessageFilter(MsgFilter); Application.Run(MyForm); } public MainForm() { Text = "Message Filter Removal Test"; }
}
</source>