首頁 > 軟體

C#中WPF顏色對話方塊控制元件的實現

2022-03-22 13:01:22

在 C# WPF開發中顏色對話方塊控制元件(ColorDialog)用於對介面中的背景、文字…(擁有顏色屬性的所有控制元件)設定顏色,例如設定標籤控制元件的背景色。

顏色對話方塊的執行效果如下圖所示:

標籤背景色設定後如下:

xml程式碼:

<Window x:Class="WpfApp.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:WpfApp"
        Icon="https://demosc.chinaz.net/Files/pic/iconsico/8254/e9.ico"
        mc:Ignorable="d"
        Title="TestImage" Height="600" Width="900" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <ResourceDictionary x:Uid="CommandDict">
            <RoutedCommand x:Uid="SetBackGround" x:Key="SetBackGround"/>
        </ResourceDictionary>
    </Window.Resources>

    <Window.CommandBindings>
        <CommandBinding x:Uid="SetBackGroundParameter" Command="{StaticResource SetBackGround}" Executed="Pic_BackGround_Click" />
    </Window.CommandBindings>

    <Window.InputBindings>
        <KeyBinding x:Uid="AddKeyBinding" Key="O" Modifiers="Ctrl+Shift" Command="{StaticResource SetBackGround}"/>
    </Window.InputBindings>
    <Grid>
        <DockPanel x:Name="myDockPanel">
            <Menu DockPanel.Dock="Top" Height="30" Margin="0,0,0.333,0" VerticalAlignment="Top" HorizontalAlignment="Left">
                <MenuItem Header="檔案"  Height="30" Background="Violet">
                    <MenuItem Header="開啟" InputGestureText="Ctrl+0">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/7595/j14.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="儲存" InputGestureText="Ctrl+S">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/7595/j13.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="另存為" InputGestureText="F12">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/7595/j12.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <Separator/>
                    <MenuItem Header="預覽列印">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/7595/j11.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="列印" InputGestureText="Ctrl+P">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/7595/j10.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="退出" InputGestureText="F4">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/7595/j9.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                </MenuItem>
                <MenuItem Header="編輯" Height="30" Background="Aqua">
                    <MenuItem Header="背景圖片" InputGestureText="Ctrl + Shift + O" Click="Pic_BackGround_Click">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/8255/b8.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="恢復" InputGestureText="Ctrl+Y">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/8255/b1.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="重做" InputGestureText="Ctrl+Z">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/8255/b2.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <Separator/>
                    <MenuItem Header="複製" InputGestureText="Ctrl+C">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/8255/b4.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="貼上" InputGestureText="Ctrl+V">
                        <MenuItem.Icon>
                            <Image Source="https://demosc.chinaz.net/Files/pic/iconsico/8255/b5.ico"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <Separator/>
                    <MenuItem Header="字型">
                        <MenuItem Header="黑體" InputGestureText="Ctrl+B" IsCheckable="True"/>
                        <MenuItem Header="斜體" InputGestureText="Ctrl+I" IsCheckable="True"/>
                        <Separator/>
                        <MenuItem Header="增大字型" IsCheckable="True"/>
                        <MenuItem Header="縮小字型" IsCheckable="True"/>
                    </MenuItem>
                </MenuItem>
            </Menu>
            <Label x:Name="colorLabel" DockPanel.Dock="Left" Width="200" Height="100" Background="AntiqueWhite" MouseDoubleClick="ColorLabel_MouseDoubleClick"></Label>
        </DockPanel>
    </Grid>
</Window>

c#程式碼:

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Forms;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Pic_BackGround_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog pic = new OpenFileDialog();
            pic.Title = "設定背景圖片";
            pic.Filter = "圖形檔案(*.jpg)|*.jpg";
            if (pic.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ImageBrush p = new ImageBrush(new BitmapImage(new Uri(pic.FileName, UriKind.Absolute)));
                Console.WriteLine(pic.FileName);
                myDockPanel.Background = p;
            }
        }

        private void ColorLabel_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ColorDialog cl = new ColorDialog();
            if(cl.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                colorLabel.Background = new SolidColorBrush(Color.FromArgb(cl.Color.A,cl.Color.R,cl.Color.G,cl.Color.B));
            }
        }
    }
}

到此這篇關於C#中WPF顏色對話方塊控制元件的實現的文章就介紹到這了,更多相關C# 顏色對話方塊控制元件內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com