admin管理员组文章数量:1534214
遇到问题:在做一个项目时由于打印机硬件提供商无法提供对应的SDK驱动文件,只能当作Windows标准打印机进行使用,在用户领取时我们需要进行打出小票的程序,现在没有办法进行屏蔽打印页面!
解决办法:
首先我们先内置好默认打印机
1、控制面板中删除无关的设备,只保留我们的打印机
2、使用C#程序屏蔽对应页面(注意使用.NET3.5版本,因为客户机器是Win7_64 IE8)
新建一个 WinForm项目
需要1个托盘控件及 右键菜单的控件,绑定一下事件:
private void CheckWindows()
{
Thread myThread = new Thread(PrintViewConnect);
myThread.Start();
}
// 循环检测窗口
private void PrintViewConnect()
{
while (true)
{
CheckPrintView();
}
}
/// <summary>
/// 找到窗口
/// </summary>
/// <param name="lpClassName">窗口类名(例:Button)</param>
/// <param name="lpWindowName">窗口标题</param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 找到窗口
/// </summary>
/// <param name="hwndParent">父窗口句柄(如果为空,则为桌面窗口)</param>
/// <param name="hwndChildAfter">子窗口句柄(从该子窗口之后查找)</param>
/// <param name="lpszClass">窗口类名(例:Button</param>
/// <param name="lpszWindow">窗口标题</param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
/// <summary>
/// 发送消息
/// </summary>
/// <param name="hwnd">消息接受窗口句柄</param>
/// <param name="wMsg">消息</param>
/// <param name="wParam">指定附加的消息特定信息</param>
/// <param name="lParam">指定附加的消息特定信息</param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
//窗口发送给按钮控件的消息,让按钮执行点击操作,可以模拟按钮点击
private const int BM_CLICK = 0xF5;
private void CheckPrintView()
{
//获取IE浏览器弹出的打印页面
IntPtr maindHwnd = FindWindow(null, "打印");
//看看能不能获取到
if (maindHwnd != IntPtr.Zero)
{
//如果获取到了查找 【打印】 按钮
IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null, "打印(&P)");//按钮控件标题
//检测能不能找到
if (childHwnd != IntPtr.Zero)
{
//如果找到了模拟点击
SendMessage(childHwnd, BM_CLICK, 0, 0);
}
else {
//debug的时候使用
//MessageBox.Show("父窗口已经找到,但未找到按钮");
}
}
else
{ //debug的时候使用
//MessageBox.Show("全部未找到");
}
}
在程序入口调用一下:
核心代码为:
--- 解决问题的方法至此结束了,下面是全部代码包含:屏蔽打印页面、开机自启、托盘运行-------------------------------------------------------(权当是垃圾筐备用,你要是直接复制不能用的话自行检查一下)----------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace SocketServerToolc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AutoStart();
//窗体最小化显示
this.WindowState = FormWindowState.Minimized;
CheckWindows();
}
/// <summary>
/// 不再显示窗体
/// </summary>
/// <param name="value"></param>
protected override void SetVisibleCore(bool value)
{
base.SetVisibleCore(false);
}
/// <summary>
/// 让程序不显示在alt+Tab视图窗体中
/// </summary>
protected override CreateParams CreateParams
{
get
{
const int WS_EX_APPWINDOW = 0x40000;
const int WS_EX_TOOLWINDOW = 0x80;
CreateParams cp = base.CreateParams;
cp.ExStyle &= (~WS_EX_APPWINDOW); // 不显示在TaskBar
cp.ExStyle |= WS_EX_TOOLWINDOW; // 不显示在Alt+Tab
return cp;
}
}
/// <summary>
/// 设置开机自启动
/// </summary>
private void AutoStart()
{
try
{
string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
string path = Application.ExecutablePath.ToLower(); //将当前程序起动路径
string name = System.IO.Path.GetFileName(path); //获得应用程序名称
var regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath, true);
if (regKey == null) regKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(regPath);
regKey.SetValue(name, path);
}
catch
{
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void CheckWindows()
{
Thread myThread = new Thread(PrintViewConnect);
myThread.Start();
}
// 监听客户端是否连接
private void PrintViewConnect()
{
while (true)
{
CheckPrintView();
}
}
/// <summary>
/// 找到窗口
/// </summary>
/// <param name="lpClassName">窗口类名(例:Button)</param>
/// <param name="lpWindowName">窗口标题</param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 找到窗口
/// </summary>
/// <param name="hwndParent">父窗口句柄(如果为空,则为桌面窗口)</param>
/// <param name="hwndChildAfter">子窗口句柄(从该子窗口之后查找)</param>
/// <param name="lpszClass">窗口类名(例:Button</param>
/// <param name="lpszWindow">窗口标题</param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
/// <summary>
/// 发送消息
/// </summary>
/// <param name="hwnd">消息接受窗口句柄</param>
/// <param name="wMsg">消息</param>
/// <param name="wParam">指定附加的消息特定信息</param>
/// <param name="lParam">指定附加的消息特定信息</param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
//窗口发送给按钮控件的消息,让按钮执行点击操作,可以模拟按钮点击
private const int BM_CLICK = 0xF5;
private void CheckPrintView()
{
//获取IE浏览器弹出的打印页面
IntPtr maindHwnd = FindWindow(null, "打印");
//看看能不能获取到
if (maindHwnd != IntPtr.Zero)
{
//如果获取到了查找 【打印】 按钮
IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null, "打印(&P)");//按钮控件标题
//检测能不能找到
if (childHwnd != IntPtr.Zero)
{
//如果找到了模拟点击
SendMessage(childHwnd, BM_CLICK, 0, 0);
}
else {
//debug的时候使用
//MessageBox.Show("父窗口已经找到,但未找到按钮");
}
}
else
{ //debug的时候使用
//MessageBox.Show("全部未找到");
}
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
}
}
}
版权声明:本文标题:IE 浏览器阻止打印页面的方法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1726871479a1087903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论