135-1821-9792

vb.net获取印机 vb获取打印机列表

vb.net 获取打印机列表

Public Function EnumPrinters()

成都创新互联专业为企业提供剑阁网站建设、剑阁做网站、剑阁网站设计、剑阁网站制作等企业网站建设、网页设计与制作、剑阁企业网站模板建站服务,十年剑阁做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

Dim PrinterSQL As String =

"SELECT * FROM Win32_Printer"

Dim

printers As Management.ManagementObjectCollection = New

Management.ManagementObjectSearcher(PrinterSQL).Get()

Dim printer As

Management.ManagementObject

For

Each printer In

printers

Dim pde As Management.PropertyDataCollection.PropertyDataEnumerator =

printer.Properties.GetEnumerator

While

(pde.MoveNext)

Try

If pde.Current.Name = "DriverName"

Then

combobox1.Items.Add(pde.Current.Value.ToString)

End

If

Catch ex As

Exception

MessageBox.Show(ex.ToString)

End

Try

End

While

Next

End Function

VB.NET中怎么获取当前打印机的纸张类型

我是VB.NET 2010版本

窗体上添加一个按钮,代码如下:

Imports System.Drawing.Printing

Public Class Form1

'采用关键字WithEvents声明一个PrintDocument对象,会引发它的PrintPage事件

Private WithEvents My_PrintDocument As PrintDocument

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'实例化PrintDocument对象

My_PrintDocument = New PrintDocument

'显示默认的打印机名称

MsgBox(My_PrintDocument.PrinterSettings.PrinterName)

'显示默认的打印机纸张类型名称

MsgBox(My_PrintDocument.DefaultPageSettings.PaperSize.ToString)

End Sub

End Class

用VB.Net 编程时如何获得打印机的物理边距

网络上找到这个比较全面,你试一下,我记得以前也是这么设置的。

int GetDeviceCaps( HDC hdc, int nIndex);

其中,hdc用来指定设备环境句柄,nIndex用来指定要获取的参量索引,对于打印机而言,它常常需要下列的预定义值:

LOGPIXELSX 打印机水平分辨率

LOGPIXELSY 打印机垂直分辨率

PHYSICALWIDTH 打印纸的实际宽度

PHYSICALHEIGHT 打印纸的实际高度

PHYSICALOFFSETX 实际可打印区域的物理左边距

PHYSICALOFFSETY 实际可打印区域的物理上边距

下面的函数代码就是用来设置页边距,并且还计算页面的物理边距:

void CEx_Prn1View::SetPageMargin(CDC *pDC, CPrintInfo *pInfo, int l, int t, int r, int b)

// l, t, r, b分别表示左上右下边距, 单位为0.1mm

{

int nOldMode = pDC- GetMapMode();

pDC- SetMapMode(MM_LOMETRIC);

// 计算一个设备单位等于多少0.1mm

double scaleX = 254.0 / (double)GetDeviceCaps(

pDC- m_hAttribDC, LOGPIXELSX);

double scaleY = 254.0 / (double)GetDeviceCaps(

pDC- m_hAttribDC, LOGPIXELSY);

int x = GetDeviceCaps(pDC- m_hAttribDC,

PHYSICALOFFSETX);

int y = GetDeviceCaps(pDC- m_hAttribDC,

PHYSICALOFFSETY);

int w = GetDeviceCaps(pDC- m_hAttribDC,

PHYSICALWIDTH);

int h = GetDeviceCaps(pDC- m_hAttribDC,

PHYSICALHEIGHT);

int nPageWidth = (int)((double)w*scaleX + 0.5);

// 纸宽,单位0.1mm

int nPageHeight = (int)((double)h*scaleY + 0.5);

// 纸高,单位0.1mm

m_nPhyLeft = (int)((double)x*scaleX + 0.5);

// 物理左边距,单位0.1mm

m_nPhyTop = (int)((double)y*scaleY + 0.5);

// 物理上边距,单位0.1mm

pDC- DPtoLP(pInfo- m_rectDraw);

CRect rcTemp = pInfo- m_rectDraw;

rcTemp.NormalizeRect();

m_nPhyRight = nPageWidth - rcTemp.Width() -

m_nPhyLeft; // 物理右边距,单位0.1mm

m_nPhyBottom = nPageHeight - rcTemp.Height() -

m_nPhyTop; // 物理下边距,单位0.1mm

// 若边距小于物理边距,则调整它们

if (l m_nPhyLeft) l = m_nPhyLeft;

if (t m_nPhyTop) t = m_nPhyTop;

if (r m_nPhyRight) r = m_nPhyRight;

if (b m_nPhyBottom) b = m_nPhyBottom;

// 计算并调整pInfo- m_rectDraw的大小

pInfo- m_rectDraw.left = l - m_nPhyLeft;

pInfo- m_rectDraw.top = - t + m_nPhyTop;

pInfo- m_rectDraw.right -= r - m_nPhyRight;

pInfo- m_rectDraw.bottom += b - m_nPhyBottom;

pDC- LPtoDP(pInfo- m_rectDraw);

pDC- SetMapMode(nOldMode);

// 恢复原来的映射模式

}

