Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .xaml
- <Window x:Class="TemplateWpfMaterial.MainWindow"
- xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:TemplateWpfMaterial"
- mc:Ignorable="d"
- Title=""
- MinHeight="510"
- MinWidth="600"
- WindowStartupLocation="CenterScreen"
- SizeToContent="WidthAndHeight"
- WindowStyle="None"
- WindowState="Normal"
- ResizeMode="CanResizeWithGrip"
- BorderThickness="1"
- Foreground="{x:Null}"
- BorderBrush="{x:Null}"
- Background="{x:Null}"
- Style="{StaticResource ModifiedWindow}" KeyDown="Window_KeyDown">
- <Grid
- Width="auto"
- Height="auto"
- x:Name="MainGrid"
- Background="White">
- <Grid.RowDefinitions>
- <RowDefinition Height="30" MinHeight="30" />
- <RowDefinition Height="30" MinHeight="30" />
- <RowDefinition Height="30" MinHeight="30" />
- <RowDefinition Height="30" MinHeight="30" />
- <RowDefinition Height="30" MinHeight="30" />
- <RowDefinition Height="30" MinHeight="30" />
- <RowDefinition Height="30" MinHeight="30" />
- <RowDefinition Height="30" MinHeight="30" />
- <RowDefinition Height="30" MinHeight="30" />
- <RowDefinition Height="30*" MinHeight="30" />
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="170" MinWidth="170" />
- <ColumnDefinition Width="30*" MinWidth="30" />
- </Grid.ColumnDefinitions>
- <!--Up Panel-->
- <StackPanel
- Grid.ColumnSpan="2"
- Grid.Column="0"
- x:Name="Drag"
- Grid.Row="0"
- HorizontalAlignment="Stretch"
- Height="30"
- Background="{DynamicResource PrimaryHueDarkBrush}"
- Margin="0,0,141,0" PreviewMouseDown="Drag_MouseDown">
- <Label Content="Computer Graphics #5"
- HorizontalAlignment="Left"
- VerticalAlignment="Center"
- Margin="10,2,0,0"
- Foreground="{DynamicResource PrimaryHueMidForegroundBrush}"/>
- </StackPanel>
- <!--Close-->
- <StackPanel
- Grid.ColumnSpan="2"
- Grid.Column="0"
- x:Name="Close"
- Width="47"
- Height="30"
- HorizontalAlignment="Right"
- Margin="0,0,0,0"
- MouseEnter="Close_MouseEnter"
- MouseLeave="Close_MouseLeave"
- Background="{DynamicResource PrimaryHueDarkBrush}"
- PreviewMouseDown="Close_MouseDown">
- <Image
- Width="19"
- Height="19"
- HorizontalAlignment="Center"
- Margin="0,5"
- Source="pack://siteoforigin:,,,/Icon/Close.png" />
- </StackPanel>
- <!--Max-->
- <StackPanel
- Grid.ColumnSpan="2"
- Grid.Column="0"
- x:Name="Max"
- Width="47"
- Height="30"
- Background="{DynamicResource PrimaryHueDarkBrush}"
- HorizontalAlignment="Right"
- Grid.Row="0"
- Margin="0,0,47,0"
- MouseEnter="Max_MouseEnter"
- MouseLeave="Max_MouseLeave" PreviewMouseDown="Max_MouseDown">
- <Image
- x:Name="IconMax"
- Width="11"
- Height="11"
- HorizontalAlignment="Center"
- Margin="0,9"
- Source="pack://siteoforigin:,,,/Icon/Max.png" />
- </StackPanel>
- <!--Min-->
- <StackPanel
- Grid.ColumnSpan="2"
- Grid.Column="0"
- x:Name="Min"
- Width="47"
- Height="30"
- HorizontalAlignment="Right"
- Grid.Row="0"
- Margin="0,0,94,0"
- Background="{DynamicResource PrimaryHueDarkBrush}"
- MouseEnter="Min_MouseEnter"
- MouseLeave="Min_MouseLeave"
- PreviewMouseDown="Min_MouseDown">
- <Image
- x:Name="IconMin"
- Width="11"
- Height="11"
- HorizontalAlignment="Center"
- Margin="0,9"
- Source="pack://siteoforigin:,,,/Icon/Min.png" />
- </StackPanel>
- <Button
- Margin="10,0,10,0"
- Grid.Row="2"
- Grid.Column="0"
- Background="{DynamicResource PrimaryHueLightBrush}"
- Content="Draw Image"
- BorderBrush="{x:Null}" Click="Button_Click"
- />
- <Button
- Margin="10,0,10,0"
- Grid.Row="4"
- Grid.Column="0"
- Background="{DynamicResource PrimaryHueLightBrush}"
- Content="Clear field"
- BorderBrush="{x:Null}" Click="Button_Click_1"
- />
- <Button
- Margin="10,0,10,0"
- Grid.Row="6"
- Grid.Column="0"
- Background="{DynamicResource PrimaryHueLightBrush}"
- Content="Display image"
- BorderBrush="{x:Null}" Click="Button_Click_2"
- />
- <Button
- Margin="10,0,10,0"
- Grid.Row="8"
- Grid.Column="0"
- Background="{DynamicResource PrimaryHueLightBrush}"
- Content="Convert to gray"
- BorderBrush="{x:Null}" Click="Button_Click_3"
- />
- <StackPanel
- Orientation="Horizontal"
- Margin="0,0,10,10"
- Grid.Row="2"
- Grid.RowSpan="8"
- Grid.Column="1"
- Background="#FFFEFEFE">
- <Canvas
- x:Name="Field">
- </Canvas>
- <Image
- x:Name="FieldImage">
- </Image>
- </StackPanel>
- </Grid>
- </Window>
- .cs
- using System;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using Microsoft.Win32;
- namespace TemplateWpfMaterial
- {
- public partial class MainWindow : Window
- {
- public static BitmapImage IMax = new BitmapImage();
- public static BitmapImage IMin = new BitmapImage();
- public static BitmapImage IMaxBlack = new BitmapImage();
- public static BitmapImage IMinBlack = new BitmapImage();
- public Brush PrimaryHueLightBrush = new SolidColorBrush();
- public Brush PrimaryHueLightForegroundBrush = new SolidColorBrush();
- public Brush PrimaryHueMidBrush = new SolidColorBrush();
- public Brush PrimaryHueMidForegroundBrush = new SolidColorBrush();
- public Brush PrimaryHueDarkBrush = new SolidColorBrush();
- public Brush PrimaryHueDarkForegroundBrush = new SolidColorBrush();
- BrushConverter bc = new BrushConverter();
- public MainWindow()
- {
- InitializeComponent();
- IMax.BeginInit();
- IMax.UriSource = new Uri("pack://siteoforigin:,,,/Icon/Max.png");
- IMax.EndInit();
- IMin.BeginInit();
- IMin.UriSource = new Uri("pack://siteoforigin:,,,/Icon/Min.png");
- IMin.EndInit();
- IMaxBlack.BeginInit();
- IMaxBlack.UriSource = new Uri("pack://siteoforigin:,,,/Icon/Max Black.png");
- IMaxBlack.EndInit();
- IMinBlack.BeginInit();
- IMinBlack.UriSource = new Uri("pack://siteoforigin:,,,/Icon/Min Black.png");
- IMinBlack.EndInit();
- ResourceDictionary resources = this.Resources;
- resources["PrimaryHueMidBrush"] = (Brush)bc.ConvertFrom("#3F51B5");
- resources["PrimaryHueMidForegroundBrush"] = (Brush)bc.ConvertFrom("#FFFFFF");
- resources["PrimaryHueDarkBrush"] = (Brush)bc.ConvertFrom("#303F9F");
- resources["PrimaryHueDarkForegroundBrush"] = (Brush)bc.ConvertFrom("#FFFFFF");
- resources["PrimaryHueLightBrush"] = (Brush)bc.ConvertFrom("#E91E63");
- resources["PrimaryHueLightForegroundBrush"] = (Brush)bc.ConvertFrom("#FFFFFF");
- this.MaxHeight = System.Windows.SystemParameters.WorkArea.Height + 10;
- this.MaxWidth = System.Windows.SystemParameters.WorkArea.Width + 10;
- GetColors();
- }
- public void GetColors()
- {
- var bc = new BrushConverter();
- ResourceDictionary resources = this.Resources;
- PrimaryHueLightBrush = (Brush)resources["PrimaryHueLightBrush"];
- PrimaryHueLightForegroundBrush = (Brush)resources["PrimaryHueLightForegroundBrush"];
- PrimaryHueMidBrush = (Brush)resources["PrimaryHueMidBrush"];
- PrimaryHueMidForegroundBrush = (Brush)resources["PrimaryHueMidForegroundBrush"];
- PrimaryHueDarkBrush = (Brush)resources["PrimaryHueDarkBrush"];
- PrimaryHueDarkForegroundBrush = (Brush)resources["PrimaryHueDarkForegroundBrush"];
- Min.Background = PrimaryHueDarkBrush;
- Max.Background = PrimaryHueDarkBrush;
- Close.Background = PrimaryHueDarkBrush;
- }
- private void Drag_MouseDown(object sender, MouseButtonEventArgs e)
- {
- if (WindowState == WindowState.Maximized)
- {
- WindowState = WindowState.Normal;
- var point = e.GetPosition(this);
- Left = point.X - 10;
- Top = point.Y - 10;
- }
- this.DragMove();
- }
- private void Close_MouseEnter(object sender, MouseEventArgs e)
- {
- Close.Background = (Brush)bc.ConvertFrom("#FFE81123");
- }
- private void Close_MouseLeave(object sender, MouseEventArgs e)
- {
- Close.Background = PrimaryHueDarkBrush;
- }
- private void Max_MouseEnter(object sender, MouseEventArgs e)
- {
- Max.Background = (Brush)bc.ConvertFrom("#FFe6e6e6");
- IconMax.Source = IMaxBlack;
- }
- private void Max_MouseLeave(object sender, MouseEventArgs e)
- {
- IconMax.Source = IMax;
- Max.Background = PrimaryHueDarkBrush;
- }
- private void Min_MouseEnter(object sender, MouseEventArgs e)
- {
- Min.Background = (Brush)bc.ConvertFrom("#FFe6e6e6");
- IconMin.Source = IMinBlack;
- }
- private void Min_MouseLeave(object sender, MouseEventArgs e)
- {
- IconMin.Source = IMin;
- Min.Background = PrimaryHueDarkBrush;
- }
- private void Close_MouseDown(object sender, MouseButtonEventArgs e)
- {
- Application.Current.Shutdown();
- }
- private void Max_MouseDown(object sender, MouseButtonEventArgs e)
- {
- Application.Current.MainWindow.SizeToContent = SizeToContent.Manual;
- if (WindowState != WindowState.Maximized)
- Application.Current.MainWindow.WindowState = WindowState.Maximized;
- else
- Application.Current.MainWindow.WindowState = WindowState.Normal;
- }
- private void Min_MouseDown(object sender, MouseButtonEventArgs e)
- {
- this.WindowState = System.Windows.WindowState.Minimized;
- }
- int x = 0, y = 0;
- System.Windows.Shapes.Polygon p = new System.Windows.Shapes.Polygon();
- System.Windows.Shapes.Polygon p2 = new System.Windows.Shapes.Polygon();
- public void Draw() {
- Clear();
- p.Stroke = Brushes.Black;
- p.Fill = Brushes.LightBlue;
- p.StrokeThickness = 1;
- p.HorizontalAlignment = HorizontalAlignment.Left;
- p.VerticalAlignment = VerticalAlignment.Center;
- //
- p.Points = new PointCollection() {
- new Point(38+x, 162+y),
- new Point(38+x,239+y),
- new Point(92+x,240+y),
- new Point(107+x,214+y),
- new Point(76+x,270+y),
- new Point(111+x,270+y),
- new Point(140+x,214+y),
- new Point(127+x,240+y),
- new Point(247+x,240+y),
- new Point(202+x,198+y),
- new Point(73+x, 198+y),
- new Point(63+x,161+y)
- };
- p2.Stroke = Brushes.Black;
- p2.Fill = Brushes.LightBlue;
- p2.StrokeThickness = 1;
- p2.HorizontalAlignment = HorizontalAlignment.Left;
- p2.VerticalAlignment = VerticalAlignment.Center;
- //
- p2.Points = new PointCollection() {
- new Point(202+x, 198+y),
- new Point(202+x,216+y),
- new Point(221+x,216+y)
- };
- Field.Children.Add(p);
- Field.Children.Add(p2);
- }
- public void Clear()
- {
- Field.Children.Remove(p);
- Field.Children.Remove(p2);
- FieldImage.Source = null;
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- Draw();
- }
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- Clear();
- }
- BitmapImage image;
- private void Button_Click_2(object sender, RoutedEventArgs e)
- {
- OpenFileDialog op = new OpenFileDialog();
- op.Title = "Select a picture";
- op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
- "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
- "Portable Network Graphic (*.png)|*.png";
- if (op.ShowDialog() == true)
- {
- image = new BitmapImage(new Uri(op.FileName));
- FieldImage.Source = image;
- }
- }
- private void Button_Click_3(object sender, RoutedEventArgs e)
- {
- var grayBitmapSource = new FormatConvertedBitmap();
- grayBitmapSource.BeginInit();
- grayBitmapSource.Source = image;
- grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
- grayBitmapSource.EndInit();
- FieldImage.Source = grayBitmapSource;
- }
- private void Window_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Left)
- x -= 1;
- else if (e.Key == Key.Up)
- y -= 1;
- else if (e.Key == Key.Down)
- y += 1;
- else if (e.Key == Key.Right)
- x += 1;
- Draw();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment