Click here to Skip to main content
15,904,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone point me to an example of adding controls to the PageSetupDialog?
I have need of adding several checkboxes and a button for things like Stretch to Fit and center on page. According to this article it's possible but I'm going to need an example to wrap my head around it.


http://msdn.microsoft.com/en-us/library/windows/desktop/ms646962(v=vs.85).aspx[^]

Thanks for any help you can give,
Richard Chase
Posted
Comments
Maciej Los 11-Oct-12 17:04pm    
have you seen this?
vonalaric 11-Oct-12 19:02pm    
Unfortunately that only deals with using the PageSetupDialog but doesn’t get into how to customize it.
Maciej Los 12-Oct-12 11:16am    
"This (PageSetupDialog) class cannot be inherited" - it means it can't be customized :( BUT not everything is lost!
See my answer.

I'm not sure what you want to do exactly, but it sounds like you need to use PrintForm[^].
 
Share this answer
 
I am aware that the class PageSetupDialog can’t be inherited but according to the article I linked to in my initial question you can supply a custom template to add controls to the PageSetupDialog but it’s all C++ structures and I’m not used to working with them.

As to what I’m trying to do, here is a little test app I through together that demonstrates what I want to do. I haven’t provided code for 2 of the check boxes but I want all 4 to appear on the PageSetupDialog and not on the form.



C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
using System.Collections;



namespace PrintingJPGsTestapp
{
    public partial class frmPrintJPGsTestApp : Form
    {
        private System.ComponentModel.IContainer components = null;

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem printToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem pageSetupToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem printPreviewToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem printToolStripMenuItem1;

        private PrintDocument printDoc = new PrintDocument();
        private PageSettings pgSettings = new PageSettings();
        private Button btnBrowse;
        private TextBox textBox1;
        private CheckBox chkFitToPage;
        private CheckBox chkCenter;
        private CheckBox chkOrient;
        private CheckBox chkSave;
        private Button btnPrint;
        private Button btnExit;
        private OpenFileDialog openFileDialog1;
        private PrinterSettings prtSettings = new PrinterSettings();

        Image PicToPrint;



        public frmPrintJPGsTestApp()
        {
            InitializeComponent();
            printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
        }

