学无先后,达者为师

网站首页 编程语言 正文

C#中WPF颜色对话框控件的实现_C#教程

作者:和煦的糖果风`   更新时间: 2022-05-24 编程语言

在 C# WPF开发中颜色对话框控件(ColorDialog)用于对界面中的背景、文字…(拥有颜色属性的所有控件)设置颜色,例如设置标签控件的背景色。

颜色对话框的运行效果如下图所示:

标签背景色设置后如下:

xml代码:


    
        
            
        
    

    
        
    

    
        
    
    
        
            
                
                    
                        
                            
                        
                    
                    
                        
                            
                        
                    
                    
                        
                            
                        
                    
                    
                    
                        
                            
                        
                    
                    
                        
                            
                        
                    
                    
                        
                            
                        
                    
                
                
                    
                        
                            
                        
                    
                    
                        
                            
                        
                    
                    
                        
                            
                        
                    
                    
                    
                        
                            
                        
                    
                    
                        
                            
                        
                    
                    
                    
                        
                        
                        
                        
                        
                    
                
            
            
        
    

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
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    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));
            }
        }
    }
}

原文链接:https://blog.csdn.net/m0_51460728/article/details/122274209

栏目分类
最近更新