5

アプリケーションにWPFウィンドウがあり、特定のシナリオMainView.Activate();MainView.BringIntoView();メソッドを呼び出してアクティブにします。また、この「MainView」ウィンドウにフォーカスを設定します。

しかし、私の要件は、このウィンドウがフォーカスされないようにすることです。つまり、カーソルは以前のアプリケーション (メモ帳、単語など) に残るはずです。

使ってみMainView.ShowActivated="False"ましたがダメでした。ここで述べたように HwndSource を使用する必要がありますか?

ケントの助けの後に使用したコード(ウィンドウが最小化されている場合にのみ機能します):

IntPtr HWND_TOPMOST = new IntPtr(-1);
            const short SWP_NOMOVE = 0X2;
            const short SWP_NOSIZE = 1;
            const short SWP_NOZORDER = 0X4;
            const int SWP_SHOWWINDOW = 0x0040;

            Process[] processes = Process.GetProcesses(".");

            foreach (var process in processes)
            {
                IntPtr handle = process.MainWindowHandle;
                if (handle != IntPtr.Zero)
                {
                    SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
                }
            }
4

4 に答える 4

6

私はそのような状況のためのWin32ヘルパークラスを持っています ここにあなたのケースのリストがあります

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace your.namespace {
    public static class Win32 {
        public static void ShowWindowNoActive( Window window) {
            var hwnd = (HwndSource.FromVisual(window) as HwndSource).Handle;
            ShowWindow(hwnd, ShowWindowCommands.SW_SHOWNOACTIVATE);
        }

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

        private enum ShowWindowCommands : int {
            SW_SHOWNOACTIVATE = 4
        }
    }
}
于 2014-12-26T13:39:20.993 に答える
5

最近のブログ投稿では、SetWindowPosフォーカスを与えずに、ウィンドウを z オーダーの前面に移動していました。私は、WPF が p/invoke なしで同じことを達成する組み込みの手段を持っているとは思わない。

于 2013-01-08T08:44:44.943 に答える