zoukankan      html  css  js  c++  java
  • C# 调用系统API函数直接连接pos打印机 打印

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;
    /// <summary>
    /// PrintPos 的摘要说明
    /// </summary>
    public class PrintPos
    {

            private FileStream fs = null;
            [DllImport("kernel32.dll")]//调用系统API打印函数
            public static extern IntPtr CreateFile
                (
                string FileName,          // file name
            uint DesiredAccess,       // access mode
            uint ShareMode,           // share mode
            uint SecurityAttributes,  // Security Attributes
            uint CreationDisposition, // how to create
            uint FlagsAndAttributes,  // file attributes
            int hTemplateFile         // handle to template file

                );

        public PrintPos()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 开始打印,本地打印机ltp1 端口打印 调用方法:PrintPos.PrintPage("dsdfdsfdsfsdfdsfsdfdsfs");
        /// </summary>
        /// <param name="strPos"></param>
        /// <returns></returns>
        public string PrintPage(string strPos)
        {
            IntPtr iHandle = CreateFile("LPT1", 0x40000000, 0, 0, 3, 0, 0);
            //判断是否连接上打印机 -1为false
            if (iHandle.ToInt32() == -1)
            {
                return "没有连接到打印机";
            }
            else
            {
                fs = new FileStream(iHandle, FileAccess.ReadWrite);
                //StreamReader sr = new StreamReader(fs);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(strPos, 0, 500);
                sw.Close();
                fs.Close();
                return "已经成功连接打印机";
            }
        }
    }

  • 相关阅读:
    硬币游戏 Project Euler 232
    屏幕空间的近似全局光照明(Approximative Global Illumination in Screen Space)
    四维之美
    vertex texture fetching in HLSL, and heightfield normal calculation
    一个VS小插件(跳出括号)
    我的算法书籍收藏
    Algorithms.算法概论.习题答案
    UML用例图教程详解
    大连理工大学软件学院博客地址
    快递查询API,我推荐“爱快递”
  • 原文地址:https://www.cnblogs.com/zahxz/p/2794866.html
Copyright © 2011-2022 走看看