wingman007

WinFormCreatedOnTheConsole_v1.3

Mar 24th, 2016
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. // The example is from the book "Windows Forms in Action 2-nd edition 2012" coolcsn
  2. // USE: csc /t:exe /out:MyForm.exe *.cs
  3. // /main switch is used to determen which method is the entry point of the application
  4. // /reference switch is used  
  5. // the reference switch can be used like this  
  6. // csc MyForm.cs /r:System.dll /r:System.Windows.Forms.dll /r:System.Drawing.dll
  7. using System; // must be first
  8. // for the controls you need to add another namespace
  9. using System.Windows.Forms;
  10.  
  11. // for Bitmap
  12. using System.Drawing;
  13. /*
  14.  using WFalias = System.Windows.Forms
  15.  using MyAppAlias = System.Windows.Forms.Application
  16. With these defined, we could write our Main method as:
  17.  public static void Main()
  18.  {
  19.  WFalias.Application.EnableVisualStyles();
  20.  MyAppAlias.Run(new MyForm());
  21.  }
  22.  */
  23.  
  24. // the program works without the following line
  25. // The following line of the program simply sets the version number for the program to 1.0, matching the section number of the book.
  26. // You can verify this by right-clicking the myform.exe file, selecting Properties, and then clicking the Version tab.
  27. [assembly: System.Reflection.AssemblyVersion("1.3")]
  28. namespace MyNamespace
  29. {
  30.     // System.Windows.Forms is the namespace
  31.     // Form is the class
  32.     class MyForm : System.Windows.Forms.Form
  33.     {
  34.         // lets create fields
  35.         private Button btnLoad;
  36.         private PictureBox pbxPhoto;
  37.        
  38.         // constructor
  39.         public MyForm()
  40.         {
  41.             // set a property
  42.             this.Text = "Hello Form";
  43.            
  44.             // 1. Create and configure a button
  45.             btnLoad = new Button();
  46.             btnLoad.Text = "&Load";
  47.             btnLoad.Left = 10;
  48.             btnLoad.Top = 10;
  49.            
  50.             //  defined internally by the .NET Framework as follows:
  51.             // public delegate void EventHandler(object sender, EventArgs e);
  52.             btnLoad.Click += new EventHandler(this.HandleLoadClick);
  53.            
  54.             // 2. Create and configure a picture box
  55.             pbxPhoto = new PictureBox();
  56.            
  57.              pbxPhoto.BorderStyle =
  58.              System.Windows.Forms.BorderStyle.Fixed3D;
  59.              pbxPhoto.Width = this.Width / 2;
  60.              pbxPhoto.Height = this.Height / 2;
  61.              pbxPhoto.Left = (this.Width - pbxPhoto.Width) / 2;
  62.              pbxPhoto.Top = (this.Height - pbxPhoto.Height) / 2;
  63.              pbxPhoto.SizeMode = PictureBoxSizeMode.Zoom;
  64.              
  65.              // instance of the Control.ControlCollectionclass
  66.              // 3. Add our new controls to the form
  67.              this.Controls.Add(btnLoad);
  68.              this.Controls.Add(pbxPhoto);
  69.         }
  70.  
  71.         // the event handler
  72.         private void HandleLoadClick(object sender, System.EventArgs e)
  73.         {
  74.             OpenFileDialog dlg = new OpenFileDialog();
  75.             dlg.Title = "Open Photo";
  76.             dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";
  77.             if (dlg.ShowDialog() == DialogResult.OK)
  78.             {
  79.                 // using System.Drawing;
  80.                 pbxPhoto.Image = new Bitmap(dlg.OpenFile());
  81.             }
  82.             dlg.Dispose();
  83.         }      
  84.  
  85. // If there are multiple Main methods in an assembly, the /main switch can
  86. // be used to indicate which method should be used, or to specify an alternate method
  87. // as the entry point.
  88.  
  89. // This line is required in Windows Forms applications,
  90. // and indicates that the thread executing this method should use a singlethreaded
  91. // apartment (STA) threading model.
  92.  
  93.         [System.STAThread] // - it works without this annotation also
  94.         // we can use /main switch to the compiler to tell wich is the main method
  95.         static void Main()
  96.         {  
  97.             // System.Windows.Forms is the namespace
  98.             // Application is the class
  99.             // EnableVisualStyles() - static method
  100.             // Enables visual styles for the application.
  101.             // It still works even without this line of code
  102.             System.Windows.Forms.Application.EnableVisualStyles();
  103.  
  104.             // Starts a standard message loop on the current thread. If a Form is given, also makes that form visible.
  105.             //  wait for operating system messages
  106.             System.Windows.Forms.Application.Run(new MyForm()); // this is blocking
  107.  
  108.             /* or you can explicitly create a context          
  109.             System.Windows.Forms.Application.EnableVisualStyles();
  110.              System.Windows.Forms.Form f = new MyForm();
  111.              System.Windows.Forms.ApplicationContext context
  112.              = new System.Windows.Forms.ApplicationContext();
  113.              context.MainForm = f;
  114.              System.Windows.Forms.Application.Run(context);
  115.             */
  116.  
  117.             Console.WriteLine("Hello World!"); // at the same time writes to the console after closing the
  118.             // previous statemnet.
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment