怎么用C#语言调用打印机!~

我想开发一个。net应用程序!~想问下怎么调用打印机,以及怎么控制他打印出来的样式!~有原代码最好不过啦,提意见也好!~谢啦!~... 我想开发一个。net 应用程序!~ 想问下怎么调用打印机,以及怎么控制他打印出来的样式!~ 有原代码最好不过啦,提意见也好!~ 谢啦!~ 展开
 我来答
匿名用户
2013-07-18
展开全部
C#调用WIN32API系列二列举局网内共享打印机C#通过调用WIN32API可以实现非常强大的功能,本文将着重讲述如何通过调用WIN32API实现列举局域网络内所有共享的打印机。首先我们看看EnumPrinters函数的定义BOOL EnumPrinters(Dword Flags, // printer object typesLPTSTR Name, // name of printer objectDword Level, // information levelLPBYTE pPrinterEnum, // printer information bufferDword cbBuf, // size of printer information bufferLPDword pcbNeeded, // bytes received or requiredLPDword pcReturned // number of printers enumerated);这个api有几个返回参数, 其中最重要的是pPrinterEnum所指的缓冲区中,是一个PRINTER_INFO_N的结构数组, 这里N根据Level参数而变化, 这里我们用的是1, 所以用到的结构是typedef struct _PRINTER_INFO_1 { Dword Flags; LPTSTR pDescription; LPTSTR pName; LPTSTR pComment; } PRINTER_INFO_1C#要调用API首先要引入动态库,EnumPrinters在winspool.drv这个动态库中。引入语句如下[DllImport("winspool.drv", CharSet=CharSet.Auto)]然后是定义PRINTER_INFO_1结构[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]struct PRINTER_INFO_1{int flags;[MarshalAs(UnmanagedType.LPTStr)]public string pDescription;[MarshalAs(UnmanagedType.LPTStr)]public string pName;[MarshalAs(UnmanagedType.LPTStr)]public string pComment;}好了,全部的源代码如下:using System;using System.Collections;using System.Runtime.InteropServices;using System.Diagnostics;using System.Drawing.Printing;public class QuickTest {[DllImport("winspool.drv", CharSet=CharSet.Auto)]static extern bool EnumPrinters(int flags, string name, int level, IntPtr pPrinterEnum,int cbBuf, out int pcbNeeded, out int pcReturned);private const int PRINTER_ENUM_NETWORK = 0x00000040;private const int PRINTER_ENUM_LOCAL = 0x00000002;private const int PRINTER_ENUM_REMOTE = 0x00000010;[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]struct PRINTER_INFO_1{int flags;[MarshalAs(UnmanagedType.LPTStr)]public string pDescription;[MarshalAs(UnmanagedType.LPTStr)]public string pName;[MarshalAs(UnmanagedType.LPTStr)]public string pComment;}public void EnumeratePrintersWin() {bool Success;int cbRequired;int nEntries;IntPtr outb = IntPtr.Zero; Success = EnumPrinters(PRINTER_ENUM_NETWORK | PRINTER_ENUM_LOCAL | PRINTER_ENUM_REMOTE, null , 1, outb, 0, out cbRequired, out nEntries);outb = Marshal.AllocHGlobal(cbRequired);Success = EnumPrinters(PRINTER_ENUM_NETWORK | PRINTER_ENUM_LOCAL | PRINTER_ENUM_REMOTE, null , 1, outb, cbRequired, out cbRequired, out nEntries);PRINTER_INFO_1[] portsArray = new PRINTER_INFO_1[cbRequired];IntPtr current = outb;try {for (int i=0; i<portsArray.Length; i++){portsArray[i] = (PRINTER_INFO_1) Marshal.PtrToStructure(current,typeof(PRINTER_INFO_1));current=(IntPtr)((int)current+Marshal.SizeOf(typeof(PRINTER_INFO_1)));Console.WriteLine(i+": \n"+portsArray[i].pName+"\n"+portsArray[i].pDescription+"\n"+portsArray[i].pComment+"\n");} }catch (Exception) {//Console.WriteLine(eXP.StackTrace);}Marshal.FreeHGlobal(outb);}public QuickTest () {}public static void Main() {QuickTest qt = new QuickTest();qt.EnumeratePrintersWin();}}
匿名用户
2013-07-18
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;namespace qwe
{
public partial class Form1 : Form
{
static Font printFont; //打印使用的字体

public Form1()
{
InitializeComponent();
} //void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
//{
// EAN13.Paint_EAN13("123456789012", e.Graphics, e.MarginBounds);
// e.HasMorePages = false;
//}
public static void PrintEventPage(object sender, PrintPageEventArgs e)
{ float yPos = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
//string line = null;
//string subs = " ";
string context = null;
//打印字体
printFont = new Font("宋体", 14F, FontStyle.Bold | FontStyle.Underline);
//打印内容
context = "收银小票";
//打印的位置
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
e.Graphics.DrawString(context, printFont, Brushes.Black,
50, 10, new StringFormat());
//换行
count++; e.HasMorePages = false;
}

private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintEventPage); PrintDialog1.AllowSomePages = true;
PrintDialog1.ShowHelp = true; PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
PrintController printController = new StandardPrintController();
docToPrint.PrintController = printController;
DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框 //If the result is OK then print the document.
if (result == DialogResult.OK)
{
docToPrint.Print();//开始打印
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-07-18
展开全部
这个问题确实很烦恼。呵呵。因为介绍如何调用打印机的书籍或者资料很少。几乎没有。英文的资料有,看英文的吧,我以前就是这么干的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-07-18
展开全部
哇,没那么麻烦,,,.net直接提供打印控件。而且有好几个呢
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式