WPF消息框

弹窗是非模式对话框,即可以多个弹窗弹出,且弹窗后面的窗体可以被操作,不会被锁定。

自定义的窗体Window实现以下步骤:

在C#代码中弹出窗体时,使用 window.Show() 而不是 window.ShowDialog();

最好设置 window.Topmost = true; 可以在XAML顶部写、也可以在C#代码中设置。否则该窗体可以被主界面遮挡(比如按Tab切换到主界面时),该弹窗没有被关闭,但又看不到。

如有需要,可以设置 ResizeMode=”NoResize”; 可以在XAML顶部写、也可以在C#代码中设置。这样该弹窗将无法改变宽高,且没有最大化、最小化按钮。

//定义消息框

string messageBoxText = “需要保存吗?”;

string caption = “HELLO”;

MessageBoxButton button = MessageBoxButton.YesNoCancel;

MessageBoxImage icon = MessageBoxImage.Warning;

//显示消息框

MessageBoxResult result = MessageBox.Show(messageBoxText, caption,
button, icon);

//处理消息框信息

switch (result)

{

case

MessageBoxResult.Yes:

    // ...                      

    break;

case

MessageBoxResult.No:

    // ...                      

    break;

case

MessageBoxResult.Cancel:

    // ...                     

    break;

}

//打开文件对话框

Microsoft.Win32.OpenFileDialog dlg = new
Microsoft.Win32.OpenFileDialog();

dlg.FileName = “Document”; // Default file name

dlg.DefaultExt = “.txt”; // Default file extension

dlg.Filter = “Text documents (.txt)|*.txt”; // Filter
files by extension

// Show open file dialog box

Nullable result = dlg.ShowDialog();

// Process open file dialog box results

if (result == true)

{

// Open document                  

string filename =

dlg.FileName;

//...             

}
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44589117/article/details/94958769