Steps through the common dialogs. Does nothing else useful


   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// CmnDlgs.cs -- steps through the common dialogs. Does nothing else useful.
//
//               Compile this program with the following command line:
//                   C:>csc CmnDlgs.cs
using System;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace clsCommonDialogs
{
    public class CommonDialogs
    {
        [STAThread]
        static public void Main ()
        {
            // Create and display a Choose Color dialog box.
            ColorDialog cd = new ColorDialog ();
            cd.ShowDialog ();
            cd.Dispose ();
            
            // Create and display a Choose Font dialog box.
            FontDialog fd = new FontDialog ();
            fd.ShowDialog ();
            fd.Dispose ();
                
            // Create and display an Open File dialog box.
            OpenFileDialog ofd = new OpenFileDialog ();
            ofd.ShowDialog ();
            ofd.Dispose ();
            
            // Create and display a Save File dialog box.
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.ShowDialog ();
            sfd.Dispose ();
            
            // Create and display a Page Setup dialog box.
            PrintDocument printDoc = new PrintDocument();
            PageSetupDialog psd = new PageSetupDialog ();
            psd.Document = printDoc;
            psd.ShowDialog ();
            psd.Dispose ();
            
            // Create and display an Print dialog box.
            PrintDialog pd = new PrintDialog ();
            pd.Document = printDoc;
            pd.ShowDialog ();
            pd.Dispose ();
            
            // Create and display an Print Preview File dialog box.
            // This dialog is not a part of the common dialog library.
            PrintPreviewDialog ppd = new PrintPreviewDialog ();
            ppd.ShowDialog ();
            ppd.Dispose ();
            printDoc.Dispose ();
        }
    }
}


           
          


Using PrintPreviewDialog

   
 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Printing;

public class frmMain : System.Windows.Forms.Form {
    private System.Drawing.Printing.PrintDocument printDoc;
    private System.Windows.Forms.PrintPreviewDialog ppDialog;
    private System.Windows.Forms.Button btnPrint;
    public frmMain() {
        this.printDoc = new System.Drawing.Printing.PrintDocument();
        this.ppDialog = new System.Windows.Forms.PrintPreviewDialog();
        this.btnPrint = new System.Windows.Forms.Button();
        this.SuspendLayout();

        this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);

        this.ppDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
        this.ppDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
        this.ppDialog.ClientSize = new System.Drawing.Size(400, 300);
        this.ppDialog.Enabled = true;
        this.ppDialog.Location = new System.Drawing.Point(195, 22);
        this.ppDialog.MaximumSize = new System.Drawing.Size(0, 0);
        this.ppDialog.Opacity = 1;
        this.ppDialog.TransparencyKey = System.Drawing.Color.Empty;
        this.ppDialog.Visible = false;

        this.btnPrint.Location = new System.Drawing.Point(52, 28);
        this.btnPrint.Text = "Print";
        this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(176, 86);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.btnPrint});
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new frmMain());
    }

    private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
        PrinterSettings pSettings = new PrinterSettings();
        Font printFont = new Font("Arial", 12);

        int nTextPosY = e.MarginBounds.Top;
        int nTextPosX = e.MarginBounds.Left;
        int nHeight = (int)printFont.GetHeight(e.Graphics);

        foreach (string sPtr in PrinterSettings.InstalledPrinters) {
            pSettings.PrinterName = sPtr;
            if (pSettings.IsValid) {
                e.Graphics.DrawString(sPtr, printFont, Brushes.Black, nTextPosX, nTextPosY + 5);
                e.Graphics.DrawString("Can Duplex: " + pSettings.CanDuplex.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight));
                e.Graphics.DrawString("Is Default: " + pSettings.IsDefaultPrinter.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 2));
                e.Graphics.DrawString("Is Plotter: " + pSettings.IsPlotter.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 3));
                e.Graphics.DrawString("Landscape Angle: " + pSettings.LandscapeAngle.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 4));
                e.Graphics.DrawString("Maximum Copies: " + pSettings.MaximumCopies.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 5));
                e.Graphics.DrawString("Maximum Page: " + pSettings.MaximumPage.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 6));
                e.Graphics.DrawString("Minimum Page: " + pSettings.MinimumPage.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 7));
                e.Graphics.DrawString("Supports Color: " + pSettings.SupportsColor.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 8));
                nTextPosY = nTextPosY + ((5 + nHeight * 8) + nHeight);
            }
        }
    }
    private void btnPrint_Click(object sender, System.EventArgs e) {
        ppDialog.Document = printDoc;
        ppDialog.ShowDialog();
    }
}

    


