很长时间没开博了。继续。。。。
WPF相对于Winform, dialog control是少了几个阿。不过都是一家子,可以必要时拿过来用用了。
做个简单程序调用个dialog。 效果图:
简单的调用,代码:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static string PicturePath = "" ;
private void btnShow_Click( object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = " .jpg " ;
dlg.Filter = " JPEG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|BMP Files (*.bmp)|*.bmp|All files (*.*)|*.* " ;
// Display OpenFileDialog by calling ShowDialog method
Nullable < bool > result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true )
{
// Open document
string filename = dlg.FileName;
PicturePath = filename;
}
this .image1.Source = GetImage();
this .image1.Width = this .picPanel.Width;
image1.Height = picPanel.Height;
}
private static BitmapImage GetImage()
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(PicturePath);
image.EndInit();
return image;
}
private void btnClear_Click( object sender, RoutedEventArgs e)
{
this .image1.Source = null ;
}
private void btnSetBG_Click( object sender, RoutedEventArgs e)
{
System.Windows.Forms.ColorDialog color = new System.Windows.Forms.ColorDialog();
if (color.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this .picPanel.Background = new SolidColorBrush(Color.FromArgb(color.Color.A,color.Color.R,color.Color.G,color.Color.B));
}
}
private void btnClose_Click( object sender, RoutedEventArgs e)
{
this .Close();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static string PicturePath = "" ;
private void btnShow_Click( object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = " .jpg " ;
dlg.Filter = " JPEG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|BMP Files (*.bmp)|*.bmp|All files (*.*)|*.* " ;
// Display OpenFileDialog by calling ShowDialog method
Nullable < bool > result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true )
{
// Open document
string filename = dlg.FileName;
PicturePath = filename;
}
this .image1.Source = GetImage();
this .image1.Width = this .picPanel.Width;
image1.Height = picPanel.Height;
}
private static BitmapImage GetImage()
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(PicturePath);
image.EndInit();
return image;
}
private void btnClear_Click( object sender, RoutedEventArgs e)
{
this .image1.Source = null ;
}
private void btnSetBG_Click( object sender, RoutedEventArgs e)
{
System.Windows.Forms.ColorDialog color = new System.Windows.Forms.ColorDialog();
if (color.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this .picPanel.Background = new SolidColorBrush(Color.FromArgb(color.Color.A,color.Color.R,color.Color.G,color.Color.B));
}
}
private void btnClose_Click( object sender, RoutedEventArgs e)
{
this .Close();
}
}
}
别忘了引用system.windows.form噢!!
哦对了,这里有个Bug:拉伸窗体的时候只有最后一个Button拉伸变化,其它的不变。什么问题呢? 待研究...