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();
}
}
Month: February 2011
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 ();
}
}
}
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());
}
}
PictureBox Scroll Text
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
Font textFont = new Font("Times New Roman", 24);
public Form1() {
this.panel1 = new System.Windows.Forms.Panel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
this.panel1.AutoScroll = true;
this.panel1.AutoScrollMinSize = new System.Drawing.Size(600, 400);
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(13, 13);
this.panel1.Size = new System.Drawing.Size(267, 243);
this.pictureBox1.BackColor = System.Drawing.SystemColors.Window;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Size = new System.Drawing.Size(600, 400);
this.pictureBox1.TabStop = false;
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 268);
this.Controls.Add(this.panel1);
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
private void Form1_Load(object sender, EventArgs e) {
Bitmap b = new Bitmap(600, 600);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(Brushes.White, new Rectangle(0, 0, b.Width, b.Height));
g.DrawString("Hello, World", textFont, Brushes.Black, 40, 40);
g.DrawString("Hello, World", textFont, Brushes.Red, 40, 240);
g.DrawString("Hello, World", textFont, Brushes.Blue, 350, 40);
g.DrawString("Hello, World", textFont, Brushes.Green, 350, 240);
pictureBox1.BackgroundImage = b;
pictureBox1.Size = b.Size;
}
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.PictureBox pictureBox1;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
PictureBox visible and invisible
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class ListText : Form {
private Button choose = new Button();
private ListBox animalList = new ListBox( );
private ListBox things = new ListBox( );
private TextBox text = new TextBox( );
private PictureBox picture = new PictureBox();
private PictureBox photo = new PictureBox();
private CheckBox show = new CheckBox();
private CheckBox author = new CheckBox();
private Bitmap moon = new Bitmap(“winter.jpg”);
public ListText( ) {
Text = “List Text”;
choose.Text = “Choose”;
show.Text = “Show a bitmap”;
author.Text = “Show another bitmap”;
Size = new Size(400, 300);
choose.Size = new Size(100,20);
text.Size = new Size(150,50);
photo.Size = new Size(100,100);
choose.Location = new Point(20,30);
animalList.Location = new Point(30 + choose.Width, 30);
things.Location = new Point(40 + choose.Width + animalList.Width, 30);
text.Location = new Point(20, 150);
photo.Location = new Point(40 + text.Width, 150);
picture.Location = new Point(60 + text.Width + photo.Width, 150);
show.Location = new Point(20,70);
author.Location = new Point(20,110);
animalList.SelectionMode = SelectionMode.MultiSimple;
things.SelectionMode = SelectionMode.One;
text.Multiline = true;
picture.Image = (Image)moon;
picture.Visible = false;
photo.Image = Image.FromFile(“winter.jpg”);
photo.Visible = false;
BackColor = Color.White;
choose.BackColor = Color.Pink;
animalList.Items.Add(“A”);
animalList.Items.Add(“B”);
animalList.Items.Add(“C”);
animalList.Items.Add(“D”);
animalList.Items.Add(“E”);
things.Items.Add(“1”);
things.Items.Add(“2”);
things.Items.Add(“3”);
things.Items.Add(“4”);
Controls.Add(animalList);
Controls.Add(things);
Controls.Add(choose);
Controls.Add(text);
Controls.Add(picture);
Controls.Add(show);
Controls.Add(author);
Controls.Add(photo);
choose.Click += new EventHandler(Choose_Click);
things.SelectedIndexChanged += new EventHandler(Things_Changed);
show.CheckedChanged += new EventHandler(Picture_Changed);
author.CheckedChanged += new EventHandler(Photo_Changed);
}
protected void Choose_Click(object sender, EventArgs e) {
for(int i = 0; i < animalList.SelectedItems.Count; i++){
Console.WriteLine(animalList.SelectedItems[i].ToString());
}
}
protected void Things_Changed(object sender, EventArgs e) {
text.Text = "You selected " + things.SelectedItem;
}
protected void Picture_Changed(Object sender, EventArgs e) {
if (show.Checked)
picture.Visible = true;
else
picture.Visible = false;
Invalidate();
}
protected void Photo_Changed(Object sender, EventArgs e) {
if (author.Checked)
photo.Visible = true;
else
photo.Visible = false;
Invalidate();
}
static void Main() {
Application.Run(new ListText());
}
}
[/csharp]
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)