        /// Clean up any resources being used.
        /// 
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.printToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.pageSetupToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.printPreviewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.printToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.btnBrowse = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.chkFitToPage = new System.Windows.Forms.CheckBox();
            this.chkCenter = new System.Windows.Forms.CheckBox();
            this.chkOrient = new System.Windows.Forms.CheckBox();
            this.chkSave = new System.Windows.Forms.CheckBox();
            this.btnPrint = new System.Windows.Forms.Button();
            this.btnExit = new System.Windows.Forms.Button();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.printToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(504, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.exitToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
            this.fileToolStripMenuItem.Text = "&File";
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(103, 22);
            this.exitToolStripMenuItem.Text = "E&xit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // printToolStripMenuItem
            // 
            this.printToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.pageSetupToolStripMenuItem,
            this.printPreviewToolStripMenuItem,
            this.printToolStripMenuItem1});
            this.printToolStripMenuItem.Name = "printToolStripMenuItem";
            this.printToolStripMenuItem.Size = new System.Drawing.Size(41, 20);
            this.printToolStripMenuItem.Text = "Print";
            // 
            // pageSetupToolStripMenuItem
            // 
            this.pageSetupToolStripMenuItem.Name = "pageSetupToolStripMenuItem";
            this.pageSetupToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.pageSetupToolStripMenuItem.Text = "Page Set&up...";
            this.pageSetupToolStripMenuItem.Click += new System.EventHandler(this.filePageSetupMenuItem_Click);
            // 
            // printPreviewToolStripMenuItem
            // 
            this.printPreviewToolStripMenuItem.Name = "printPreviewToolStripMenuItem";
            this.printPreviewToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.printPreviewToolStripMenuItem.Text = "Print Pre&view";
            this.printPreviewToolStripMenuItem.Click += new System.EventHandler(this.filePrintPreviewMenuItem_Click);
            // 
            // printToolStripMenuItem1
            // 
            this.printToolStripMenuItem1.Name = "printToolStripMenuItem1";
            this.printToolStripMenuItem1.Size = new System.Drawing.Size(152, 22);
            this.printToolStripMenuItem1.Text = "&Print...";
            this.printToolStripMenuItem1.Click += new System.EventHandler(this.filePrintMenuItem_Click);
            // 
            // btnBrowse
            // 
            this.btnBrowse.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnBrowse.Location = new System.Drawing.Point(456, 43);
            this.btnBrowse.Name = "btnBrowse";
            this.btnBrowse.Size = new System.Drawing.Size(31, 24);
            this.btnBrowse.TabIndex = 1;
            this.btnBrowse.Text = "...";
            this.btnBrowse.UseVisualStyleBackColor = true;
            this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(15, 43);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(441, 20);
            this.textBox1.TabIndex = 2;
            // 
            // chkFitToPage
            // 
            this.chkFitToPage.AutoSize = true;
            this.chkFitToPage.Font = new System.Drawing.Font("Times New Roman", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.chkFitToPage.Location = new System.Drawing.Point(15, 77);
            this.chkFitToPage.Name = "chkFitToPage";
            this.chkFitToPage.Size = new System.Drawing.Size(83, 17);
            this.chkFitToPage.TabIndex = 3;
            this.chkFitToPage.Text = "Fit To Page";
            this.chkFitToPage.UseVisualStyleBackColor = true;
            // 
            // chkCenter
            // 
            this.chkCenter.AutoSize = true;
            this.chkCenter.Font = new System.Drawing.Font("Times New Roman", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.chkCenter.Location = new System.Drawing.Point(15, 100);
            this.chkCenter.Name = "chkCenter";
            this.chkCenter.Size = new System.Drawing.Size(63, 17);
            this.chkCenter.TabIndex = 4;
            this.chkCenter.Text = "Center";
            this.chkCenter.UseVisualStyleBackColor = true;
            // 
            // chkOrient
            // 
            this.chkOrient.AutoSize = true;
            this.chkOrient.Font = new System.Drawing.Font("Times New Roman", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.chkOrient.Location = new System.Drawing.Point(104, 77);
            this.chkOrient.Name = "chkOrient";
            this.chkOrient.Size = new System.Drawing.Size(183, 17);
            this.chkOrient.TabIndex = 5;
            this.chkOrient.Text = "Orient by Picture Dimensions";
            this.chkOrient.UseVisualStyleBackColor = true;
            // 
            // chkSave
            // 
            this.chkSave.AutoSize = true;
            this.chkSave.Font = new System.Drawing.Font("Times New Roman", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.chkSave.Location = new System.Drawing.Point(104, 100);
            this.chkSave.Name = "chkSave";
            this.chkSave.Size = new System.Drawing.Size(150, 17);
            this.chkSave.TabIndex = 6;
            this.chkSave.Text = "Save as Default settings";
            this.chkSave.UseVisualStyleBackColor = true;
            // 
            // btnPrint
            // 
            this.btnPrint.Location = new System.Drawing.Point(293, 84);
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Size = new System.Drawing.Size(82, 32);
            this.btnPrint.TabIndex = 7;
            this.btnPrint.Text = "&Print";
            this.btnPrint.UseVisualStyleBackColor = true;
            this.btnPrint.Click += new System.EventHandler(this.filePrintMenuItem_Click);
            // 
            // btnExit
            // 
            this.btnExit.Location = new System.Drawing.Point(405, 85);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(82, 32);
            this.btnExit.TabIndex = 8;
            this.btnExit.Text = "E&xit";
            this.btnExit.UseVisualStyleBackColor = true;
            this.btnExit.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.FileName = "openFileDialog1";
            // 
            // frmPrintJPGsTestApp
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(504, 140);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnPrint);
            this.Controls.Add(this.chkSave);
            this.Controls.Add(this.chkOrient);
            this.Controls.Add(this.chkCenter);
            this.Controls.Add(this.chkFitToPage);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.btnBrowse);
            this.Controls.Add(this.menuStrip1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "frmPrintJPGsTestApp";
            this.Text = "Printing JPGs Test App";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion



        private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
        {
            int leftMargin = e.MarginBounds.Left;
            int topMargin = e.MarginBounds.Top;
            int PrintWidth = e.MarginBounds.Right - e.MarginBounds.Left;
            int PrintHieght = e.MarginBounds.Bottom - e.MarginBounds.Top;
            int lWidth = 0;
            int lHeight = 0;
            int lnewWidth = 0;
            int lnewHeight = 0; 

            if (this.chkFitToPage.Checked)
            {
                try
                {

                    lWidth = PicToPrint.Width;
                    lHeight = PicToPrint.Height;

                    lnewWidth = PrintWidth;
                    lnewHeight = (int)(lHeight * ((double)lnewWidth / (double)lWidth));

                    if (lnewHeight > PrintHieght)
                    {
                        lnewHeight = PrintHieght;
                        lnewWidth = (int)(lWidth * ((double)lnewHeight / (double)lHeight));
                    }

                }
                catch (Exception e2)
                {
                    MessageBox.Show(e2.Message, "Error Resizing Image", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    return;
                }


                if (this.chkCenter.Checked)
                {
                    //Center Image
                }

                if (this.chkSave.Checked)
                {
                    //Save settings for use later
                }

            }

            if (this.chkFitToPage.Checked)
            {
                try
                {
                    e.Graphics.DrawImage(PicToPrint, leftMargin, topMargin, lnewWidth, lnewHeight);
                }
                catch (Exception e2)
                {
                    MessageBox.Show(e2.Message, "Error Printing Image", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
            else
            {
                try
                {
                    e.Graphics.DrawImage(PicToPrint, leftMargin, topMargin);
                }
                catch (Exception e2)
                {
                    MessageBox.Show(e2.Message, "Error Printing Image", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }


        }

        private void LoadImage()
        {
            try
            {
                PicToPrint = Image.FromStream(new MemoryStream(System.IO.File.ReadAllBytes(this.textBox1.Text)));
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message, "Picture Load Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }


            if (this.chkOrient.Checked)
            {
                if (PicToPrint.Width > PicToPrint.Height)
                {
                    pgSettings.Landscape = true;
                }
                else
                {
                    pgSettings.Landscape = false;
                }
            }
        }


        private void filePrintMenuItem_Click(Object sender, EventArgs e)
        {
            LoadImage();

            printDoc.DefaultPageSettings = pgSettings;
            PrintDialog dlg = new PrintDialog();
            dlg.Document = printDoc;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                printDoc.Print();
            }
        }

        private void filePrintPreviewMenuItem_Click(Object sender, EventArgs e)
        {
            LoadImage();
            PrintPreviewDialog dlg = new PrintPreviewDialog();
            printDoc.DefaultPageSettings = pgSettings;
            dlg.Document = printDoc;
            dlg.ShowDialog();
        }

        private void filePageSetupMenuItem_Click(Object sender, EventArgs e)
        {
            PageSetupDialog pageSetupDialog = new PageSetupDialog();
            pageSetupDialog.PageSettings = pgSettings;
            pageSetupDialog.PrinterSettings = prtSettings;
            pageSetupDialog.AllowOrientation = true;
            pageSetupDialog.AllowMargins = true;
            pageSetupDialog.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPGs (.jpg)|*.jpg";

            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) 
            {
                this.textBox1.Text = openFileDialog1.FileName;
            }

        }

    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmPrintJPGsTestApp());
        }
    }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900