0

我正在为 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 代码。我希望能够将此代码转换为实际打印文本的内容。

4

2 回答 2

2
[DllImport("SlpApi7x32.dll")]
static extern Font SlpCreateFont(...)

这里使用Font不正确。SlpCreateFont() 返回一个 HFONT,一个“字体句柄”。这是在非托管代码中创建字体时操作字体的方式。并且与您从Font.ToHfont()方法返回的动物完全相同。所以你必须以 ToHfont() 返回它的方式声明它,它必须IntPtr在你的声明中。相应地更新其他声明。

请注意,您将有一些不错的机会可以使用 Font.ToHfont() 而不是 SlpCreateFont()。规则是相同的,但是,您必须确保在使用完字体后调用 DeleteObject(),否则您将泄漏 GDI 对象,最终会使您的代码崩溃。

于 2013-08-06T19:42:50.347 回答
0

汉斯完全正确。为了将来参考,下面是工作代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SSLP
{
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, IntPtr iFont, String lpText);

    [DllImport("SlpApi7x32.dll")]
    static extern bool SlpEndLabel();

    [DllImport("SlpApi7x32.dll")]
    static extern IntPtr SlpCreateFont(String lpName, int nPoints, int nAttributes);

    [DllImport("GDI32.dll")]
    public static extern bool DeleteObject(IntPtr objectHandle); 

    public Form1()
    {
        InitializeComponent();
    }
    private void Button1_Click(object sender, EventArgs e)
    {

        IntPtr font = SlpCreateFont("Arial", 10, 0);

        SlpDebugMode(2);

        //The second parameter defines the type of label per the documentation.
        SlpOpenPrinter("Smart Label Printer 440", 3, false);

        {
            SlpStartLabel();

            //Draw as much as you want with these!
            SlpDrawTextXY(0, 0, font, "Hello World");

            SlpEndLabel();
        }

        SlpClosePrinter();

        DeleteObject(font);
    }
}

}

于 2013-08-13T16:32:05.613 回答