728x90
반응형

[마우스제어]

[xaml]

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="486.8" >
    <StackPanel>
        <Rectangle x:Name="rect01"
            Fill="Green" Height="100" Stroke="Black"
                   MouseEnter="OnMouseEnter"
                   MouseLeave="OnMouseLeave"
                   MouseMove="OnMouseMove"
                   MouseDown="InMouseDown"
                   Margin="40"
                   />
        <TextBlock x:Name="textBlock" 
                   TextWrapping="Wrap"
                   Height="40"
                   />
        <TextBlock x:Name="textBlock1" 
                   TextWrapping="Wrap"
                   Height="40"
                   />
        <TextBlock x:Name="textBlock2" 
                   TextWrapping="Wrap"
                   Height="40"
                   />

    </StackPanel>
</Window>

 

[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;
using System.ComponentModel;
using System.Drawing;


namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        private void OnMouseEnter(object sender, MouseEventArgs e)
        {
            Rectangle rect = e.Source as Rectangle;
            if(rect != null)
            {
                rect.Fill = Brushes.Blue;
            }
            textBlock.Text = "MouseEnter";
        }

        private void OnMouseLeave(object sender, MouseEventArgs e)
        {
            Rectangle rect = e.Source as Rectangle;
            if (rect != null)
            {
                rect.Fill = Brushes.Green;
            }
            textBlock.Text = "MouseLeave";
            textBlock1.Text = "";
        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(rect01);
            textBlock1.Text = "Mouse Move : " + pt.ToString();
        }

        private void InMouseDown(object sender, MouseButtonEventArgs e)
        {
            Rectangle rect = e.Source as Rectangle;
            Point pt = e.GetPosition(rect01);
            textBlock2.Text = "Mouse Down : " + pt.ToString();

            if (rect != null)
            {
                rect.Fill = Brushes.Red;
            }
        }
    }
}


[키보드제어]

[xaml]

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="486.8" >
    <StackPanel Orientation="Horizontal"
                KeyDown="OnTextInputKeyDown">
        <TextBox Width="350" Height="30" Margin="10"/>
        <Button Click="OnTextInputButton_Click" 
                Content="Open" Margin="10" Width="50" Height="30" />

    </StackPanel>
</Window>

 

[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;
using System.ComponentModel;
using System.Drawing;


namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        private void OnTextInputKeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.A)
            {
                MessageBox.Show("KeyDown");
            }
        }

        private void OnTextInputButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("TextInputButton_Click"); 
        }
    }
}


[RoutedCommands: 메뉴만들기]

[xaml]

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="486.8" >
    <Grid>
        <StackPanel x:Name="stack" Background="Transparent">
            <StackPanel.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="New" Command="New" />
                    <MenuItem Header="Open" Command="Open" />
                    <MenuItem Header="Save" Command="Save" />
                </ContextMenu>
            </StackPanel.ContextMenu>
            
            <Menu>
                <MenuItem Header="File" >
                    <MenuItem Header="New" Command="New" />
                    <MenuItem Header="Open" Command="Open" />
                    <MenuItem Header="Save" Command="Save" />
                </MenuItem>
            </Menu>
        </StackPanel>
    </Grid>
</Window>

 

[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;
using System.ComponentModel;
using System.Drawing;


namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            CommandBindings.Add(new CommandBinding(ApplicationCommands.New, NewExecuted, CanNew));
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, NewExecuted, CanOpen));
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Save, NewExecuted, CanSave));

        }

        private void NewExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("You want to create new file.");
        }

        private void CanNew(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void OpenExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("You want to open existing file.");
        }

        private void CanOpen(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void SaveExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("You want to save a file.");
        }
        private void CanSave(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

    }
}

728x90
반응형

+ Recent posts