Csharp/C Sharp/GUI Windows Form/Form Style

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

AutoScroll Window

<source lang="csharp">

 using System;
 using System.Drawing;
 using System.Collections;
 using System.ruponentModel;
 using System.Windows.Forms;
 using System.Data;
 public class Form1 : System.Windows.Forms.Form
 {
   private Button myButton; 
   public Form1()
   {
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
       
           this.AutoScroll=true;
     myButton = new Button();
     myButton.Text = "www.nfex.ru";
     myButton.Location = new System.Drawing.Point(64, 32);
     myButton.Size = new System.Drawing.Size(150, 50);

     Controls.Add(myButton);
   }
   static void Main() 
   {
     Application.Run(new Form1());
   }
 }
          
      </source>


BorderLess Window

<source lang="csharp">

 using System;
 using System.Drawing;
 using System.Collections;
 using System.ruponentModel;
 using System.Windows.Forms;
 using System.Data;
 public class Form1 : System.Windows.Forms.Form
 {
   private Button myButton; 
   public Form1()
   {
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
       
           this.ControlBox=false;
           this.MaximizeBox=false;
           this.MinimizeBox=false;
           this.FormBorderStyle=FormBorderStyle.None;
     myButton = new Button();
     myButton.Text = "www.nfex.ru";
     myButton.Location = new System.Drawing.Point(64, 32);
     myButton.Size = new System.Drawing.Size(150, 50);

           myButton.Click+=new EventHandler(Close_Window);        
     
     Controls.Add(myButton);
   }
       public void Close_Window(object sender,EventArgs eArgs) {
          ((Form)((Button)sender).Parent).Close();
       }
   static void Main() 
   {
     Application.Run(new Form1());
   }
 }


      </source>


MinimumWindow Size

<source lang="csharp">

using System; using System.Drawing; using System.Windows.Forms;

class HowdyWorldFullFit: Form {

    public static void Main()
    {
         Application.Run(new HowdyWorldFullFit());
    }
    public HowdyWorldFullFit()
    {
         ResizeRedraw = true; 
         MinimumSize = SystemInformation.MinimumWindowSize + new Size(0,1);
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
         DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
    }     
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
    {
         Font  font  = new Font("Times New Roman", 10, FontStyle.Italic);
         SizeF sizef = grfx.MeasureString(Text, font);
         float fScaleHorz = cx / sizef.Width;
         float fScaleVert = cy / sizef.Height;
  
         grfx.ScaleTransform(fScaleHorz, fScaleVert);
  
         grfx.DrawString(Text, font, new SolidBrush(clr), 0, 0);
    }

}

</source>


Non-resizable form

<source lang="csharp">

 using System;
 using System.Drawing;
 using System.Collections;
 using System.ruponentModel;
 using System.Windows.Forms;
 using System.Data;
 public class Form1 : System.Windows.Forms.Form
 {
   private Button myButton; 
   public Form1()
   {
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
       
           this.MaximumSize=new Size(300,300);
           this.MinimumSize=new Size(300,300);
     myButton = new Button();
     myButton.Text = "www.nfex.ru";
     myButton.Location = new System.Drawing.Point(64, 32);
     myButton.Size = new System.Drawing.Size(150, 50);
     Controls.Add(myButton);
   }
   static void Main() 
   {
     Application.Run(new Form1());
   }
 }


      </source>


Not in TaskBar

<source lang="csharp">

 using System;
 using System.Drawing;
 using System.Collections;
 using System.ruponentModel;
 using System.Windows.Forms;
 using System.Data;
 public class Form1 : System.Windows.Forms.Form
 {
   private Button myButton; 
   public Form1()
   {
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
       
           this.ShowInTaskbar=false;
     myButton = new Button();
     myButton.Text = "Minimize the window and you won"t find it in TaskBar";
     myButton.Location = new System.Drawing.Point(64, 32);
     myButton.Size = new System.Drawing.Size(450, 50);

     Controls.Add(myButton);
   }
   static void Main() 
   {
     Application.Run(new Form1());
   }
 }
          
      </source>


