2012-08-10 57 views
1

我有一个问题把我的窗口放在所有的绝对前面,但是当我启动一个打印对话框时,我无法找到将前面的窗口保持在前面:我必须将窗口放在打印对话框上做TopMost,但是任务栏出现虽然是隐藏的。Windows窗体最大化总体上绝对

在我的表格窗口中的代码,将其最大化,并把它整体:

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); 
    } 
} 

我试过这段代码,在我的表单中调用SetWinFullScreen之后,“this.TopMost = true;”最大限度的形式,但它不工作之一:

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); 

该行推出的打印对话框,然后出现任务栏(隐藏或不)。

+4

从雷蒙德陈在这里的页面,但是,你有没有考虑过如果两个程序试图同时这样做会发生什么? – asawyer 2012-08-10 13:27:02

+0

您是否正在编写自助服务终端应用程序? – Jodrell 2012-08-10 13:29:37

+2

@asawyer仅供参考:http://blogs.msdn.com/b/oldnewthing/archive/2005/06/07/426294.aspx – rene 2012-08-10 13:32:58

回答

0

打印对话框不是窗体,它是从窗体继承的控件,如文本框和组合框。所以你不应该期望最高的应用。下面的重点是我的。

最上面的形式是重叠所有其它(非最上面的)形式 形式即使它不是活性或前景形式。最上面的表格 始终显示在桌面上的 窗口的z顺序中的最高点。您可以使用此属性创建一个始终显示在您的应用程序中的表格 ,例如Find和 替换工具窗口。

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost.aspx

+0

TopMost并不是我尝试过的独特的东西,你可以看到代码,我可以做什么来使我的窗体在所有窗口/弹出窗口/等等......然后呢? 感谢您的回答。 – Eldoran 2012-08-10 15:13:52

+0

就是这样。对话框都是这样工作的。如果你不能接受它作为你的解决方案的一部分,通过主窗体处理所有的打印对话框的东西,并完成它。想想你在问什么。你需要一个弹出窗口,但是一旦弹出窗口,就会压缩它,这样主窗体就会处于最前面。那么你会如何控制弹出窗口足够长的时间以让它按照你所需要的去做? – GrayFox374 2012-08-10 17:10:12

0

这个环节是,要接近解决问题的办法有用: Hide Start Orb on Vista/Win 7 in C#

继链路的步骤,我添加到我的任务栏类:

[System.Runtime.InteropServices.DllImport("user32.dll")] 
private static extern IntPtr FindWindowEx(
      IntPtr parentHwnd, 
      IntPtr childAfterHwnd, 
      IntPtr className, 
      string windowText); 

public static void Show() 
{ 
     ShowWindow(Handle, SW_SHOW); 
     ShowWindow(FindWindowEx(IntPtr.Zero, IntPtr.Zero, (IntPtr)0xC017, null), SW_SHOW); 
} 

public static void Hide() 
{ 
     ShowWindow(Handle, SW_HIDE); 
     ShowWindow(FindWindowEx(IntPtr.Zero, IntPtr.Zero, (IntPtr)0xC017, null), SW_HIDE); 
} 

并使用TopMost。然后当出现对话框我控制的时刻和焦点我的应用程序快速放在'弹出'前,但这次没有显示任务栏/ Windows图标和用户不能点击任何东西。

不是完美的解决方案,但它对我有效。