我有一个问题要绝对将我的窗口放在前面,但是当我启动一个打印对话框时,我无论如何都找不到将我的窗口放在前面:我必须将我的窗口放在打印对话框上执行 TopMost,但出现任务栏虽然是隐藏的。
我的表单窗口中的代码将其最大化并整体放置:
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.TopMost = true;
启动打印对话框的代码:
PrinterSettings printerSettings = new PrinterSettings();
IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
IntPtr pDevMode = GlobalLock(hDevMode);
int sizeNeeded = DocumentProperties(IntPtr.Zero, IntPtr.Zero, printerSettings.PrinterName, IntPtr.Zero, pDevMode, 0);
IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
DocumentProperties(IntPtr.Zero, IntPtr.Zero, printerSettings.PrinterName, devModeData, pDevMode, 14);
// <--- Here the print dialog appears and the thread stops till I close the dialog
GlobalUnlock(hDevMode);
printerSettings.SetHdevmode(devModeData);
printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
GlobalFree(hDevMode);
Marshal.FreeHGlobal(devModeData);
这是我隐藏任务栏的代码:
public class Taskbar
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
public Taskbar()
{
}
protected static int Handle
{
get
{
return FindWindow("Shell_TrayWnd", "");
}
}
public static void Show()
{
ShowWindow(Handle, SW_SHOW);
}
public static void Hide()
{
ShowWindow(Handle, SW_HIDE);
}
}
我尝试了这段代码,在“this.TopMost = true;”之后也在我的表单中调用了 SetWinFullScreen 最大化形式,但它也不起作用:
public class WinApi
{
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
private static extern int GetSystemMetrics(int which);
[DllImport("user32.dll")]
private static extern void
SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int X, int Y, int width, int height, uint flags);
private const int SM_CXSCREEN = 0;
private const int SM_CYSCREEN = 1;
private static IntPtr HWND_TOP = IntPtr.Zero;
private const int SWP_SHOWWINDOW = 64; // 0×0040
public static int ScreenX
{
get { return GetSystemMetrics(SM_CXSCREEN);}
}
public static int ScreenY
{
get { return GetSystemMetrics(SM_CYSCREEN);}
}
public static void SetWinFullScreen(IntPtr hwnd)
{
SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
}
}
在所有情况下,当启动打印对话框时,会出现任务栏并且用户可以单击它,因为它位于所有内容的前面。
有没有办法在后台启动此打印对话框或将我的表单绝对放在最上面(不出现任何任务栏或对话框)?
编辑:问题出在这一行:
DocumentProperties(IntPtr.Zero, IntPtr.Zero, printerSettings.PrinterName, devModeData, pDevMode, 14);
此行启动打印对话框,然后出现任务栏(隐藏或不隐藏)。