WPF experience-Dialog Box call

Source: Internet
Author: User

It has been a long time before the blog was launched. Continue ....

Compared with winform, WPF has fewer dialog control. But it's all a family. You can use it if necessary.

Make a simple program to call a dialog. :

Simple call, code:

 

Code

 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();
}


}
}

Don't forget to reference system. Windows. form !!

 

By the way, there is a bug here: only the last button changes while stretching the form, and the rest remains unchanged. What's the problem? To be studied...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.