using System;
using System.Drawing;
using Wankuma.Interop;
namespace Wankuma.Drawing.Printing
{
///
/// PrinterInfo の概要の説明です。
///
public sealed class PrinterInfo
{
private PrinterInfo()
{
}
///
/// プリンタのマージンを取得します。
/// 用紙の縦モードで取得です。
///
///
/// ページバウンズ
/// 取得したマージン
public static Rectangle MarginBounds( string PrinterName, Rectangle PageBounds )
{
//PrinterNameがnullや""だと例外
if ( PrinterName == null || PrinterName == "" )
{
throw new ArgumentNullException("PrinterName");
}
//デバイスキャップラッパオブジェクトを作成
using ( GetDeviceCapsWrapper devicecaps = new GetDeviceCapsWrapper() )
{
//各マージンは.NETFrameworkのデフォルトと同じ1inchとする
int TopMargin = 100;
int LeftMargin = 100;
int RightMargin = 100;
int BottomMargin = 100;
if ( devicecaps.Init(PrinterName) == true )
{
int XRes = devicecaps.GetData(GetDeviceCapsWrapper.CommandIndex.LOGPIXELSX);
int YRes = devicecaps.GetData(GetDeviceCapsWrapper.CommandIndex.LOGPIXELSY);
LeftMargin = (int)((float)devicecaps.GetData(GetDeviceCapsWrapper.CommandIndex.PHYSICALOFFSETX) / (float)XRes * 100.0);
TopMargin = (int)((float)devicecaps.GetData(GetDeviceCapsWrapper.CommandIndex.PHYSICALOFFSETY) / (float)YRes * 100.0);
int Width = (int)((float)devicecaps.GetData(GetDeviceCapsWrapper.CommandIndex.PHYSICALWIDTH) / (float)XRes * 100.0);
int Height = (int)((float)devicecaps.GetData(GetDeviceCapsWrapper.CommandIndex.PHYSICALHEIGHT) / (float)YRes * 100.0);
int HorzRes = (int)((float)devicecaps.GetData(GetDeviceCapsWrapper.CommandIndex.HORZRES) / (float)XRes * 100.0);
int VertRes = (int)((float)devicecaps.GetData(GetDeviceCapsWrapper.CommandIndex.VERTRES) / (float)YRes * 100.0);
RightMargin = Width - HorzRes - LeftMargin;
BottomMargin = Height - VertRes - TopMargin;
}
if ( PageBounds.Width > PageBounds.Height )
{
//返却用構造体の作成
return new Rectangle(TopMargin, LeftMargin, PageBounds.Width - TopMargin - BottomMargin, PageBounds.Height - LeftMargin - RightMargin);
}
else
{
//返却用構造体の作成
return new Rectangle(LeftMargin, TopMargin, PageBounds.Width - LeftMargin - RightMargin, PageBounds.Height - TopMargin - BottomMargin);
}
}
}
}
}