Popup Menu action


   


  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class MainForm : System.Windows.Forms.Form
  {
    private int currFontSize = 20;

    private ContextMenu popUpMenu;

    private MenuItem currentCheckedItem;
    private MenuItem checkedHuge;
    private MenuItem checkedNormal;
    private MenuItem checkedTiny;

    public MainForm()
    {
      InitializeComponent();

      Text = "PopUp Menu";
      CenterToScreen();

      popUpMenu = new ContextMenu();

      popUpMenu.MenuItems.Add("Huge", new EventHandler(PopUp_Clicked));
      popUpMenu.MenuItems.Add("Normal", new EventHandler(PopUp_Clicked));
      popUpMenu.MenuItems.Add("Tiny", new EventHandler(PopUp_Clicked));


      this.ContextMenu = popUpMenu;

      checkedHuge = this.ContextMenu.MenuItems[0];
      checkedNormal = this.ContextMenu.MenuItems[1];        
      checkedTiny = this.ContextMenu.MenuItems[2];
      currentCheckedItem = checkedNormal;
      currentCheckedItem.Checked = true;

      this.Resize += new System.EventHandler(this.MainForm_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);

    }

    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Form1";

    }

    static void Main() 
    {
      Application.Run(new MainForm());
    }
    private void PopUp_Clicked(object sender, EventArgs e) 
    {
      currentCheckedItem.Checked = false;

      MenuItem miClicked = (MenuItem)sender;
      string item = miClicked.Text;
      
      if(item == "Huge")
      {
        currFontSize = 30;
        currentCheckedItem = checkedHuge;
      }

      if(item == "Normal")
      {
        currFontSize = 20;
        currentCheckedItem = checkedNormal;
      }

      if(item == "Tiny")
      {
        currFontSize = 8;
        currentCheckedItem = checkedTiny;
      }
      currentCheckedItem.Checked = true;
      Invalidate();
        }

    private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.DrawString("Right click your mouse", 
        new Font("Times New Roman", (float)currFontSize), 
        new SolidBrush(Color.Black), 
        this.DisplayRectangle);
    }

    private void MainForm_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
          


Dragging Images


   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class MainForm : Form {
    private PictureBox happyBox = new PictureBox();
    private int oldX, oldY;
    private bool isDragging;
    private Rectangle dropRect = new Rectangle(100, 100, 140, 170);

    public MainForm() {
        happyBox.SizeMode = PictureBoxSizeMode.StretchImage;
        happyBox.Location = new System.Drawing.Point(64, 32);
        happyBox.Size = new System.Drawing.Size(50, 50);
        happyBox.Cursor = Cursors.Hand;
        happyBox.Image = new Bitmap("happyDude.bmp");
        happyBox.MouseDown += new MouseEventHandler(happyBox_MouseDown);
        happyBox.MouseUp += new MouseEventHandler(happyBox_MouseUp);
        happyBox.MouseMove += new MouseEventHandler(happyBox_MouseMove);
        Controls.Add(happyBox);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 361);
        this.Text = "Dragging Images";
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);

    }

    void happyBox_MouseMove(object sender, MouseEventArgs e) {
        if (isDragging) {
            happyBox.Top = happyBox.Top + (e.Y - oldY);
            happyBox.Left = happyBox.Left + (e.X - oldX);
        }
    }

    void happyBox_MouseUp(object sender, MouseEventArgs e) {
        isDragging = false;
        if (dropRect.Contains(happyBox.Bounds))
            MessageBox.Show("You win!", "What an amazing test of skill...");
    }

    void happyBox_MouseDown(object sender, MouseEventArgs e) {
        isDragging = true;
        oldX = e.X;
        oldY = e.Y;
    }

    private void MainForm_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.FillRectangle(Brushes.BlueViolet, dropRect);
        g.DrawString("Drag the happy guy in here...",new Font("Times New Roman", 25), Brushes.WhiteSmoke, dropRect);
    }
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new MainForm());
    }
}

    


Subclass PictureBox

   
 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class PictureBoxPlusDemo: Form
{
     public static void Main()
     {
          Application.Run(new PictureBoxPlusDemo());
     }
     public PictureBoxPlusDemo()
     {
          Text = "PictureBoxPlus Demo";
   
          PictureBoxPlus picbox = new PictureBoxPlus();
          picbox.Parent = this;
          picbox.Dock = DockStyle.Fill;
          picbox.Image = Image.FromFile("Color.jpg");
     }
}
     class PictureBoxPlus: PictureBox
     {
          protected override void OnPaint(PaintEventArgs pea)
          {
               ScaleImageIsotropically(pea.Graphics, Image,ClientRectangle);
          }
          void ScaleImageIsotropically(Graphics grfx, Image image,
                                       Rectangle rect)
          {
               SizeF sizef = 
                    new SizeF(image.Width / image.HorizontalResolution,
                              image.Height / image.VerticalResolution);
   
               float fScale = Math.Min(rect.Width  / sizef.Width,
                                       rect.Height / sizef.Height);
   
               sizef.Width  *= fScale;
               sizef.Height *= fScale;
          
               grfx.DrawImage(image,
                              rect.X + (rect.Width  - sizef.Width ) / 2,
                              rect.Y + (rect.Height - sizef.Height) / 2,
                              sizef.Width, sizef.Height);
          }
     }

    


