システムフォーム定義をプログラムで追加する方法は、AddForm
を呼び出すことです。私が知っているこの呼び出しのための良いラッパーはありませんが、AddForm
にP/Invokingが動作します。私は速いラッパーとposted it on GitHubを書きました。ラッパーを使用
例:
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
PS C:\Drop> Import-Module .\PowershellPrinterFormsModule.dll
PS C:\Drop> Add-SystemForm -Name 'Demo User Form try 1' -Units Inches -Size '4,5'
PS C:\Drop> Add-SystemForm -Name 'Demo User Form try 2' -Units Inches -Size '4,5' -Margin '0.25,0.5'
PS C:\Drop> Add-SystemForm -Name 'Demo User Form try 3' -Units Millimeters -Size '80,50' -Margin '10,10,0,0'
実際のP/AddForm
へのコール呼び出し:たぶん
SafePrinterHandle hServer;
if (!OpenPrinter(null, out hServer, IntPtr.Zero))
{
throw new Win32Exception();
}
using (hServer)
{
var form = new FORM_INFO_1()
{
Flags = 0,
Name = this.Name,
Size = (SIZEL)pageSize,
ImageableArea = (RECTL)imageableArea
};
if (!AddForm(hServer, 1, ref form))
{
throw new Win32Exception();
}
}
internal static class NativeMethods
{
#region Constants
internal const int ERROR_INSUFFICIENT_BUFFER = 0x7A;
#endregion
#region winspool.drv
private const string Winspool = "winspool.drv";
[DllImport(Winspool, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool OpenPrinter(string szPrinter, out SafePrinterHandle hPrinter, IntPtr pd);
public static SafePrinterHandle OpenPrinter(string szPrinter)
{
SafePrinterHandle hServer;
if (!OpenPrinter(null, out hServer, IntPtr.Zero))
{
throw new Win32Exception();
}
return hServer;
}
[DllImport(Winspool, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport(Winspool, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool EnumForms(SafePrinterHandle hPrinter, int level, IntPtr pBuf, int cbBuf, out int pcbNeeded, out int pcReturned);
[DllImport(Winspool, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool AddForm(SafePrinterHandle hPrinter, int level, [In] ref FORM_INFO_1 form);
[DllImport(Winspool, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool DeleteForm(SafePrinterHandle hPrinter, string formName);
#endregion
#region Structs
[StructLayout(LayoutKind.Sequential)]
internal struct FORM_INFO_1
{
public int Flags;
[MarshalAs(UnmanagedType.LPWStr)]
public string Name;
public SIZEL Size;
public RECTL ImageableArea;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SIZEL
{
public int cx;
public int cy;
public static explicit operator SIZEL(Size r)
=> new SIZEL { cx = (int)r.Width, cy = (int)r.Height };
public static explicit operator Size(SIZEL r)
=> new Size(r.cx, r.cy);
}
[StructLayout(LayoutKind.Sequential)]
internal struct RECTL
{
public int left;
public int top;
public int right;
public int bottom;
public static explicit operator RECTL(Rect r)
=> new RECTL { left = (int)r.Left, top = (int)r.Top, right = (int)r.Right, bottom = (int)r.Bottom };
public static explicit operator Rect(RECTL r)
=> new Rect(new Point(r.left, r.top), new Point(r.right, r.bottom));
}
#endregion
}
internal sealed class SafePrinterHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public SafePrinterHandle()
: base(true)
{
}
protected override bool ReleaseHandle()
{
return NativeMethods.ClosePrinter(handle);
}
}
を[本](https://social.technet.microsoft.com/Forums/azure/en-US/5784736a-a4f1-41cf-9767-ed7b993557a2/add-and-delete-custom-printer-forms-using-powershell?forum = ITCG)が役立ちます。ベンダー固有のようです。 – DarkLite1