在WPF程序中,正常情况下,调用窗体的ShowDialog方法之后,w1.ShowDialog() 该应用程序的除去w1窗口之外所有窗口都将处于无效状态(disable),直到w1窗口关闭。
在某些情况下我们希望,w1.ShowDialog()方法调用之后,只有w1窗口的父窗口处于无效状态 而程序其他窗口可以继续响应用户操作。百度、google了一番,方法都太复杂,自己想了一个方法也挺简单的
这里我们需要用到W32函数 EnableWindow 我做了简单的封装 直接使用下面的类就可以了 调用ShowDialog之前先设置窗口的owner,就是这么简单
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Interop;
namespace WpfApp3
{
public static class WindowHelper
{
[DllImport("user32.dll")]
static extern int EnableWindow(IntPtr handle, bool enable);
public static void ShowDialog(this Window window,bool onlyDisableOwner)
{
if(window.Owner!=null&&onlyDisableOwner)
{
window.Loaded += Window_Loaded;
}