מיני פרויקט חלונות תרגיל 3

benny#C, NET.Leave a Comment

מיני פרויקט חלונות תרגיל 3

 

 

MainWindow.xaml

<Window x:Class="dotNetWPF_03_8814_8802.MainWindow"
        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:dotNetWPF_03_8814_8802"
        mc:Ignorable="d"
        Title="MainWindow" Height="400" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="80*"/>
            <RowDefinition Height="20*"/>
        </Grid.RowDefinitions>
        <Grid x:Name="printersGrid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <local:PrinterUserControl />
            <local:PrinterUserControl Grid.Column="1"/>
            <local:PrinterUserControl Grid.Column="2"  Loaded="PrinterUserControl_Loaded"/>


        </Grid>
        <Button x:Name="printButton" Content="Print" FontSize="36" Margin="10,10,10,0" Grid.Row="1" VerticalAlignment="Top" Height="54" Click="printButton_Click"/>

    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 dotNetWPF_03_8814_8802
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window 

    {

        PrinterUserControl CourentPrinter;
        Queue<PrinterUserControl> queue;

        public MainWindow()
        {
            InitializeComponent();
            queue = new Queue<PrinterUserControl>();
            foreach (PrinterUserControl item in printersGrid.Children)
            {
                item.PageMissing += out_Of_Paper;
                item.InkEmpty += out_Of_Ink;
                queue.Enqueue(item);
            }
            CourentPrinter = queue.Dequeue();
        }

        //out of paper function
        private void out_Of_Paper(object sender, PrinterEventArgs e)
        { 
            MessageBox.Show("the Date is :" + e.my_Error_Time + "\nMessage from " + e.my_Printer_Name+":  "  + e.my_Error_Type, " Out Of Paper", MessageBoxButton.OK, MessageBoxImage.Error);
            
            CourentPrinter.addPeges();
            
            queue.Enqueue(CourentPrinter);
            CourentPrinter = queue.Dequeue();
        }

        //out of ink function
        private void out_Of_Ink(object sender, PrinterEventArgs e)
        {
            if (!e.is_Critical)
            {
                MessageBox.Show("Date:" + e.my_Error_Time + "\nmessage from " + e.my_Printer_Name + " : " + e.my_Error_Type, " Low Ink ", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
           
            if (e.is_Critical)
            {
                MessageBox.Show("Date:" + e.my_Error_Time + "\nmessage from " + e.my_Printer_Name + " : " + e.my_Error_Type, " Out Of Ink ", MessageBoxButton.OK, MessageBoxImage.Error);
                CourentPrinter.addInk();
                
                queue.Enqueue(CourentPrinter);
                CourentPrinter = queue.Dequeue();
            }
            
        }

        private void PrinterUserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void printButton_Click(object sender, RoutedEventArgs e)
        {    
            CourentPrinter.print();
        }

        
    }
}

PrinterEventArgs.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dotNetWPF_03_8814_8802
{
    public class PrinterEventArgs
    {
        private bool isCritical;
        private DateTime myErrorTime;
        private String myErrorType;
        private String myPrinterName;


        public  bool is_Critical { get { return isCritical; }  }
        public DateTime my_Error_Time { get { return myErrorTime; } }
        public String my_Error_Type { get { return myErrorType; } }
        public String my_Printer_Name { get { return myPrinterName; } }

       public PrinterEventArgs(bool criti,string errorType,string printerNeme)
        {
            isCritical = criti;
            myErrorType = errorType;
            myPrinterName = printerNeme;
            myErrorTime = DateTime.Now;
        }

    }
}

PrinterUserControl.xaml

<UserControl x:Class="dotNetWPF_03_8814_8802.PrinterUserControl"
             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" 
             xmlns:local="clr-namespace:dotNetWPF_03_8814_8802"
             xmlns:sys="clr-namespace:System;assembly=mscorlib" 
             
             mc:Ignorable="d" BorderThickness="1" BorderBrush="#FF6DB97B">
    <Grid Margin="-5,-4,-3,2" Width="285.333">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Image x:Name="image" HorizontalAlignment="Left" Height="156" Margin="23,10,0,0" VerticalAlignment="Top" Width="239" Source="imeges/printer.jpg"/>

        <Grid HorizontalAlignment="Stretch" Height="auto"
 Grid.Row="2" VerticalAlignment="Stretch" Width="auto" Margin="0,0,0,-0.001" Grid.RowSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="35"/>
                <ColumnDefinition Width="6"/>
                <ColumnDefinition Width="1*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>

            </Grid.RowDefinitions>
            <Label x:Name="pagesLabel" Content=" Pages"  Grid.Row="1" Grid.ColumnSpan="2"   />
            <Label x:Name="inkLabel" Content=" Ink" Grid.ColumnSpan="2" />
            <ProgressBar x:Name="inkCountProgressBar" Grid.Column="2" Value="23.0" Margin="0,0,31,0" MouseEnter="inkCountProgressBar_MouseEnter" Background="#FFDDF7F3" FontFamily="Fgfcg" RenderTransformOrigin="0.5,0.5" >
                <ProgressBar.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform X="5"/>
                    </TransformGroup>
                </ProgressBar.RenderTransform>
                <ProgressBar.Effect>
                    <DropShadowEffect Color="#FFE5FBC5" Direction="314"/>
                </ProgressBar.Effect>
                <ProgressBar.Foreground>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="#FF4DEA12" Offset="1"/>
                    </LinearGradientBrush>
                </ProgressBar.Foreground>
            </ProgressBar>

            <Slider x:Name="pageCountSlider" Grid.Column="2" Grid.Row="1"  Value="200" Maximum="{x:Static local:PrinterUserControl.MaxPages}" MouseEnter="pageCountSlider_MouseEnter" Margin="0,0,23,0" BorderBrush="#FF762121" Foreground="#FFE41616" />
        </Grid>
        <Label x:Name="printerNameLabel" Content="printer Name" HorizontalAlignment="Center"  VerticalAlignment="Center" Foreground="RoyalBlue" FontSize="16" Grid.Row="1" MouseEnter="printerNameLabel_MouseEnter" MouseLeave="printerNameLabel_MouseLeave" />
        <Image x:Name="criticalImeg" HorizontalAlignment="Left" Height="33" Margin="216,123,0,0" VerticalAlignment="Top" Width="34" Source="imeges/criticalError.jpg" Visibility="Hidden"/>

    </Grid>
</UserControl>

PrinterUserControl.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 dotNetWPF_03_8814_8802
{
    /// <summary>
    /// Interaction logic for PrinterUserControl.xaml
    /// </summary>
    public partial class PrinterUserControl : UserControl
    {
        // public  
        public string PrinterName { get { return printerNameLabel.Content.ToString(); } set { printerNameLabel.Content = value; } }
        public double InkCount { get
            {
                string.Format("{0:0.#}", inkCountProgressBar.Value);
                return inkCountProgressBar.Value;
            }

            set { inkCountProgressBar.Value = value; } }
        public int PageCount { get { return (int)pageCountSlider.Value; } set { pageCountSlider.Value = value; } }

        private const double MAX_INK = 100;
        private const double MIN_ADD_INK = MAX_INK / 10;
        private const double MAX_PRINT_INK = 100;
        private const int MAX_PAGES = 400;
        private const int MIN_ADD_PAGES = MAX_PAGES /2;
        private const int MAX_PRINT_PAGES = 40;
        private static int printerNumber = 1;
        Random rand = new Random();
         


       private EventHandler<PrinterEventArgs> page_missing;
       private EventHandler<PrinterEventArgs> ink_empty;

        public PrinterUserControl()
        { 
            InitializeComponent();
            PrinterName = " printer " + printerNumber;
            printerNumber++;
          
        }


        public event EventHandler<PrinterEventArgs> PageMissing   // event for out of paper
        {
            add
            {
                page_missing += value;
            }
            remove
            {
                page_missing -= value;
            }
        }

        public event EventHandler<PrinterEventArgs> InkEmpty    // event for low ink\out of ink
        {
            add
            {
                ink_empty += value;
            }
            remove
            {
                ink_empty -= value;
            }
        }

       

        private void printerNameLabel_MouseEnter(object sender, MouseEventArgs e)
        {
            printerNameLabel.FontSize *= 2;
        }

        private void printerNameLabel_MouseLeave(object sender, MouseEventArgs e)
        {
            printerNameLabel.FontSize /= 2;
        }

        private void inkCountProgressBar_MouseEnter(object sender, MouseEventArgs e)
        {
            inkCountProgressBar.ToolTip = inkCountProgressBar.Value.ToString("f1") +" %";
        }

        public void print()
        {
            int x = rand.Next(1, MAX_PRINT_PAGES);     //  rondom page num to print

            if (PageCount - x <= 0)                    // not hav the require amount of paper to print (out of paper)
            {
                int how_many_paege_missing = x - PageCount;   // how many pages missing for the corrent printing
                PageCount = 0;
                pagesLabel.Foreground = Brushes.Red;         // the  pagesLabel will be show in red color
                criticalImeg.Visibility = Visibility.Visible;  // show the imeg for critical stastus
                PrinterEventArgs outOfPaper = new PrinterEventArgs(true, how_many_paege_missing + " pages is missing for last print", PrinterName);
                page_missing(this, outOfPaper);    // event activation
                return;
            }
            else
                PageCount -= x;

            x = rand.Next(1,(int)MAX_PRINT_INK/10);    //  rondom % of ink to use for printing (divdin by 10 for slower progress)
            double y = rand.NextDouble();   // to get a double random number
            double temp = x + y;

            if (InkCount - temp > 0)
            {

                
                if (InkCount - temp >= 10 && InkCount - temp <= 15)
                {
                    InkCount -= temp;
                    inkLabel.Foreground = Brushes.Yellow;                                                      
                    PrinterEventArgs outOfInk = new PrinterEventArgs(false, "your ink is only  " + InkCount.ToString("f1") + " %", PrinterName);
                    ink_empty(this, outOfInk);       // event activation
                    return;
                }
                if (InkCount - temp >= 1 && InkCount - temp <= 10)
                {
                    InkCount -= temp;
                    inkLabel.Foreground = Brushes.Orange;           
                    PrinterEventArgs outOfInk = new PrinterEventArgs(false, "your ink is only  " + InkCount.ToString("f1") + " %", PrinterName);
                    ink_empty(this, outOfInk);         // event activation
                    return;
                }

                InkCount -= temp;
            }
            else
            {
                InkCount = 0;
                criticalImeg.Visibility = Visibility.Visible;
                ink_empty(this, new PrinterEventArgs(true,  " printer OUT OF INK", PrinterName));
            }
           


        }

        public void addPeges()
        {
            int x = rand.Next((int)MAX_PRINT_INK);
           
            if (x + PageCount > MAX_PAGES)
                PageCount = MAX_PAGES;
            else
                PageCount += x;
            pagesLabel.Foreground = Brushes.Black;
            criticalImeg.Visibility = Visibility.Hidden;

        }

        public void addInk()
        {
            
            int x = rand.Next((int)MAX_PRINT_INK);
            double y = rand.NextDouble();
            double temp = x + y;                   // to get a double random number

            if (temp + InkCount > MAX_PAGES)
                InkCount = MAX_INK;
            else
                InkCount += temp;
            inkLabel.Foreground = Brushes.Black;
            criticalImeg.Visibility = Visibility.Hidden;

        }

        private void pageCountSlider_MouseEnter(object sender, MouseEventArgs e)
        {
            pageCountSlider.ToolTip = pageCountSlider.Value;
        }
        public static double MaxPages { get { return MAX_PAGES; } }  // additinal to part B
    }
}

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *