我正在为 C# 中的标签打印机使用打印机 SDK,可以在此处找到详细信息:
https://stackoverflow.com/questions/18083309/getting-a-printer-api-to-work-with-c-sharp
接受那里给出的建议,我使用 PInvoke 让 DLL 中的函数工作,令我惊讶的是,这一切都开始融合在一起……主要是。
函数 SlpDrawTextXY() 应该能够为 Hfont 类型的字体获取参数。这可以由一个名为 SlpCreateFont() 的函数创建。(这些方法的详细信息分别在文档的第 21 页和第 19 页)。
现在,我发现 Hfont 到底是什么的探索很糟糕。MSDN 提到了一点,但并没有真正告诉我它到底是什么。如果您是盲人,提供的文章并不是真正有用,而且绝对更适合已经半路的人。关于它的其他文档非常少,我只能猜测到底应该发生什么。
我有一个看起来像这样的代码块:
public partial class Form1 : Form
{
[DllImport("SlpApi7x32.dll")]
static extern void SlpDebugMode(int nMode);
[DllImport("SlpApi7x32.dll")]
static extern int SlpOpenPrinter(String strPrinterName, int nID, bool fPortrait);
[DllImport("SlpApi7x32.dll")]
static extern void SlpClosePrinter();
[DllImport("SlpApi7x32.dll")]
static extern bool SlpStartLabel();
[DllImport("SlpApi7x32.dll")]
static extern void SlpDrawTextXY(int x, int y, Font iFont, String lpText);
[DllImport("SlpApi7x32.dll")]
static extern bool SlpEndLabel();
[DllImport("SlpApi7x32.dll")]
static extern Font SlpCreateFont(String lpName, int nPoints, int nAttributes);
[DllImport("GDI32.dll")]
public static extern bool DeleteObject(IntPtr objectHandle);
public Form1()
{
InitializeComponent();
}
private void print_Click(object sender, EventArgs e)
{
//Font myFont = new Font("Arial", 12);
//IntPtr hFont = myFont.ToHfont();
SlpDebugMode(2);
SlpOpenPrinter("Smart Label Printer 440", 1, false);
{
SlpStartLabel();
//Font font = SlpCreateFont("Courier", 12, 0);
SlpDrawTextXY(30, 30, null, "Hello World!");
SlpEndLabel();
}
SlpClosePrinter();
}
}
我玩弄的一些残余已被注释掉。如果它被注释掉,它就不起作用。
这段代码实际上会进入打印机并“打印”一个空白标签,所以看起来我真的很接近了。SlpDrawTextXY 中的第三个参数是字体应该在的位置,我将它设置为“null”只是为了看看我是否可以成功通过它。此代码基于第 12 页文档中的示例 C 代码。我希望能够将此代码转换为实际打印文本的内容。