如何獲得以下樣式的消息框非常好?該MessageBox function似乎不能做的伎倆如何在WPF中爲消息框設置不同樣式的字體
2
A
回答
2
您可以使用窗口
這裏讓自己的通知對話框是一個簡單的例子我剛掀起一起
用法:
NotificationDialog.ShowNotification("Backup and Restor center"
, "You need to be an Administrator to run backup"
, "Use Fast User Switching to switch to an account with administrator privileges, or log off and log on as an administrator"
, NotifyIcon.Exclamation);
代碼:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
namespace WpfApplication8
{
/// <summary>
/// Interaction logic for NotificationDialog.xaml
/// </summary>
public partial class NotificationDialog : Window, INotifyPropertyChanged
{
public static void ShowNotification(string title, string caption, string message, NotifyIcon icon)
{
NotificationDialog dialog = new NotificationDialog();
dialog.Title = title;
dialog.Caption = caption;
dialog.Message = message;
dialog.Image = dialog.GetIcon(icon);
dialog.ShowDialog();
}
private string _caption;
private string _message;
private BitmapSource _image;
private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
public NotificationDialog()
{
DataContext = this;
InitializeComponent();
Loaded += (s, e) =>
{
var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
};
}
public string Caption
{
get { return _caption; }
set { _caption = value; NotifyPropertyChanged(); }
}
public string Message
{
get { return _message; }
set { _message = value; NotifyPropertyChanged(); }
}
public BitmapSource Image
{
get { return _image; }
set { _image = value; NotifyPropertyChanged(); }
}
private BitmapSource GetIcon(NotifyIcon iconType)
{
Icon icon = (Icon)typeof(SystemIcons).GetProperty(iconType.ToString(), BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
return Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
private void Button_Close_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName]string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public enum NotifyIcon
{
Application,
Asterisk,
Error,
Exclamation,
Hand,
Information,
Question,
Shield,
Warning,
WinLogo
}
}
的XAML:
<Window x:Class="WpfApplication8.NotificationDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="239" Width="417" Title="{Binding Title}" ResizeMode="NoResize" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="223*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Button Content="Close" HorizontalAlignment="Right" Margin="0,0,10,7" Grid.Row="1" VerticalAlignment="Bottom" Width="75" Click="Button_Close_Click"/>
<StackPanel Margin="70,10,10,0">
<TextBlock Text="{Binding Caption}" FontSize="20" Foreground="DarkBlue" TextWrapping="Wrap"/>
<TextBlock Margin="0,10,0,0" Text="{Binding Message}" TextWrapping="Wrap"/>
</StackPanel>
<Image Source="{Binding Image}" Stretch="None" HorizontalAlignment="Left" Height="52" Margin="6,10,0,0" VerticalAlignment="Top" Width="55"/>
</Grid>
</Window>
結果:
0
我不認爲這是對這種風格正式框架的支持,但對於建築的消息框狀的準則(包括該截圖中使用的特定字體)在這裏:User Interface Text。
另請參見此CodeProject文章:WPF TaskDialog Wrapper and Emulator。顯然,它使用本地Windows 7調用來生成正確的樣式。
相關問題
- 1. 如何爲文本視圖設置不同的字體樣式
- 2. 如何設置字體樣式在FPDF
- 3. 如何在WPF Window.Resources中設置樣式。
- 4. 爲多行WKInterfaceLabel設置不同的字體樣式
- 5. 如何設置WebView的字體樣式?
- 6. 如何在WPF DataGrid中設置組合框的樣式?
- 7. 如何在Android中設置自定義不同的字體樣式
- 8. Wpf樣式:在列表框中設置樣式
- 9. 如何設置字體樣式?
- 10. WPF如何在RadioButton上設置樣式?
- 11. 如何在WPF網格設置樣式
- 12. 如何在wpf中創建消息框
- 13. toolStripComboBox設置字體樣式?
- 14. 如何爲後備字體設置不同的字體重量?
- 15. 爲RTF文檔設置字體樣式
- 16. 如何爲不同的textareas設置不同的字體顏色
- 17. Apache POI:如何爲字段設置字體樣式(PAGE,PAGENUM,PAGEREF ...)
- 18. 如何在JTextArea實例中爲所選文本設置粗體字體樣式
- 19. 如何在QMessageBox中爲消息設置邊框?
- 20. 在Blend中設置WPF中的樣式列表框
- 21. 如何爲WPF中的多個TabItem設置一種樣式?
- 22. WPF消息框與WinForms的消息框
- 23. 如何在WPF中爲文本框和組合框應用相同的樣式?
- 24. 如何在JAVA中設置字體樣式和大小?
- 25. 如何在運行時在wpf中設置gridview行的樣式?
- 26. 在WPF中使用IDataErrorInfo設置文本框的樣式
- 27. 如何設置粗體樣式的自定義字體
- 28. 爲文本框中的不同行設置不同的字體大小
- 29. 如何設置tkinter消息邊框
- 30. 在WPF中設置樣式TargetType
只需從一個窗口製作一個。 –