Mega Code Archive

 
Categories / C# Tutorial / WPF
 

Register and Handle Window event in code

/* # build.rsp # /target:winexe /out:SimpleWPFAppRevisited.exe /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.dll" *.cs */ using System; using System.Windows; using System.Windows.Controls; namespace SimpleWPFAppRevisited {   class MyWPFApp : Application   {     [STAThread]     static void Main()     {       MyWPFApp app = new MyWPFApp();       app.Startup += AppStartUp;       app.Run();     }     static void AppStartUp(object sender, StartupEventArgs e)     {       Application.Current.Properties["A"] = false;     foreach(string arg in e.Args)     {        Console.WriteLine(arg);     }       MainWindow wnd = new MainWindow("My better WPF App!", 200, 300);       MinimizeAllWindows();     }          static void MinimizeAllWindows()     {       foreach (Window wnd in Application.Current.Windows)       {         wnd.WindowState = WindowState.Minimized;       }     }   }      class MainWindow : Window   {     Button btnExitApp = new Button();          protected void MainWindow_Activated(object sender, EventArgs e)     {       Console.WriteLine("Activate Event");     }     protected void MainWindow_Deactivated(object sender, EventArgs e)     {       Console.WriteLine("Deactivated Event");     }     protected void MainWindow_Loaded(object sender, RoutedEventArgs e)     {       Console.WriteLine("Loaded Event Fired");     }     protected void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)     {       Console.WriteLine("Closing Event Fired");              string msg = "Close?";       MessageBoxResult result = MessageBox.Show(msg, "My App", MessageBoxButton.YesNo, MessageBoxImage.Warning);       if (result == MessageBoxResult.No)       {         e.Cancel = true;       }     }       protected void MainWindow_Closed(object sender, EventArgs e)     {       Console.WriteLine("Closing Event");     }     protected void MainWindow_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)     {       this.Title = e.GetPosition(this).ToString();            }     protected void MainWindow_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)     {       this.Title = e.Key.ToString();          }          public MainWindow(string windowTitle, int height, int width)     {       this.Activated += MainWindow_Activated;       this.Deactivated += MainWindow_Deactivated;       this.Loaded += MainWindow_Loaded;       this.Closing += MainWindow_Closing;       this.Closed += MainWindow_Closed;       this.MouseMove += MainWindow_MouseMove;       this.KeyUp += MainWindow_KeyUp;            btnExitApp.Click += new RoutedEventHandler(btnExitApp_Clicked);       btnExitApp.Content = "Exit Application";       btnExitApp.Height = 25;       btnExitApp.Width = 100;       this.AddChild(btnExitApp);       this.Title = windowTitle;       this.WindowStartupLocation = WindowStartupLocation.CenterScreen;       this.Height = height;       this.Width = width;       this.Show();     }     private void btnExitApp_Clicked(object sender, RoutedEventArgs e)     {       Console.WriteLine((bool)Application.Current.Properties["A"]);       Application.Current.Shutdown();     }   } }