using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace GridPrintPreviewLib
{
/// <summary>
/// Control to manage margins setting with preview
/// </summary>
public partial class GLMarginSettings : UserControl
{
// Maring and Size are stored in display unit
private Margins m_PageMargins = new Margins(0, 0 , 0, 0);
private PaperSize m_PageSize = new PaperSize();
// Unit used for show margin to user
private PrinterUnit m_UnitForShow = PrinterUnit.Display;
// Flag to check if page is in landscape
private bool m_Landscape = false;
public GLMarginSettings()
{
InitializeComponent();
}
/// <summary>
/// PageSize in display unit
/// </summary>
public PaperSize PageSize
{
get
{
return m_PageSize;
}
set
{
m_PageSize = value;
RefreshPreview();
}
}
/// <summary>
/// Margins in display unit
/// </summary>
public Margins PageMargins
{
get
{
return m_PageMargins;
}
set
{
m_PageMargins = value;
RefreshPreview();
RefreshUnit();
}
}
/// <summary>
/// Unit used for show values to user
/// </summary>
public PrinterUnit ShowUnit
{
get
{
return m_UnitForShow;
}
set
{
m_UnitForShow = value;
RefreshUnit();
}
}
/// <summary>
/// Get/Set if page in is landscape
/// </summary>
public bool Landscape
{
get
{
return m_Landscape;
}
set
{
m_Landscape = value;
RefreshPreview();
}
}
/// <summary>
/// Refresh image of the page with margin
/// </summary>
private void RefreshPreview()
{
if(m_PageSize.Width == 0 || m_PageSize.Height == 0)
{
return;
}
if (pbPage.Image == null)
{
Bitmap bitmap = new Bitmap(pbPage.Width, pbPage.Height);
pbPage.Image = bitmap;
}
using (Graphics g = Graphics.FromImage(pbPage.Image))
{
float pageWidth = (!m_Landscape) ? m_PageSize.Width : m_PageSize.Height;
float pageHeight = (!m_Landscape) ? m_PageSize.Height : m_PageSize.Width;
float scaleX = pbPage.Width / pageWidth;
float scaleY = pbPage.Height / pageHeight;
float scale = Math.Min(scaleX, scaleY);
SizeF scalePageSize = new SizeF(pageWidth * scale, pageHeight * scale);
float x = (pbPage.Width - scalePageSize.Width) / 2;
float y = (pbPage.Height - scalePageSize.Height) / 2;
g.FillRectangle(Brushes.Gray, 0, 0, pbPage.Width, pbPage.Height);
g.FillRectangle(Brushes.White, x, y, scalePageSize.Width, scalePageSize.Height);
Pen p = new Pen(Color.Black, 1);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
float mleft = m_PageMargins.Left * scaleX + x;
g.DrawLine(p, mleft, 0, mleft, pbPage.Height);
float mright = x + scalePageSize.Width - (m_PageMargins.Right * scaleX);
g.DrawLine(p, mright, 0, mright, pbPage.Height);
float mtop = m_PageMargins.Top * scaleY + y;
g.DrawLine(p, 0, mtop, pbPage.Width, mtop);
float mbottom = y + scalePageSize.Height - (m_PageMargins.Bottom * scaleY);
g.DrawLine(p, 0, mbottom, pbPage.Width, mbottom);
}
this.pbPage.Refresh();
}
/// <summary>
/// Refresh unit shown to user
/// </summary>
private void RefreshUnit()
{
Margins m = TransformHelper.Convert(m_PageMargins, PrinterUnit.Display, m_UnitForShow);
nudLeft.Value = m.Left;
nudTop.Value = m.Top;
nudRight.Value = m.Right;
nudBottom.Value = m.Bottom;
}
/// <summary>
/// Handle numeric up down change
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void nud_ValueChanged(object sender, EventArgs e)
{
Margins m = new Margins((int)nudLeft.Value, (int)nudRight.Value, (int)nudTop.Value, (int)nudBottom.Value);
m_PageMargins = TransformHelper.Convert(m, m_UnitForShow, PrinterUnit.Display);
this.RefreshPreview();
}
}
}