Предварительный просмотр печати: некоторые свойства принтера, кажется, не предварительно просмотреныC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Предварительный просмотр печати: некоторые свойства принтера, кажется, не предварительно просмотрены

Сообщение Anonymous »

Я следовал учебным пособиям по печати и печати. Я использовал printpreviewcontrol вместо printpreviewdialog . Я также добавил кнопку «Свойства принтера» в свой диалог, поэтому я могу изменить настройки, такие как безграничные / серого, размер страницы. Печать работает нормально: выходит зеленый или серый в зависимости от свойств. Предварительный просмотр всегда выходит зеленым с обычной границей. Предварительный просмотр правильно реагирует на изменения размера страницы. .Net Framework 4.8 Project) < /p>
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
// from
// https://learn.microsoft.com/en-us/dotne ... t-document
// and
// https://learn.microsoft.com/en-us/dotne ... nt-preview
// then improved with printPreviewControl1
// Q at
// https://stackoverflow.com/questions/796 ... eview-fake
namespace Penrose
{
public partial class printCustom2 : Form
{
private string stringToPrint = ""; // portion of the document that is not printed.
readonly string docName = "testPage.txt";
readonly string docPath = @"C:\Users\georg\My Drive\Personal\Godel Escher Bach\Maths\Penrose test";
public PrinterSettings printerSettings { get; private set; }
public printCustom2()
{
InitializeComponent();
printerSettings = new PrinterSettings();
printerSettings.PrinterName = "Canon TS8200 series"; // use your printer here
printDocument1.PrinterSettings = printerSettings;
}

private void ButtonPreview_Click(object sender, EventArgs e)
{
string fullPath = System.IO.Path.Combine(docPath, docName);
printDocument1.DocumentName = docName;
stringToPrint = System.IO.File.ReadAllText(fullPath);
printPreviewControl1.Document = printDocument1;
printPreviewControl1.StartPage = 0;
printPreviewControl1.Invalidate();

//printPreviewDialog1.Document = printDocument1; // original from tutorial
//printPreviewDialog1.ShowDialog();
}
private void ButtonUp_Click(object sender, EventArgs e)
{
if (printPreviewControl1.StartPage > 0)
{
printPreviewControl1.StartPage--;
printPreviewControl1.Invalidate();
}
}
private void ButtonDown_Click(object sender, EventArgs e)
{
printPreviewControl1.StartPage++;
printPreviewControl1.Invalidate();
}

private void buttonPrint_Click(object sender, EventArgs e)
{
string fullPath = System.IO.Path.Combine(docPath, docName);
printDocument1.DocumentName = docName;
stringToPrint = System.IO.File.ReadAllText(fullPath);
printDocument1.Print();
}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Console.WriteLine("printDocument1_PrintPage");
int charactersOnPage = 0;
int linesPerPage = 0;

// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);

// Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Green,
e.MarginBounds, StringFormat.GenericTypographic);

// Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage);

// Check to see if more pages are to be printed.
e.HasMorePages = (stringToPrint.Length > 0);
}