Self Placing Window (save form window related information to Registry)

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32;

   public class Form1 : Form
   {
       private System.Windows.Forms.ListBox listBoxMessages;
       private System.Windows.Forms.Button buttonChooseColor;
       private ColorDialog chooseColorDialog = new ColorDialog();
       public Form1()
       {
           this.listBoxMessages = new System.Windows.Forms.ListBox();
           this.buttonChooseColor = new System.Windows.Forms.Button();
           this.SuspendLayout();
           this.listBoxMessages.Size = new System.Drawing.Size(288, 199);
           this.buttonChooseColor.Location = new System.Drawing.Point(0, 208);
           this.buttonChooseColor.Size = new System.Drawing.Size(104, 23);
           this.buttonChooseColor.Text = "Choose Color";
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(292, 232);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                     this.buttonChooseColor,
                                     this.listBoxMessages});
           this.ResumeLayout(false);
           buttonChooseColor.Click += new EventHandler(OnClickChooseColor);
           try
           {
               if (ReadSettings() == false)
                   listBoxMessages.Items.Add("No information in registry");
               else
                   listBoxMessages.Items.Add("Information read in from registry");
               StartPosition = FormStartPosition.Manual;
           }
           catch (Exception e)
           {
               listBoxMessages.Items.Add("A problem occurred reading in data from registry:");
               listBoxMessages.Items.Add(e.Message);
           }
       }
       void OnClickChooseColor(object Sender, EventArgs e)
       {
           if (chooseColorDialog.ShowDialog() == DialogResult.OK)
               BackColor = chooseColorDialog.Color;
       }
       void SaveSettings()
       {
           RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software", true);
           RegistryKey wroxKey = softwareKey.CreateSubKey("JJJ");
           RegistryKey selfPlacingWindowKey = wroxKey.CreateSubKey("SelfPlacingWindow");
           selfPlacingWindowKey.SetValue("BackColor", (object)BackColor.ToKnownColor());
           selfPlacingWindowKey.SetValue("Red", (object)(int)BackColor.R);
           selfPlacingWindowKey.SetValue("Green", (object)(int)BackColor.G);
           selfPlacingWindowKey.SetValue("Blue", (object)(int)BackColor.B);
           selfPlacingWindowKey.SetValue("Width", (object)Width);
           selfPlacingWindowKey.SetValue("Height", (object)Height);
           selfPlacingWindowKey.SetValue("X", (object)DesktopLocation.X);
           selfPlacingWindowKey.SetValue("Y", (object)DesktopLocation.Y);
           selfPlacingWindowKey.SetValue("WindowState", (object)WindowState.ToString());
       }
       bool ReadSettings()
       {
           RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software");
           RegistryKey wroxKey = softwareKey.OpenSubKey("JJJ");
           if (wroxKey == null)
               return false;
           RegistryKey selfPlacingWindowKey =
               wroxKey.OpenSubKey("SelfPlacingWindow");
           if (selfPlacingWindowKey == null)
               return false;
           else
               listBoxMessages.Items.Add("Successfully opened key " + selfPlacingWindowKey.ToString());
           int redComponent = (int)selfPlacingWindowKey.GetValue("Red");
           int greenComponent = (int)selfPlacingWindowKey.GetValue("Green");
           int blueComponent = (int)selfPlacingWindowKey.GetValue("Blue");
           this.BackColor = Color.FromArgb(redComponent, greenComponent, blueComponent);
           listBoxMessages.Items.Add("Background color: " + BackColor.Name);
           int X = (int)selfPlacingWindowKey.GetValue("X");
           int Y = (int)selfPlacingWindowKey.GetValue("Y");
           this.DesktopLocation = new Point(X, Y);
           listBoxMessages.Items.Add("Desktop location: " + DesktopLocation.ToString());
           this.Height = (int)selfPlacingWindowKey.GetValue("Height");
           this.Width = (int)selfPlacingWindowKey.GetValue("Width");
           listBoxMessages.Items.Add("Size: " + new Size(Width, Height).ToString());
           string initialWindowState = (string)selfPlacingWindowKey.GetValue("WindowState");
           listBoxMessages.Items.Add("Window State: " + initialWindowState);
           this.WindowState = (FormWindowState)FormWindowState.Parse
               (WindowState.GetType(), initialWindowState);
           return true;
       }
       [STAThread]
       static void Main()
       {
           Application.Run(new Form1());
       }
   }
</source>