- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
[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!