VB.NET或C#如何调用某个打印机(例如"Microsoft XPS Document Writer")的“打印首选项”?

实现打印功能的核心是PrintDocument类这个类属于System.Drawing.Printing名字空间这个类封装了当前的打印设置页面设置以及所

有的与打印有关的事件和方法

这个类包括以下几个属性 事件 和方法

1、PrinterSettings 属性

存放打印机的设置信息这个属性不需要程序员设置因为它是由打印对话框获取的

2、PrintCountroller 属性

控制打印过程

3、DefaultPageSettings 属性

存放页面设置信息 打印纸大小方向等也不需要程序员设置因为它是由页面设置对话框获取的

4、DocumentName 属性

指定文档名称,出现在打印机状态窗口中

1。 BeginPrint事件

在打印之前发出

2. PrintPage事件

每打印一页是发出,事件接受一个PrintPageEventArgs参数该参数封装了打印相关的信息

PrintPageEventArgs参数有很多重要的属性

1 Cancel 取消打印

2 Graphics 页面的绘图对象

3 HasMorePages 是否还有要打印的页面

Print 方法 该方法没有参数 调用它将按照当前设置开始打印

若实现打印功能首先构造PrintDocument对象添加打印事件

PrintDocument printDocument;

private void InitializeComponent()

{

...

printDocument=new PrintDocument();

printDocument.PrintPage += new PrintPageEventHandler (this.printDocument_PrintPage);

...

}

实现打印事件功能

打印和绘图类似都是调用Graphics 类的方法进行画图 不同的是一个在显示器上一个在打印纸上并且打印要进行一些复杂的计算

如换行 分页等。

private void printDocument_PrintPage(object sender,PrintPageEventArgs e)

{

StringReader lineReader = new StringReader(textBox.Text);

Graphics g = e.Graphics; //获得绘图对象

float linesPerPage = 0; //页面的行号

float yPosition = 0; //绘制字符串的纵向位置

int count = 0; //行计数器

float leftMargin = e.MarginBounds.Left; //左边距

float topMargin = e.MarginBounds.Top; //上边距

string line = null; 行字符串

Font printFont = this.textBox.Font; //当前的打印字体

SolidBrush myBrush = new SolidBrush(Color.Black);//刷子

linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每页可打印的行数

//逐行的循环打印一页

while(count linesPerPage ((line=lineReader.ReadLine()) != null))

{

yPosition = topMargin + (count * printFont.GetHeight(g));

g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());

count++;

}

如果本页打印完成而line不为空说明还有没完成的页面这将触发下一次的打印事件在下一次的打印中lineReader会

自动读取上次没有打印完的内容因为lineReader是这个打印方法外的类的成员它可以记录当前读取的位置

if(line != null)

e.HasMorePages = true;

else

e.HasMorePages = false;

}

打印设置,构造打印对话框 将对话框中设置的Document属性赋给printDocument这样会将用户的设置自动保存到printDocument

的PrinterSettings属性中

protected void FileMenuItem_PrintSet_Click(object sender,EventArgs e)

{

PrintDialog printDialog = new PrintDialog();

printDialog.Document = printDocument;

printDialog.ShowDialog();

}

页面设置和打印预览与打印设置原理相同都是构造对话框将用户在对话框中的设置保存到相应的类的属性中

protected void FileMenuItem_PageSet_Click(object sender,EventArgs e)

{

PageSetupDialog pageSetupDialog = new PageSetupDialog();

pageSetupDialog.Document = printDocument;

pageSetupDialog.ShowDialog();

}

打印预览

protected void FileMenuItem_PrintView_Click(object sender,EventArgs e)

{

PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();

printPreviewDialog.Document = printDocument;

try

{

printPreviewDialog.ShowDialog();

}

catch(Exception excep)

{

MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

打印就可以直接调用printDocument的Print()方法因为用户可能在打印之前还要再更改打印设置所以

在这里再次显示打印设置对话框

protected void FileMenuItem_Print_Click(object sender,EventArgs e)

{

PrintDialog printDialog = new PrintDialog();

printDialog.Document = printDocument;

lineReader = new StringReader(textBox.Text);

if (printDialog.ShowDialog() == DialogResult.OK)

{

try

{

printDocument.Print();

}

catch(Exception excep)

{

MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);

printDocument.PrintController.OnEndPrint(printDocument,new PrintEventArgs());

}

}

}

总结打印的过程是

1 在应用程序窗体初始化时构造PrintDocument对象 添加 printDocument 的 PrintPage 方法

2 实现PrintPage方法 4 在用户的单击事件中调用 printDocument 的 Print方法实现打印功能

在这中间可能要用到 PrintDialog PrintPreviewDialog PageSetupDialog 设置和查看打印效


名称栏目:vb.net获取印机 vb获取打印机列表
链接地址:http://kswsj.com/article/hiipig.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号