PrintDialog поверх остальных окон - C#
Формулировка задачи:
Здравствйте!
Есть программа, она ждет пока в определенной папке появится файл и когда он там оказывается, выводит его на печать. Проблема в том, что диалог печати при выводе не становится активным, и не появляется поверх остальных окон. Пол дня бьюсь над этой проблемой, нормального решения так и не нашел. Помогите пожалуйста, время поджимает, программу надо сдавать.
Тоесть, сначала мы импортируем библиотеки Windows, затем при помощи функции FindWindow пытаемся определить hWnd окна с заголовком "Печать". После этого делаем окно с этим hWnd активным, при помощи функции SetForegroundWindow.
Но и здесь меня ждала неудача, FindWindow вернула нулевое значение.
// Create the print dialog object and set options
PrintDialog pDialog = new PrintDialog();
pDialog.AllowSomePages = true;
pDialog.ShowHelp = true;
DialogResult result = pDialog.ShowDialog();
// If the result is OK then print the document
if (result == DialogResult.OK)
{
PrintDocument D = new PrintDocument();
D.DocumentName = destFile;
D.Print();
}
Нашел решение через Windows API:
using System.Runtime.InteropServices;
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SetForegroundWindow(IntPtr hWnd);
IntPtr printWnd = FindWindow(null, "Печать");
SetForegroundWindow(printWnd);Решение задачи: «PrintDialog поверх остальных окон»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace ca6
{
public class SetForegroundPrintdialog
{
[DllImport("user32.dll")]
public static extern IntPtr SetForegroundWindow(IntPtr hWnd);
// For Windows Mobile, replace user32.dll with coredll.dll
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static IntPtr GetPrintHWnd(int n)
{
if (n > 0)
{
Thread.Sleep(200);
IntPtr printWnd = FindWindow(null, "Печать");
if (printWnd.ToString() == "0")
{
return GetPrintHWnd(n - 1);
}
else
{
return printWnd;
}
}
else
{
IntPtr printWnd = FindWindow(null, "Печать");
return printWnd;
}
}
public static void Main()
{
SetForegroundWindow(GetPrintHWnd(30));
}
}
}