简体   繁体   中英

How make the PrintDialog top most

I have create an exe and will prompt the print dialog in new thread. This exe will be run when user click a button from another application . How can I make the print dialog top most or not allow user switch back to the caller until print dialog was closed ? Thanks.

Thread t = new Thread(() =>
{
    System.Windows.Controls.PrintDialog pDialog = new System.Windows.Controls.PrintDialog();
    pDialog.PageRangeSelection = PageRangeSelection.AllPages;
    pDialog.UserPageRangeEnabled = true;

    Nullable<Boolean> print = pDialog.ShowDialog();

    if (print == true)
    {

    }
});

t.SetApartmentState(ApartmentState.STA);
t.Start();
        `[DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetWindowPos(IntPtr hWnd,
          int hWndInsertAfter, int x, int y, int cx, int cy, int uFlags);

        private const int HWND_TOPMOST = -1;
        private const int HWND_NOTOPMOST = -2;
        private const int SWP_NOMOVE = 0x0002;
        private const int SWP_NOSIZE = 0x0001;
        private const int SWP_SHOWWINDOW = 0x0040;

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);

        private bool TweakPrintDialog()
        {
            IntPtr result = IntPtr.Zero;
            IntPtr printDialogHandle = FindWindowEx(<Insert here Current WindowHandler>, result, "#32770", null);

            if (printDialogHandle != IntPtr.Zero)
            {
                SetWindowPos(printDialogHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);

                return false;
            }
            return true;
        }

` Using:

           Dispatcher.BeginInvoke(new Action(() =>
            {
                do
                {
                    Thread.Sleep(100);
                } while (TweakPrintDialog());
            }));

            if (printDialog.ShowDialog() == true)
            {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM