using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Wankuma.Interop
{
///
/// PrinterInfo の概要の説明です。
///
public class GetDeviceCapsWrapper : IDisposable
{
#region P/Invoke
[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);
[DllImport("gdi32.dll")]
private static extern bool DeleteDC(IntPtr hdc);
#endregion
#region メンバ
///
/// プリンタハンドル
///
private IntPtr _PrinterHandle;
///
/// プリンタ名
///
private string _PrinterName = null;
#endregion
#region public enum CommandIndex
///
/// データ取得用コマンド番号
///
public enum CommandIndex
{
///
/// Device driver version
///
DRIVERVERSION = 0, // Device driver version
///
/// Device classification
///
TECHNOLOGY = 2, // Device classification
///
/// Horizontal size in millimeters
///
HORZSIZE = 4, // Horizontal size in millimeters
///
/// Vertical size in millimeters
///
VERTSIZE = 6, // Vertical size in millimeters
///
/// Horizontal width in pixels
///
HORZRES = 8, // Horizontal width in pixels
///
/// Vertical height in pixels
///
VERTRES = 10, // Vertical height in pixels
///
/// Number of bits per pixel
///
BITSPIXEL = 12, // Number of bits per pixel
///
/// Number of planes
///
PLANES = 14, // Number of planes
///
/// Number of brushes the device has
///
NUMBRUSHES = 16, // Number of brushes the device has
///
/// Number of pens the device has
///
NUMPENS = 18, // Number of pens the device has
///
/// Number of markers the device has
///
NUMMARKERS = 20, // Number of markers the device has
///
/// Number of fonts the device has
///
NUMFONTS = 22, // Number of fonts the device has
///
/// Number of colors the device supports
///
NUMCOLORS = 24, // Number of colors the device supports
///
/// Size required for device descriptor
///
PDEVICESIZE = 26, // Size required for device descriptor
///
/// Curve capabilities
///
CURVECAPS = 28, // Curve capabilities
///
/// Line capabilities
///
LINECAPS = 30, // Line capabilities
///
/// Polygonal capabilities
///
POLYGONALCAPS = 32, // Polygonal capabilities
///
/// Text capabilities
///
TEXTCAPS = 34, // Text capabilities
///
/// Clipping capabilities
///
CLIPCAPS = 36, // Clipping capabilities
///
/// Bitblt capabilities
///
RASTERCAPS = 38, // Bitblt capabilities
///
/// Length of the X leg
///
ASPECTX = 40, // Length of the X leg
///
/// Length of the Y leg
///
ASPECTY = 42, // Length of the Y leg
///
/// Length of the hypotenuse
///
ASPECTXY = 44, // Length of the hypotenuse
///
/// Logical pixels/inch in X
///
LOGPIXELSX = 88, // Logical pixels/inch in X
///
/// Logical pixels/inch in Y
///
LOGPIXELSY = 90, // Logical pixels/inch in Y
///
/// Number of entries in physical palette
///
SIZEPALETTE = 104, // Number of entries in physical palette
///
/// Number of reserved entries in palette
///
NUMRESERVED = 106, // Number of reserved entries in palette
///
/// Actual color resolution
///
COLORRES = 108, // Actual color resolution
// Printing related DeviceCaps. These replace the appropriate Escapes
///
/// Physical Width in device units
///
PHYSICALWIDTH = 110, // Physical Width in device units
///
/// Physical Height in device units
///
PHYSICALHEIGHT = 111, // Physical Height in device units
///
/// Physical Printable Area x margin
///
PHYSICALOFFSETX = 112, // Physical Printable Area x margin
///
/// Physical Printable Area y margin
///
PHYSICALOFFSETY = 113, // Physical Printable Area y margin
///
/// Scaling factor x
///
SCALINGFACTORX = 114, // Scaling factor x
///
/// Scaling factor y
///
SCALINGFACTORY = 115, // Scaling factor y
// Display driver specific
///
/// Current vertical refresh rate of the display device (for displays only) in Hz
///
VREFRESH = 116, // Current vertical refresh rate of the display device (for displays only) in Hz
///
/// Horizontal width of entire desktop in pixels
///
DESKTOPVERTRES = 117, // Horizontal width of entire desktop in pixels
///
/// Vertical height of entire desktop in pixels
///
DESKTOPHORZRES = 118, // Vertical height of entire desktop in pixels
///
/// Preferred blt alignment
///
BLTALIGNMENT = 119, // Preferred blt alignment
///
/// Shading and blending caps
///
SHADEBLENDCAPS = 120, // Shading and blending caps
///
/// Color Management caps
///
COLORMGMTCAPS = 121, // Color Management caps
}
#endregion
#region public GetDeviceCapsWrapper()
///
/// コンストラクタ
///
public GetDeviceCapsWrapper()
{
}
#endregion
#region public bool Init(string PrinterName)
///
/// 初期化処理
///
/// 情報を取得するプリンタ名を設定
/// true = 取得成功, false = 取得失敗
public bool Init(string PrinterName)
{
if ( PrinterName == null || PrinterName == "" )
{
throw new ArgumentNullException("PrinterName");
}
this._PrinterName = PrinterName;
return this.InitPrinter();
}
#endregion
#region public bool Init(PrinterSettings ps)
///
/// 初期化処理
///
/// 情報を取得するプリンタ設定
/// true = 取得成功, false = 取得失敗
public bool Init(PrinterSettings ps)
{
if ( ps == null )
{
throw new ArgumentNullException("ps");
}
this._PrinterName = ps.PrinterName;
return this.InitPrinter();
}
#endregion
#region private bool InitPrinter()
///
/// プリンタハンドルを取得する
///
/// true=成功, false=失敗
private bool InitPrinter()
{
IntPtr PrinterHandle;
PrinterHandle = CreateDC(@"WINSPOOL", this._PrinterName, null, IntPtr.Zero );
if ( PrinterHandle == IntPtr.Zero )
{
//失敗
return false;
}
else
{
this._PrinterHandle = PrinterHandle;
return true;
}
}
#endregion
#region public int GetData(CommandIndex ci)
///
/// データ取得
///
/// コマンド番号
/// 取得した値
public int GetData(CommandIndex ci)
{
if ( this._PrinterHandle == IntPtr.Zero )
{
throw new Exception("手続きミス");
}
return GetDeviceCaps(this._PrinterHandle, (int)ci);
}
#endregion
#region IDisposable メンバ
///
/// Dispose処理
/// プリンタを開放します。
///
public void Dispose()
{
// TODO: PrinterInfo.Dispose 実装を追加します。
if ( this._PrinterHandle != IntPtr.Zero )
{
DeleteDC(this._PrinterHandle);
}
}
#endregion
}//class
}//namespace