private void ButtonSettings_Click(object sender, EventArgs e)
{
// DocumentProperties at
// https://learn.microsoft.com/en-us/windo ... properties
string printerName = printerSettings.PrinterName;
IntPtr hPrinter = IntPtr.Zero;

if (!OpenPrinter(printerName, out hPrinter, IntPtr.Zero))
return;
IntPtr pDevMode = IntPtr.Zero; // the printer properties
IntPtr pDevModeOut = IntPtr.Zero;
IntPtr settingsDevMode = printerSettings.GetHdevmode();
IntPtr pDevModeSettings = IntPtr.Zero;

try
{
// Get current DEVMODE
int sizeNeeded = DocumentProperties(IntPtr.Zero, hPrinter, printerName, IntPtr.Zero, IntPtr.Zero, 0);
if (sizeNeeded
А вот сгенерированный код для диалога < /p>
namespace Penrose
{
partial class printCustom2
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;

///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(printCustom2));
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
this.ButtonPreview = new System.Windows.Forms.Button();
this.buttonPrint = new System.Windows.Forms.Button();
this.printPreviewControl1 = new System.Windows.Forms.PrintPreviewControl();
this.ButtonUp = new System.Windows.Forms.Button();
this.ButtonDown = new System.Windows.Forms.Button();
this.ButtonSettings = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// printDocument1
//
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
//
// 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.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.Visible = false;
//
// ButtonPreview
//
this.ButtonPreview.Location = new System.Drawing.Point(262, 535);
this.ButtonPreview.Name = "ButtonPreview";
this.ButtonPreview.Size = new System.Drawing.Size(138, 54);
this.ButtonPreview.TabIndex = 0;
this.ButtonPreview.Text = "Preview";
this.ButtonPreview.UseVisualStyleBackColor = true;
this.ButtonPreview.Click += new System.EventHandler(this.ButtonPreview_Click);
//
// buttonPrint
//
this.buttonPrint.Location = new System.Drawing.Point(262, 665);
this.buttonPrint.Name = "buttonPrint";
this.buttonPrint.Size = new System.Drawing.Size(138, 53);
this.buttonPrint.TabIndex = 1;
this.buttonPrint.Text = "Print";
this.buttonPrint.UseVisualStyleBackColor = true;
this.buttonPrint.Click += new System.EventHandler(this.buttonPrint_Click);
//
// printPreviewControl1
//
this.printPreviewControl1.Location = new System.Drawing.Point(506, 163);
this.printPreviewControl1.Name = "printPreviewControl1";
this.printPreviewControl1.Size = new System.Drawing.Size(636, 916);
this.printPreviewControl1.TabIndex = 2;
//
// ButtonUp
//
this.ButtonUp.Location = new System.Drawing.Point(424, 514);
this.ButtonUp.Name = "ButtonUp";
this.ButtonUp.Size = new System.Drawing.Size(41, 40);
this.ButtonUp.TabIndex = 3;
this.ButtonUp.Text = "↑";
this.ButtonUp.UseVisualStyleBackColor = true;
this.ButtonUp.Click += new System.EventHandler(this.ButtonUp_Click);
//
// ButtonDown
//
this.ButtonDown.Location = new System.Drawing.Point(424, 560);
this.ButtonDown.Name = "ButtonDown";
this.ButtonDown.Size = new System.Drawing.Size(41, 40);
this.ButtonDown.TabIndex = 4;
this.ButtonDown.Text = "↓";
this.ButtonDown.UseVisualStyleBackColor = true;
this.ButtonDown.Click += new System.EventHandler(this.ButtonDown_Click);
//
// ButtonSettings
//
this.ButtonSettings.Location = new System.Drawing.Point(262, 414);
this.ButtonSettings.Name = "ButtonSettings";
this.ButtonSettings.Size = new System.Drawing.Size(138, 54);
this.ButtonSettings.TabIndex = 5;
this.ButtonSettings.Text = "Settings";
this.ButtonSettings.UseVisualStyleBackColor = true;
this.ButtonSettings.Click += new System.EventHandler(this.ButtonSettings_Click);
//
// printCustom2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1192, 1120);
this.Controls.Add(this.ButtonSettings);
this.Controls.Add(this.ButtonDown);
this.Controls.Add(this.ButtonUp);
this.Controls.Add(this.printPreviewControl1);
this.Controls.Add(this.buttonPrint);
this.Controls.Add(this.ButtonPreview);
this.Name = "printCustom2";
this.Text = "printCustom2";
this.ResumeLayout(false);

}

#endregion

private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Windows.Forms.Button ButtonPreview;
private System.Windows.Forms.Button buttonPrint;
private System.Windows.Forms.PrintPreviewControl printPreviewControl1;
private System.Windows.Forms.Button ButtonDown;
private System.Windows.Forms.Button ButtonUp;
private System.Windows.Forms.Button ButtonSettings;
}
}


Подробнее здесь: https://stackoverflow.com/questions/796 ... -previewed
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»