从.NET打印API检索设备上下文 [英] Retrieving Device Context from .NET print API
问题描述
我正在使用.NET应用程序来控制DataCard桌面证卡打印机,但我偶然发现了一个问题.为了执行磁条和芯片编码,我必须在本机打印机API dll中调用一个函数以启用打印机的交互模式.
I am working on a .NET application to control DataCard Desktop Card printers, and I have stumbled upon a problem. In order to execute Magnetic Stripe and Chip encoding, I have to call a function in a native printer API dll to enable the printers Interactive mode.
我设法创建了P/Invoke代码来调用此本机函数.但是问题在于,该方法将打印机的设备上下文句柄作为参数,并且必须在调用Gdi32 StartDoc函数之前被调用.
I have managed to create the P/Invoke code to call this native function. But the problem is that this method takes the printer Device Context handle as a parameter, and have to be called BEFORE the Gdi32 StartDoc function is called.
在.NET打印API中,我确实可以从Graphics对象访问hDC.但据我所知,只有在调用StartDoc函数之后,才可以使用Graphics对象.
In the .NET printing API I do have access to the hDC from the Graphics object. But as far as I can see the Graphics object is only availible AFTER the StartDoc function is invoked.
所以我的问题是在调用StartDoc之前是否有人知道一种检索上下文的方法?
So my question is if anyone knows a way to retrieve the context before StartDoc is called?
推荐答案
您可以执行OpenPrinter
来检索打印机句柄(HANDLE
),然后通过传入该句柄以获取打印机DC来调用CreateDC
.随时(在StartDoc
之前).
You can do a OpenPrinter
to retrieve a printer handle (HANDLE
) and then call CreateDC
by passing in this handle to get a printer DC anytime (before StartDoc
).
这篇关于从.NET打印API检索设备上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!