SizeMode in PictureBox

   
 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.PictureBox pictureBox2;
    private System.Windows.Forms.PictureBox pictureBox3;
    private System.Windows.Forms.PictureBox pictureBox4;
    public Form1() {
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.button1 = new System.Windows.Forms.Button();
        this.pictureBox2 = new System.Windows.Forms.PictureBox();
        this.pictureBox3 = new System.Windows.Forms.PictureBox();
        this.pictureBox4 = new System.Windows.Forms.PictureBox();
        this.SuspendLayout();
        // 
        // pictureBox1
        // 
        this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pictureBox1.Location = new System.Drawing.Point(8, 8);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(100, 70);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(232, 24);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(56, 23);
        this.button1.TabIndex = 1;
        this.button1.Text = "button1";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // pictureBox2
        // 
        this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pictureBox2.Location = new System.Drawing.Point(120, 8);
        this.pictureBox2.Name = "pictureBox2";
        this.pictureBox2.Size = new System.Drawing.Size(100, 70);
        this.pictureBox2.TabIndex = 0;
        this.pictureBox2.TabStop = false;
        // 
        // pictureBox3
        // 
        this.pictureBox3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pictureBox3.Location = new System.Drawing.Point(8, 88);
        this.pictureBox3.Name = "pictureBox3";
        this.pictureBox3.Size = new System.Drawing.Size(100, 70);
        this.pictureBox3.TabIndex = 0;
        this.pictureBox3.TabStop = false;
        // 
        // pictureBox4
        // 
        this.pictureBox4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pictureBox4.Location = new System.Drawing.Point(120, 88);
        this.pictureBox4.Name = "pictureBox4";
        this.pictureBox4.Size = new System.Drawing.Size(100, 70);
        this.pictureBox4.TabIndex = 0;
        this.pictureBox4.TabStop = false;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(376, 254);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button1,
                                      this.pictureBox2,
                                      this.pictureBox3,
                                      this.pictureBox4,
                                      this.pictureBox1});
        this.ResumeLayout(false);
        this.button1.Text = "Display";

    }

    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    private void button1_Click(object sender, System.EventArgs e) {
        SetPictureBoxSizeMode();
    }
    private void SetPictureBoxSizeMode() {
        string path = "3.BMP";  // Change the path if needed.

        pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
        pictureBox1.Image = Image.FromFile(path);

        pictureBox2.SizeMode = PictureBoxSizeMode.Normal;
        pictureBox2.Image = Image.FromFile(path);

        pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox3.Image = Image.FromFile(path);

        pictureBox4.SizeMode = PictureBoxSizeMode.AutoSize;
        pictureBox4.Image = Image.FromFile(path);
    }

}

    


PictureBox Demo



   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace BictureBox
{
    /// <summary>
    /// Summary description for PictureBoxDemo.
    /// </summary>
    public class PictureBoxDemo : System.Windows.Forms.Form
    {
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.PictureBox pictureBox3;
        private System.Windows.Forms.PictureBox pictureBox4;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public PictureBoxDemo()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            this.Text = "SizeMode in PictureBox";
            this.button1.Text = "Display";

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.button1 = new System.Windows.Forms.Button();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.pictureBox3 = new System.Windows.Forms.PictureBox();
            this.pictureBox4 = new System.Windows.Forms.PictureBox();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox1.Location = new System.Drawing.Point(8, 8);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(100, 70);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(232, 24);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(56, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // pictureBox2
            // 
            this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox2.Location = new System.Drawing.Point(120, 8);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(100, 70);
            this.pictureBox2.TabIndex = 0;
            this.pictureBox2.TabStop = false;
            // 
            // pictureBox3
            // 
            this.pictureBox3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox3.Location = new System.Drawing.Point(8, 88);
            this.pictureBox3.Name = "pictureBox3";
            this.pictureBox3.Size = new System.Drawing.Size(100, 70);
            this.pictureBox3.TabIndex = 0;
            this.pictureBox3.TabStop = false;
            // 
            // pictureBox4
            // 
            this.pictureBox4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox4.Location = new System.Drawing.Point(120, 88);
            this.pictureBox4.Name = "pictureBox4";
            this.pictureBox4.Size = new System.Drawing.Size(100, 70);
            this.pictureBox4.TabIndex = 0;
            this.pictureBox4.TabStop = false;
            // 
            // PictureBoxDemo
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(376, 254);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.button1,
                                                                          this.pictureBox2,
                                                                          this.pictureBox3,
                                                                          this.pictureBox4,
                                                                          this.pictureBox1});
            this.Name = "PictureBoxDemo";
            this.Text = "PictureBoxDemo";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new PictureBoxDemo());
        }
        private void button1_Click(object sender, System.EventArgs e)
        {
            SetPictureBoxSizeMode();
        }
        private void SetPictureBoxSizeMode()
        {
            string path = @"Dobos3.BMP";  // Change the path if needed.

            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
            pictureBox1.Image = Image.FromFile(path);
         
            pictureBox2.SizeMode = PictureBoxSizeMode.Normal;
            pictureBox2.Image = Image.FromFile(path);
         
            pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox3.Image = Image.FromFile(path);

            pictureBox4.SizeMode = PictureBoxSizeMode.AutoSize;
            pictureBox4.Image = Image.FromFile(path);
        }

    }
}

           
          


P17_PictureBox.zip( 103 k)