一、WPF字体颜色TO WINFORM 格式及WINFORM 字体颜色TO WPF 格式
-
private void setFontDialog()
-
{
-
System.Windows.Forms.FontDialog fontDialog = new System.Windows.Forms.FontDialog();
-
fontDialog.ShowColor = true;
-
Control currCtrl = ((Control)this.mSelectedElem);
-
-
/* WPF 字体颜色 TO WINFORM 格式 */
-
System.Drawing.Font font = new System.Drawing.Font(currCtrl.FontFamily.ToString(), (float)currCtrl.FontSize);
-
fontDialog.Font = font;
-
byte r = ((SolidColorBrush)currCtrl.Foreground).Color.R;
-
byte g = ((SolidColorBrush)currCtrl.Foreground).Color.G;
-
byte b = ((SolidColorBrush)currCtrl.Foreground).Color.B;
-
fontDialog.Color = System.Drawing.Color.FromArgb(r,g,b);
-
fontDialog.ShowDialog();
-
-
/* Reset font and forecolor to selectedelem */
-
/* WINFORM 字体颜色 TO WPF 格式 */
-
if (currCtrl is Label)
-
{
-
Label currLbl = (Label)currCtrl;
-
-
/* 设置字体名、字体粗体与斜体写法 */
-
string fontName = fontDialog.Font.FontFamily.Name;
-
currLbl.FontFamily = new System.Windows.Media.FontFamily(fontName);
-
currLbl.FontSize = fontDialog.Font.Size;
-
//!!!!!!!!!!
-
if(fontDialog.Font.Bold)
-
currLbl.FontWeight = FontWeights.Bold;
-
if (fontDialog.Font.Italic)
-
currLbl.FontStyle = FontStyles.Italic;
-
-
/* 字体颜色 */
-
System.Windows.Media.SolidColorBrush scBrush = new System.Windows.Media.SolidColorBrush();
-
System.Windows.Media.Color clr = new System.Windows.Media.Color();
-
-
clr.A = fontDialog.Color.A;
-
clr.B = fontDialog.Color.B;
-
clr.G = fontDialog.Color.G;
-
clr.R = fontDialog.Color.R;
-
scBrush.Color = clr;
-
currLbl.Foreground = scBrush;
-
}
-
}
二、WPF背景色TO WINFORM背景色及WINFORM背景色TO WPF背景色
-
private void setColor()
-
{
-
System.Windows.Forms.ColorDialog clrDialog = new System.Windows.Forms.ColorDialog();
-
clrDialog.AllowFullOpen = true;
-
clrDialog.FullOpen = true;
-
clrDialog.ShowHelp = false;
-
-
/* WPF 背景色 TO WINFORM 背景色 */
-
Control currCtrl = ((Control)this.mSelectedElem);
-
byte r = ((SolidColorBrush)currCtrl.Background).Color.R;
-
byte g = ((SolidColorBrush)currCtrl.Background).Color.G;
-
byte b = ((SolidColorBrush)currCtrl.Background).Color.B;
-
-
clrDialog.Color = System.Drawing.Color.FromArgb(r, g, b);
-
clrDialog.ShowDialog();
-
-
/* WINFORM 背景色 TO WPF 背景色 */
-
System.Windows.Media.SolidColorBrush scBrush = new System.Windows.Media.SolidColorBrush();
-
System.Windows.Media.Color clr = new System.Windows.Media.Color();
-
-
clr.A = clrDialog.Color.A;
-
clr.B = clrDialog.Color.B;
-
clr.G = clrDialog.Color.G;
-
clr.R = clrDialog.Color.R;
-
scBrush.Color = clr;
-
-
if (currCtrl is Label)
-
{
-
currCtrl.Background = scBrush;
-
}
-
}
相关参考代码:
-
WPF中将System.Drawing.Color转换为System.Media.Brush
-
System.Windows.Forms.ColorDialog cl = new System.Windows.Forms.ColorDialog();
-
if (cl.ShowDialog() == System.Windows.Forms.DialogResult.OK)
-
{
-
Brush br = new SolidColorBrush(Color.FromArgb(cl.Color.A, cl.Color.R, cl.Color.G, cl.Color.B));
-
}
-
-
//color转为brush:
-
Brush br = new SolidColorBrush(Color.FromRgb(0,0,0));
-
//string转Color
-
(Color)ColorConverter.ConvertFromString((string)str);
-
-
//Color转string((Color)value).ToString();
-
string和Brush的转换
-
Brush color = newSolidColorBrush((Color)ColorConverter.ConvertFromString((string)str));
-
-
//Brush转string
-
((Brush)value).ToString();
-
//string转byte[]
-
System.Text.UnicodeEncoding converter = newSystem.Text.UnicodeEncoding();
-
-
byte[] stringBytes = converter.GetBytes(inputString);
-
//byte[]转string
-
System.Text.UnicodeEncoding converter = newSystem.Text.UnicodeEncoding();
-
stringoutputString = converter.GetString(stringByte);
-
-
-
1.由string的rgb数值"255,255,0"转换为color
-
{
-
string[] color_params = e.Parameter.ToString().Split(',');
-
byte color_R = Convert.ToByte(color_params[0]);
-
byte color_G = Convert.ToByte(color_params[1]);
-
byte color_B = Convert.ToByte(color_params[2]);
-
}
-
2.由颜色名称字符串("black") 转化为color
-
{
-
//ColorConverter c = new ColorConverter();
-
//object obj = c.ConvertFrom();
-
//Color color = (Color)obj;
-
Color color = Color.FromRgb(color_R, color_G, color_B);
-
}
-
3.将blend的 8位颜色值转为color
-
///
-
-
/// 将blend的8位颜色值转为color
-
///
-
-
///
-
///
-
public Color ToColor(string colorName)
-
{
-
if (colorName.StartsWith("#"))
-
colorName = colorName.Replace("#", string.Empty);
-
int v = int.Parse(colorName, System.Globalization.NumberStyles.HexNumber);
-
return new Color()
-
{
-
A = Convert.ToByte((v >> 24) & 255),
-
R = Convert.ToByte((v >> 16) & 255),
-
G = Convert.ToByte((v >> 8) & 255),
-
B = Convert.ToByte((v >> 0) & 255)
-
};
-
}
参考网址:
http://blog.163.com/dream_perfect@yeah/blog/static/11625921420105732937606/
http://blog.csdn.net/hwt0101/article/details/8090987
阅读(4297) | 评论(0) | 转发(0) |