Re: PrintPreview margins not displaying by emorgoch
emorgoch
Wed Dec 06 13:33:25 CST 2006
Thanks for you attemped answer Ciaran, but I don't think you understand
my problem. The problem is that in the print preview dialog, the user
defined margins (set in the Page setup dialog) are not displayed, and
the image of the form appears in very upper left corner of the preivew
page. But when printed, the image is moved according to the margins.
The full code for an example application is included below. As for your
suggestions, they are not applicable, as this is for a .NET 1.1 Windows
form application, and the items you suggest are only available in .NET
2.0. Thank you for your suggestions though. On a side note, a nearly
identical sample application built in VS2005 on .NET 2.0 does not show
the same behavior.
Thanks
Evan
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace SimplePrintApp
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button1;
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Drawing.Bitmap formImage;
private System.ComponentModel.Container components = null;
public Form1() {
InitializeComponent();
}
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int
nWidth,
int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
public void GetFormImage() {
Graphics mygraphics = this.CreateGraphics();
Bitmap tempImage = new Bitmap(this.Size.Width, this.Size.Height,
mygraphics);
Graphics memGraphics = Graphics.FromImage(tempImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memGraphics.GetHdc();
BitBlt (dc2, 0, 0, this.ClientRectangle.Width,
this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memGraphics.ReleaseHdc(dc2);
this.formImage = tempImage;
}
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()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog();
this.printPreviewDialog1 = new
System.Windows.Forms.PrintPreviewDialog();
this.SuspendLayout();
//
// button2
//
this.button2.Location = new System.Drawing.Point(72, 80);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 23);
this.button2.TabIndex = 0;
this.button2.Text = "Print Preview";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(72, 128);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(120, 23);
this.button3.TabIndex = 1;
this.button3.Text = "Print";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 23);
this.button1.TabIndex = 2;
this.button1.Text = "Page Setup";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// printDocument1
//
this.printDocument1.OriginAtMargins = true;
this.printDocument1.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
//
// pageSetupDialog1
//
this.pageSetupDialog1.Document = this.printDocument1;
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new
System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new
System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400,
300);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon =
((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Location = new System.Drawing.Point(290,
17);
this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375,
250);
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.TransparencyKey =
System.Drawing.Color.Empty;
this.printPreviewDialog1.Visible = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e) {
e.Graphics.DrawImage (this.formImage, 0, 0);
}
private void button1_Click(object sender, System.EventArgs e) {
this.GetFormImage();
this.pageSetupDialog1.ShowDialog();
}
private void button2_Click(object sender, System.EventArgs e) {
this.GetFormImage();
this.printPreviewDialog1.ShowDialog();
}
private void button3_Click(object sender, System.EventArgs e) {
this.printDocument1.Print();
}
}
}