- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
- [C#] Hướng dẫn Copy With All Property in Class Object
- [DEVEXPRESS] Hướng dẫn load Json DataSource vào GridView
- [C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
- [DEVEXPRESS] Format code T-SQL highlight in RichEditControl
- [C#] Hướng dẫn upload file, hình ảnh từ Winform lên server API ASP.NET Core
[C#] Chia sẽ thư viện sử dụng Color Picker trong Winform
Xin chào các bạn bài viết hôm nay mình sẽ chia sẽ đến các thư viện Color Picker dùng để lấy mã màu trong lập trình C# Winform.
[C#] Color picker in winform
Trong lập trình Winform, ở thanh công cụ các bạn sẽ có một control Color Dialog.
Color Dialog là một control cho phép bạn chọn màu hiển thị ở dạng cửa sổ hộp thoại Dialog.
Tuy nhiên, bây giờ các bạn muốn có cung cụ màu nằm ở Winform luôn, thì các bạn có thể sử dụng thư viên Color Picker này.
Thư viện Color Picker cung cấp cho chúng ta 4 Component để chọn lựa:
Bao gồm 4 loại:
- ColorBox2D
- ColorHexagon
- ColorSoliderVertical
- ColorWheel
Dưới đây là giao diện demo sử dụng các loại màu sắc:
Source code demo ứng dụng Color Picker C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MechanikaDesign.WinForms.UI.ColorPicker;
namespace ColorHexagon.Demo
{
public partial class FormColorPickerDemo : Form
{
#region Fields
private HslColor colorHsl = HslColor.FromAhsl(0xff);
private ColorModes colorMode = ColorModes.Hue;
private Color colorRgb = Color.Empty;
private bool lockUpdates = false;
#endregion
public FormColorPickerDemo()
{
InitializeComponent();
this.colorBox2D.ColorMode = this.colorMode;
this.colorSlider.ColorMode = this.colorMode;
}
private void colorHexagon_ColorChanged(object sender, ColorChangedEventArgs args)
{
labelCurrentColor.BackColor = colorHexagon.SelectedColor;
textboxHexColor.Text = ColorTranslator.ToHtml(colorHexagon.SelectedColor);
}
private void colorWheel_ColorChanged(object sender, EventArgs e)
{
labelCurrentColor.BackColor = colorWheel.Color;
textboxHexColor.Text = ColorTranslator.ToHtml(colorWheel.Color);
}
private void colorSlider_ColorChanged(object sender, ColorChangedEventArgs args)
{
if (!this.lockUpdates)
{
HslColor colorHSL = this.colorSlider.ColorHSL;
this.colorHsl = colorHSL;
this.colorRgb = this.colorHsl.RgbValue;
this.lockUpdates = true;
this.colorBox2D.ColorHSL = this.colorHsl;
this.lockUpdates = false;
labelCurrentColor.BackColor = this.colorRgb;
textboxHexColor.Text = ColorTranslator.ToHtml(this.colorRgb);
UpdateColorFields();
}
}
private void colorBox2D_ColorChanged(object sender, ColorChangedEventArgs args)
{
if (!this.lockUpdates)
{
HslColor colorHSL = this.colorBox2D.ColorHSL;
this.colorHsl = colorHSL;
this.colorRgb = this.colorHsl.RgbValue;
this.lockUpdates = true;
this.colorSlider.ColorHSL = this.colorHsl;
this.lockUpdates = false;
labelCurrentColor.BackColor = this.colorRgb;
textboxHexColor.Text = ColorTranslator.ToHtml(this.colorRgb);
UpdateColorFields();
}
}
private void ColorModeChangedHandler(object sender, EventArgs e)
{
if (sender == this.radioRed)
{
this.colorMode = ColorModes.Red;
}
else if (sender == this.radioGreen)
{
this.colorMode = ColorModes.Green;
}
else if (sender == this.radioBlue)
{
this.colorMode = ColorModes.Blue;
}
else if (sender == this.radioHue)
{
this.colorMode = ColorModes.Hue;
}
else if (sender == this.radioSaturation)
{
this.colorMode = ColorModes.Saturation;
}
else if (sender == this.radioLuminance)
{
this.colorMode = ColorModes.Luminance;
}
this.colorSlider.ColorMode = this.colorMode;
this.colorBox2D.ColorMode = this.colorMode;
}
private void UpdateColorFields()
{
this.lockUpdates = true;
this.numRed.Value = this.colorRgb.R;
this.numGreen.Value = this.colorRgb.G;
this.numBlue.Value = this.colorRgb.B;
this.numHue.Value = (int)(((decimal)this.colorHsl.H) * 360M);
this.numSaturation.Value = (int)(((decimal)this.colorHsl.S) * 100M);
this.numLuminance.Value = (int)(((decimal)this.colorHsl.L) * 100M);
this.lockUpdates = false;
}
private void UpdateRgbFields(Color newColor)
{
this.colorHsl = HslColor.FromColor(newColor);
this.colorRgb = newColor;
this.lockUpdates = true;
this.numHue.Value = (int)(((decimal)this.colorHsl.H) * 360M);
this.numSaturation.Value = (int)(((decimal)this.colorHsl.S) * 100M);
this.numLuminance.Value = (int)(((decimal)this.colorHsl.L) * 100M);
this.lockUpdates = false;
this.colorSlider.ColorHSL = this.colorHsl;
this.colorBox2D.ColorHSL = this.colorHsl;
}
private void UpdateHslFields(HslColor newColor)
{
this.colorHsl = newColor;
this.colorRgb = newColor.RgbValue;
this.lockUpdates = true;
this.numRed.Value = this.colorRgb.R;
this.numGreen.Value = this.colorRgb.G;
this.numBlue.Value = this.colorRgb.B;
this.lockUpdates = false;
this.colorSlider.ColorHSL = this.colorHsl;
this.colorBox2D.ColorHSL = this.colorHsl;
}
private void numRed_ValueChanged(object sender, EventArgs e)
{
if (!this.lockUpdates)
{
UpdateRgbFields(Color.FromArgb((int)this.numRed.Value, (int)this.numGreen.Value, (int)this.numBlue.Value));
}
}
private void numGreen_ValueChanged(object sender, EventArgs e)
{
if (!this.lockUpdates)
{
UpdateRgbFields(Color.FromArgb((int)this.numRed.Value, (int)this.numGreen.Value, (int)this.numBlue.Value));
}
}
private void numBlue_ValueChanged(object sender, EventArgs e)
{
if (!this.lockUpdates)
{
UpdateRgbFields(Color.FromArgb((int)this.numRed.Value, (int)this.numGreen.Value, (int)this.numBlue.Value));
}
}
private void numHue_ValueChanged(object sender, EventArgs e)
{
if (!this.lockUpdates)
{
HslColor newColor = HslColor.FromAhsl((double)(((float)((int)this.numHue.Value)) / 360f), this.colorHsl.S, this.colorHsl.L);
this.UpdateHslFields(newColor);
}
}
private void numSaturation_ValueChanged(object sender, EventArgs e)
{
if (!this.lockUpdates)
{
HslColor newColor = HslColor.FromAhsl(this.colorHsl.A, this.colorHsl.H, (double)(((float)((int)this.numSaturation.Value)) / 100f), this.colorHsl.L);
this.UpdateHslFields(newColor);
}
}
private void numLuminance_ValueChanged(object sender, EventArgs e)
{
if (!this.lockUpdates)
{
HslColor newColor = HslColor.FromAhsl(this.colorHsl.A, this.colorHsl.H, this.colorHsl.S, (double)(((float)((int)this.numLuminance.Value)) / 100f));
this.UpdateHslFields(newColor);
}
}
}
}
Thanks for watching!