reportViewer1.PrintDialog()的关闭 触发打印对话框的打印按钮
public void button1_Click(object sender, EventArgs e)
{
Pg();
}
public void Pg()
{
Closed clos = new Closed();
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(clos.ClosePrintDialog));
th.Start();
reportViewer1.PrintDialog();
th.Abort();
clos.Dispose();
}
//closed 窗口的代码
using System.Runtime.InteropServices;
namespace printcode
{
public partial class Closed : Form
{
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
public Closed()
{
InitializeComponent();
}
private void Closed_Load(object sender, EventArgs e)
{
//this.Hide();
}
public void ClosePrintDialog()
{
while (true)
{
//下面的这些参数都可以用Spy++查到
string lpszParentClass = "#32770"; //整个窗口的类名
string lpszParentWindow = "打印"; //窗口标题
string lpszClass_Print = "Button"; //需要查找的Button的类名
string lpszName_Print = "打印(&P)"; //需要查找的Button的标题
IntPtr ParenthWnd = new IntPtr(0);
IntPtr ButtonhWnd = new IntPtr(0);
//查到窗体,得到整个窗体
ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);
//判断这个窗体是否有效
if (!ParenthWnd.Equals(IntPtr.Zero))
{
//得到Button这个子窗体,并触发它的Click事件
ButtonhWnd = FindWindowEx(ParenthWnd, ButtonhWnd, lpszClass_Print, lpszName_Print);
if (!ButtonhWnd.Equals(IntPtr.Zero))
{
SendMessage(ButtonhWnd, WM_CLICK, (IntPtr)0, "0");
}
}
System.Threading.Thread.Sleep(30);
}
}
}
}