一般不用吧???C#的打印功能可以满足吧?{ PrintDialog pd = new PrintDialog(); pd.PrinterSettings = new PrinterSettings(); pd.Document = new PrintDocument(); if( DialogResult.OK == pd.ShowDialog(this) ) { // Look for the "TestGreg" form string MyString = "TestGreg"; int nIndex = 0; int count = 0;foreach(PaperSize pksize in pd.Document.PrinterSettings.PaperSizes) { if ((String.Compare(MyString, pksize.PaperName)) == 0) nIndex = count; count++; }pd.Document.DefaultPageSettings.PaperSize = pd.Document.PrinterSettings.PaperSizes[nIndex];pd.Document.PrintPage += new PrintPageEventHandler( OnPrintPage ); pd.Document.DocumentName = "Test"; pd.Document.Print(); } }
using System.Runtime.Interc....... [dllimort("user32.dll"] public static extern bool AddForm( IntPtr hPrinter, // handle to printer object UInt32 Level, // data-structure level ref Form_Info_1 pForm // form information buffer );//具体的内容到c++中查一下就有了 public struct Form_Info_1 { }
大家看看我的,为什么不行???using System; using System.Runtime.InteropServices;///Win32API.cs namespace Test { /// <summary> /// Win32API 的摘要说明。 /// </summary> public class Win32API { [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct DEVMODE { public const int CCHDEVICENAME=32; public const int CCHFORMNAME = 32; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=CCHDEVICENAME)] public string dmDeviceName; public int dmSpecVersion; public int dmDriverVersion; public int dmSize; public int dmDriverExtra; public int dmFields; public int dmOrientation; public int dmPaperSize; public int dmPaperLength; public int dmPaperWidth; public int dmScale; public int dmCopies; public int dmDefaultSource; public int dmPrintQuality; public int dmColor; public int dmDuplex; public int dmYResolution; public int dmTTOption; public int dmCollate; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=CCHFORMNAME)] public string dmFormName; public int dmUnusedPadding; public int dmBitsPerPel; public int dmPelsWidth; public int dmPelsHeight; public int dmDisplayFlags; public int dmDisplayFrequency; } [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct PRINTER_DEFAULTS { public string pDatatype; public DEVMODE pDevMode; public int DesiredAccess; } [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct SIZEL { public int cx; public int cy; }
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct RECTL { public int Left; public int Top; public int Right; public int Bottom; } [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct FORM_INFO_1 { public string pName; public SIZEL Size; public RECTL ImageableArea; } [System.Runtime.InteropServices.DllImport("winspool.drv")] public static extern int OpenPrinter(string pPrinterName,ref int phPrinter,[MarshalAs(UnmanagedType.Struct)] ref PRINTER_DEFAULTS pDefault);
[System.Runtime.InteropServices.DllImport("winspool.drv")] public static extern int ClosePrinter(int hPrinter); [System.Runtime.InteropServices.DllImport("winspool.drv")] public static extern int AddForm(int hPrinter,int Level,System.IntPtr pForm); // [ DllImport( // "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true, // CallingConvention=CallingConvention.StdCall )]
[DllImport("winspool.drv")] public static extern int AddForm(int hPrinter,int Level,byte pForm); 下载这个软件帮你在C#/VB.net 声明API http://www.freevbcode.com/ShowCode.Asp?ID=3639
To fupip(小贝): 我的代码就是用那个apiviewer声明的,不过还是不能用。 public static extern int AddForm(int hPrinter,int Level,byte pForm);中的byte pForm是不对的,应该用指针或ref
我都说好几次了,api view 垃圾我才自己写个api explorer 你们为什么不用呢?用我的api explorer 肯定没这问题[DllImport("winspool.drv", EntryPoint="AddForm")] public static extern int AddForm ( int hPrinter, int Level, ref byte pForm );download from: http://zpcity.com/arli
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
pd.Document = new PrintDocument();
if( DialogResult.OK == pd.ShowDialog(this) )
{
// Look for the "TestGreg" form
string MyString = "TestGreg";
int nIndex = 0;
int count = 0;foreach(PaperSize pksize in
pd.Document.PrinterSettings.PaperSizes)
{
if ((String.Compare(MyString, pksize.PaperName)) == 0)
nIndex = count;
count++;
}pd.Document.DefaultPageSettings.PaperSize =
pd.Document.PrinterSettings.PaperSizes[nIndex];pd.Document.PrintPage += new PrintPageEventHandler(
OnPrintPage );
pd.Document.DocumentName = "Test";
pd.Document.Print();
}
}
[dllimort("user32.dll"]
public static extern bool AddForm(
IntPtr hPrinter, // handle to printer object
UInt32 Level, // data-structure level
ref Form_Info_1 pForm // form information buffer
);//具体的内容到c++中查一下就有了
public struct Form_Info_1
{
}
using System.Runtime.InteropServices;///Win32API.cs
namespace Test
{
/// <summary>
/// Win32API 的摘要说明。
/// </summary>
public class Win32API
{
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct DEVMODE
{
public const int CCHDEVICENAME=32;
public const int CCHFORMNAME = 32; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=CCHDEVICENAME)]
public string dmDeviceName;
public int dmSpecVersion;
public int dmDriverVersion;
public int dmSize;
public int dmDriverExtra;
public int dmFields;
public int dmOrientation;
public int dmPaperSize;
public int dmPaperLength;
public int dmPaperWidth;
public int dmScale;
public int dmCopies;
public int dmDefaultSource;
public int dmPrintQuality;
public int dmColor;
public int dmDuplex;
public int dmYResolution;
public int dmTTOption;
public int dmCollate;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=CCHFORMNAME)]
public string dmFormName;
public int dmUnusedPadding;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
} [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct PRINTER_DEFAULTS
{
public string pDatatype;
public DEVMODE pDevMode;
public int DesiredAccess;
} [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct SIZEL
{
public int cx;
public int cy;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct RECTL
{
public int Left;
public int Top;
public int Right;
public int Bottom;
} [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct FORM_INFO_1
{
public string pName;
public SIZEL Size;
public RECTL ImageableArea;
} [System.Runtime.InteropServices.DllImport("winspool.drv")]
public static extern int OpenPrinter(string pPrinterName,ref int phPrinter,[MarshalAs(UnmanagedType.Struct)] ref PRINTER_DEFAULTS pDefault);
[System.Runtime.InteropServices.DllImport("winspool.drv")]
public static extern int ClosePrinter(int hPrinter); [System.Runtime.InteropServices.DllImport("winspool.drv")]
public static extern int AddForm(int hPrinter,int Level,System.IntPtr pForm);
// [ DllImport(
// "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
// CallingConvention=CallingConvention.StdCall )]
public Win32API()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
}///调用部分
int lPrinter=0;
Test.Win32API.PRINTER_DEFAULTS myPRINTER_DEFAULTS=new Test.Win32API.PRINTER_DEFAULTS();
Test.Win32API.FORM_INFO_1 myFORM_INFO=new Test.Win32API.FORM_INFO_1(); Test.Win32API.OpenPrinter("EPSON LQ-1600KIII",ref lPrinter,ref myPRINTER_DEFAULTS);
myFORM_INFO.pName="Test";
myFORM_INFO.Size.cx=380000;
myFORM_INFO.Size.cy=280000;
myFORM_INFO.ImageableArea.Left=0;
myFORM_INFO.ImageableArea.Top=0;
myFORM_INFO.ImageableArea.Right=0;
myFORM_INFO.ImageableArea.Bottom=0;
System.IntPtr myIntPtr=new System.IntPtr(0x0);
System.Runtime.InteropServices.Marshal.StructureToPtr(myFORM_INFO,myIntPtr,true);
Test.Win32API.AddForm(lPrinter,1,myIntPtr);
Test.Win32API.ClosePrinter(lPrinter);
public static extern int AddForm(int hPrinter,int Level,byte pForm);
下载这个软件帮你在C#/VB.net 声明API
http://www.freevbcode.com/ShowCode.Asp?ID=3639
我的代码就是用那个apiviewer声明的,不过还是不能用。
public static extern int AddForm(int hPrinter,int Level,byte pForm);中的byte pForm是不对的,应该用指针或ref
public static extern int AddForm (
int hPrinter,
int Level,
ref byte pForm
);download from: http://zpcity.com/arli