Search This Blog

Showing posts with label Windows. Show all posts
Showing posts with label Windows. Show all posts

Tuesday, August 22, 2017

Display XAML window in the second display WPF

WPF > Windows > Forms > Screen

AllScreens gets an array of all displays on the system.

Example

Display window in the second display WPF

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   var screen = System.Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).First();
   var area = screen.WorkingArea;
   Left = area.Left;
   Top = area.Top;
   Width = area.Width;
   Height = area.Height;
}

Friday, January 24, 2014

Open File Dialog WPF Example

WPF > Windows > OpenFileDialog


Open File Dialog  WPF Example

Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".doc";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
   string filename = dlg.FileName;
}

Tuesday, November 19, 2013

WPF Windows Namespace

WPF > Windows

WPF Windows Namespace contains base classes, classes that support the WPF property system and event logic.



WPF DependencyProperty Example

WPF > Windows > DependencyProperty 

DependencyProperty is a property that can be set through methods such as, styling, data binding, animation, and inheritance.

Example: Create a numeric TextBox with DependencyProperty 

Create new WPF User Control Library


NumericTextBox.xaml

<TextBox x:Class="NumericTextBox.NumericTextBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          >
</TextBox>

 
NumericTextBox.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
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 NumericTextBox
{
    /// <summary>
    /// Interaction logic for NumericTextBox.xaml
    /// </summary>
    public partial class NumericTextBox : TextBox
    {
        public NumericTextBox()
        {
            InitializeComponent();
            this.PreviewTextInput += OnPreviewTextInput;
        }

        /// <summary>
        /// The max value property
        /// </summary>

        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register("MaxValue", typeof(double?),
            typeof(NumericTextBox), new UIPropertyMetadata(null));

        public double? MaxValue
        {
            get { return (double?)GetValue(MaxValueProperty); }
            set { SetValue(MaxValueProperty, value); }
        }


        /// <summary>
        /// The min value property
        /// </summary>

        public static readonly DependencyProperty MinValueProperty =
            DependencyProperty.Register("MinValue", typeof(double?),
            typeof(NumericTextBox), new UIPropertyMetadata(null));

        public double? MinValue
        {
            get { return (double?)GetValue(MinValueProperty); }
            set { SetValue(MinValueProperty, value); }
        }

        private bool CheckNumeric(string text)
        {
             var regex = new Regex("[^0-9.]+");
            return !regex.IsMatch(text);
        }

        private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (this.CheckNumeric(e.Text))
            {
                if (e.Text == "." && this.Text.Contains("."))
                    e.Handled = true;
                else
                {
                    // Check min value
                    if (MinValue != null && Convert.ToDouble(this.Text + e.Text) < MinValue)
                    {
                        this.Text = MinValue.ToString();
                        this.SelectionStart = this.Text.Length;
                        e.Handled = true;
                    }

                    // Check max value
                    if (MaxValue != null && Convert.ToDouble(this.Text + e.Text) > MaxValue)
                    {
                        this.Text = MaxValue.ToString();
                        this.SelectionStart = this.Text.Length;
                        e.Handled = true;
                    }
                }
            }
            else
                e.Handled = true;
        }
    }
}



MainWindow.xaml

You can set MinValue and MaxValue properties

<Window

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:NumericTextBox="clr-namespace:NumericTextBox;assembly=NumericTextBox" x:Class="WpfApplication3.MainWindow"

        Title="MainWindow" Height="350" Width="525">

    <Grid>

        <NumericTextBox:NumericTextBox HorizontalAlignment="Left" Margin="98,89,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"

                                     Width="201" MinValue="1" MaxValue="100000"/>

    </Grid>

</Window>




Wednesday, October 30, 2013

WPF Window

WPF > Controls  > ContentControl > Window

A Windows Presentation Foundation (WPF) Window represents interaction between a user and a standalone application.
WPF window has two areas:
  • A non-client area (title, icon, buttons, border)
  • A client area (application content)

WPF Window Example:

In this example we will create a transparent window with opacity of 80%.

XAML:

<Window x:Class="ButtonExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Opacity="0.8" AllowsTransparency="True" WindowStyle="None" Background="#FFEEE4E4"
        WindowStartupLocation="CenterScreen">
    <Grid>
        <Button Content="Close" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

C# Code:

private void Button_Click(object sender, RoutedEventArgs e)
{  
  this.Close();
}

Tuesday, October 29, 2013

Display SplashScreen From Resource File WPF


WPF > Windows > SplashScreen 

SplashScreen shows an image when a WPF application starts or in a startup window.
Call the Show method to display the splash screen.
Supported image format: BMP, GIF, JPEG, PNG, TIFF.

Example:

var splashScreen = new SplashScreen( resourceName : "Resources/loading1.gif");
splashScreen.Show(false);