|
-
Mar 23rd, 2006, 09:03 AM
#1
Thread Starter
New Member
[Resolved] Print margins problem
Hi there
I am having issues with getting print preview to work reliably with C#.
I have viewed the help files/docs on msdn and scoured many other articles on the web, its quite tricky to piece together how printing is meant to work as there is a lack of documentation on the issue however i have struggled on to finally get my little app working to some degree only to find what i think is a possible bug! (but probly bad code on my behalf)
I am making a windows forms app that has a rich text box, some buttons to print preview/page settings/print and also a print preview.
The problem arises when i open the page settings dialog, every time i open this the settings for the margins are changed to be made smaller, this even happens if myPageSetupDialog.AllowMargins = false.
I am fairly sure that this is not down to my code as i do not touch the margins in anyway except when the printPage event is fired and i print a line with the MarginBounds settings.
I dont think the values of the margins should be changing at all.
Code:
namespace SimplePrinting
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private PrintDocument myPrintDoc = new PrintDocument();
private System.Windows.Forms.RichTextBox richTextBox1;
private PrintPreviewDialog myPrintPreviewDialog = new PrintPreviewDialog();
private PrintDialog myPrintDialog = new PrintDialog();
private PageSettings myPageSettings = new PageSettings();
private System.Windows.Forms.PrintPreviewControl printPreviewControl1;
private System.Windows.Forms.Button btnUpdate;
private System.Windows.Forms.ComboBox comboPrinters;
PrinterSettings myPrinterSettings = new PrinterSettings();
public Form1()
{
myPrintDoc.PrintPage += new PrintPageEventHandler(myPrintDoc_PrintPage);
MenuItem fileMenuItem = new MenuItem("&File");
MenuItem filePageSetupMenuItem = new MenuItem("Page Set&up...",
new EventHandler(filePageSetupMenuItem_Click));
MenuItem filePrintPreviewMenuItem = new MenuItem("Print Pre&view",
new EventHandler(filePrintPreviewMenuItem_Click));
MenuItem filePrintMenuItem = new MenuItem("&Print...",
new EventHandler(filePrintMenuItem_Click), Shortcut.CtrlP);
fileMenuItem.MenuItems.Add(filePageSetupMenuItem);
fileMenuItem.MenuItems.Add(filePrintPreviewMenuItem);
fileMenuItem.MenuItems.Add(filePrintMenuItem);
this.Menu = new MainMenu();
this.Menu.MenuItems.Add(fileMenuItem);
InitializeComponent();
}
private void Form1_Load(object sender, System.EventArgs e) {
richTextBox1.LoadFile("C:\\Temp\\test.rtf");
fillComboPrinters();
}
//- Event Handlers for the menu --------------
private void filePageSetupMenuItem_Click(Object sender, EventArgs e){
//Page Setup event handler
PageSetupDialog myPageSetupDialog = new PageSetupDialog();
myPageSetupDialog.PageSettings = myPageSettings;
myPageSetupDialog.PrinterSettings = myPrinterSettings;
myPageSetupDialog.AllowOrientation = true;
myPageSetupDialog.AllowMargins = true;
myPageSetupDialog.AllowPrinter = true;
if(myPageSetupDialog.ShowDialog() == DialogResult.OK){
myPrintDoc.DefaultPageSettings = myPageSetupDialog.PageSettings;
myPageSettings = myPageSetupDialog.PageSettings;
printPreviewControl1.Document = myPrintDoc;
}
}
private void filePrintPreviewMenuItem_Click(Object sender, EventArgs e){
//Print Preview event handler
myPrintDoc.DefaultPageSettings = myPageSettings;
myPrintPreviewDialog.Document = myPrintDoc;
myPrintPreviewDialog.ShowDialog();
}
private void filePrintMenuItem_Click(Object sender, EventArgs e){
//Print event handler
//myPrintDoc.Print() will fire off the newly defined
//myPrintDoc_PrintPage() event where we make the content to send.
//but first we set the documents print settings
//then we set the printdialogs document
//otherwise it doesnt know what the fsck is going on!
//then finally show the print dialog box
myPrintDoc.DefaultPageSettings = myPageSettings;
myPrintDialog.Document = myPrintDoc;
if(myPrintDialog.ShowDialog() == DialogResult.OK){
myPrintDoc.Print();
}
}
//- End of menu Event Handlers --------------------
private void myPrintDoc_PrintPage(Object sender, PrintPageEventArgs e){
//PrintPage event handler, here we acctually print stuff
//using the PrintPageEventArgs object.
//its .Graphics property represents a print page and we
//can use its methods to print stuff!
Brush myBrush = new SolidBrush(richTextBox1.ForeColor);
//set the margins
int leftMargin = e.MarginBounds.Left;
int topMargin = e.MarginBounds.Top;
int y = 0;
foreach(String line in richTextBox1.Lines){
e.Graphics.DrawString (line, richTextBox1.Font, myBrush, leftMargin, topMargin + y);
y +=15;
if(y >= e.MarginBounds.Bottom){
e.HasMorePages = true;
y = 0;
return;
}
else{
e.HasMorePages = false;
}
}
}
private void btnUpdate_Click(object sender, System.EventArgs e) {
printPreviewControl1.Document = null;
myPrintDoc.DefaultPageSettings = myPageSettings;
printPreviewControl1.Document = myPrintDoc;
}
private void comboInstalledPrinters_SelectionChanged(object sender, System.EventArgs e) {
System.Console.WriteLine("Combo changed!");
if(comboPrinters.SelectedIndex != -1){
myPrintDoc.PrinterSettings.PrinterName = comboPrinters.Text;
myPrinterSettings.PrinterName = comboPrinters.Text;
myPageSettings.PrinterSettings.PrinterName = comboPrinters.Text;
}
}
private void fillComboPrinters(){
string strCurPrinter = null;
for(int i = 0; i < PrinterSettings.InstalledPrinters.Count -1; i++){
strCurPrinter = PrinterSettings.InstalledPrinters[i];
comboPrinters.Items.Add(strCurPrinter);
System.Console.WriteLine(strCurPrinter);
}
}
}
}
this is really just a tarted up version of http://www.ondotnet.com/pub/a/dotnet.../printing.html which i would have thought would work, but i still get this funny anomaly.
any help would be much appreciated
thanks alot
Eddy Parris
Last edited by Hack; Mar 28th, 2006 at 09:17 AM.
Reason: Added green "resolved" checkmark
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|