Csharp/C Sharp by API/System.Windows.Forms/Application

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

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.ApplicationExit

 
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.CompanyName

 
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());
  }
}


Application.Idle

 

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);
    }
}


Application.ProductName

 
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());
  }
}


Application.RemoveMessageFilter

 

using System;
using System.Windows.Forms;
public class mainForm : System.Windows.Forms.Form
{
  private MyMessageFilter msgFliter = new MyMessageFilter();
  public mainForm()
  {
    Application.ApplicationExit += new EventHandler(Form_OnExit);
    Application.AddMessageFilter(msgFliter);    
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new mainForm());
  }
  private void Form_OnExit(object sender, EventArgs evArgs) 
  {
    Application.RemoveMessageFilter(msgFliter);
  }
}
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;
  }
}


Application.Run

 
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.StartupPath

 
using System;
using System.Collections.Generic;
using System.Windows.Forms;
static class MainClass
{
    [STAThread]
    static void Main()
    {
        Console.WriteLine( Application.StartupPath);
  
    }
}


Application.ThreadException

 
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.ThreadExit

 
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);
    }
}