中の技術日誌
 ホーム / 上へ

わんくまライブラリ Wankuma.Interop.GetDeviceCapsWrapperクラス Version1

2005/01/25

この文書はVisual Studio 2003(.NET1.1)をベースに記述されています。それ以降のバージョンや、あなたが読んでいる時点では変更されている可能性があります。
またバージョンアップされている場合にはなんらかかの不具合を含んでいる可能性があります。

ドキュメントへ
Wankuma.Interop.GetDeviceCapsWrapper1d.htm

ソースファイル直接ダウンロードへ
Wankuma.Interop.GetDeviceCapsWrapper1c.txt

利用規約へ
../kiyaku.htm

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Wankuma.Interop
{
  /// <summary>
  /// PrinterInfo の概要の説明です。
  /// </summary>
  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 メンバ
    /// <summary>
    /// プリンタハンドル
    /// </summary>
    private IntPtr _PrinterHandle;

    /// <summary>
    /// プリンタ名
    /// </summary>
    private string _PrinterName = null;
    #endregion
    #region public enum CommandIndex
    /// <summary>
    /// データ取得用コマンド番号
    /// </summary>
    public enum CommandIndex
    {
      /// <summary>
      /// Device driver version
      /// </summary>
      DRIVERVERSION = 0,     // Device driver version
      /// <summary>
      /// Device classification
      /// </summary>
      TECHNOLOGY    = 2,     // Device classification
      /// <summary>
      /// Horizontal size in millimeters
      /// </summary>
      HORZSIZE      = 4,     // Horizontal size in millimeters
      /// <summary>
      /// Vertical size in millimeters
      /// </summary>
      VERTSIZE      = 6,     // Vertical size in millimeters
      /// <summary>
      /// Horizontal width in pixels
      /// </summary>
      HORZRES       = 8,     // Horizontal width in pixels
      /// <summary>
      /// Vertical height in pixels
      /// </summary>
      VERTRES       = 10,    // Vertical height in pixels
      /// <summary>
      /// Number of bits per pixel
      /// </summary>
      BITSPIXEL     = 12,    // Number of bits per pixel
      /// <summary>
      /// Number of planes
      /// </summary>
      PLANES        = 14,    // Number of planes
      /// <summary>
      /// Number of brushes the device has
      /// </summary>
      NUMBRUSHES    = 16,    // Number of brushes the device has
      /// <summary>
      /// Number of pens the device has
      /// </summary>
      NUMPENS       = 18,    // Number of pens the device has
      /// <summary>
      /// Number of markers the device has
      /// </summary>
      NUMMARKERS    = 20,    // Number of markers the device has
      /// <summary>
      /// Number of fonts the device has
      /// </summary>
      NUMFONTS      = 22,    // Number of fonts the device has
      /// <summary>
      /// Number of colors the device supports
      /// </summary>
      NUMCOLORS     = 24,    // Number of colors the device supports
      /// <summary>
      /// Size required for device descriptor
      /// </summary>
      PDEVICESIZE   = 26,    // Size required for device descriptor
      /// <summary>
      /// Curve capabilities
      /// </summary>
      CURVECAPS     = 28,    // Curve capabilities
      /// <summary>
      /// Line capabilities
      /// </summary>
      LINECAPS      = 30,    // Line capabilities
      /// <summary>
      /// Polygonal capabilities
      /// </summary>
      POLYGONALCAPS = 32,    // Polygonal capabilities
      /// <summary>
      /// Text capabilities
      /// </summary>
      TEXTCAPS      = 34,    // Text capabilities
      /// <summary>
      /// Clipping capabilities
      /// </summary>
      CLIPCAPS      = 36,    // Clipping capabilities
      /// <summary>
      /// Bitblt capabilities
      /// </summary>
      RASTERCAPS    = 38,    // Bitblt capabilities
      /// <summary>
      /// Length of the X leg
      /// </summary>
      ASPECTX       = 40,    // Length of the X leg
      /// <summary>
      /// Length of the Y leg
      /// </summary>
      ASPECTY       = 42,    // Length of the Y leg
      /// <summary>
      /// Length of the hypotenuse
      /// </summary>
      ASPECTXY      = 44,    // Length of the hypotenuse
      /// <summary>
      /// Logical pixels/inch in X
      /// </summary>
      LOGPIXELSX    = 88,    // Logical pixels/inch in X
      /// <summary>
      /// Logical pixels/inch in Y
      /// </summary>
      LOGPIXELSY    = 90,    // Logical pixels/inch in Y
      /// <summary>
      /// Number of entries in physical palette
      /// </summary>
      SIZEPALETTE  = 104,    // Number of entries in physical palette
      /// <summary>
      /// Number of reserved entries in palette
      /// </summary>
      NUMRESERVED  = 106,    // Number of reserved entries in palette
      /// <summary>
      /// Actual color resolution
      /// </summary>
      COLORRES     = 108,    // Actual color resolution
      // Printing related DeviceCaps. These replace the appropriate Escapes
      /// <summary>
      /// Physical Width in device units
      /// </summary>
      PHYSICALWIDTH   = 110, // Physical Width in device units
      /// <summary>
      /// Physical Height in device units
      /// </summary>
      PHYSICALHEIGHT  = 111, // Physical Height in device units
      /// <summary>
      /// Physical Printable Area x margin
      /// </summary>
      PHYSICALOFFSETX = 112, // Physical Printable Area x margin
      /// <summary>
      /// Physical Printable Area y margin
      /// </summary>
      PHYSICALOFFSETY = 113, // Physical Printable Area y margin
      /// <summary>
      /// Scaling factor x
      /// </summary>
      SCALINGFACTORX  = 114, // Scaling factor x
      /// <summary>
      /// Scaling factor y
      /// </summary>
      SCALINGFACTORY  = 115, // Scaling factor y

      // Display driver specific

      /// <summary>
      /// Current vertical refresh rate of the display device (for displays only) in Hz
      /// </summary>
      VREFRESH        = 116,  // Current vertical refresh rate of the display device (for displays only) in Hz
      /// <summary>
      /// Horizontal width of entire desktop in pixels
      /// </summary>
      DESKTOPVERTRES  = 117,  // Horizontal width of entire desktop in pixels
      /// <summary>
      /// Vertical height of entire desktop in pixels
      /// </summary>
      DESKTOPHORZRES  = 118,  // Vertical height of entire desktop in pixels
      /// <summary>
      /// Preferred blt alignment
      /// </summary>
      BLTALIGNMENT    = 119,  // Preferred blt alignment

      /// <summary>
      /// Shading and blending caps
      /// </summary>
      SHADEBLENDCAPS  = 120,  // Shading and blending caps
      /// <summary>
      /// Color Management caps
      /// </summary>
      COLORMGMTCAPS   = 121,  // Color Management caps
    }

    #endregion
    #region public GetDeviceCapsWrapper()
    /// <summary>
    /// コンストラクタ
    /// </summary>
    public GetDeviceCapsWrapper()
    {
    }
    #endregion
    #region public bool Init(string PrinterName)
    /// <summary>
    /// 初期化処理
    /// </summary>
    /// <param name="PrinterName">情報を取得するプリンタ名を設定</param>
    /// <returns>true = 取得成功, false = 取得失敗</returns>
    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)
    /// <summary>
    /// 初期化処理
    /// </summary>
    /// <param name="ps">情報を取得するプリンタ設定</param>
    /// <returns>true = 取得成功, false = 取得失敗</returns>
    public bool Init(PrinterSettings ps)
    {
      if ( ps == null )
      {
        throw new ArgumentNullException("ps");
      }

      this._PrinterName = ps.PrinterName;
      return this.InitPrinter();
    }
    #endregion
    #region private bool InitPrinter()
    /// <summary>
    /// プリンタハンドルを取得する
    /// </summary>
    /// <returns>true=成功, false=失敗</returns>
    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)
    /// <summary>
    /// データ取得
    /// </summary>
    /// <param name="ci">コマンド番号</param>
    /// <returns>取得した値</returns>
    public int GetData(CommandIndex ci)
    {
      if ( this._PrinterHandle == IntPtr.Zero )
      {
        throw new Exception("手続きミス");
      }
      return GetDeviceCaps(this._PrinterHandle, (int)ci);
    }
    #endregion
    #region IDisposable メンバ

    /// <summary>
    /// Dispose処理
    /// プリンタを開放します。
    /// </summary>
    public void Dispose()
    {
      // TODO:  PrinterInfo.Dispose 実装を追加します。
      if ( this._PrinterHandle != IntPtr.Zero )
      {
        DeleteDC(this._PrinterHandle);
      }
    }

    #endregion
  }//class
}//namespace

勉強会: 05/18 大阪 登録受付中

中の技術日誌
コンテンツ
わんくま同盟
わんくま同盟
わんくま同盟
広告
バナー
MVP LOGO
MSMVP Visual C# Since 2004/04-2007/03
MCP LOGO
070-316
姉妹サイト
姉妹サイト:じゃんぬのC#, VB.NET 入門
じゃんぬの
C#, VB.NET 入門
検索
Google

ブログ本家
広告