Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12433099
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C#/.net

2015-12-11 15:00:11

一、WPF字体颜色TO WINFORM 格式及WINFORM 字体颜色TO  WPF 格式


  1. private void setFontDialog()
  2.         {
  3.             System.Windows.Forms.FontDialog fontDialog = new System.Windows.Forms.FontDialog();
  4.             fontDialog.ShowColor = true;
  5.             Control currCtrl = ((Control)this.mSelectedElem);

  6.             /* WPF 字体颜色 TO WINFORM 格式 */
  7.             System.Drawing.Font font = new System.Drawing.Font(currCtrl.FontFamily.ToString(), (float)currCtrl.FontSize);
  8.             fontDialog.Font = font;
  9.             byte r = ((SolidColorBrush)currCtrl.Foreground).Color.R;
  10.             byte g = ((SolidColorBrush)currCtrl.Foreground).Color.G;
  11.             byte b = ((SolidColorBrush)currCtrl.Foreground).Color.B;
  12.             fontDialog.Color = System.Drawing.Color.FromArgb(r,g,b);
  13.             fontDialog.ShowDialog();

  14.             /* Reset font and forecolor to selectedelem */
  15.             /* WINFORM 字体颜色 TO WPF 格式 */
  16.             if (currCtrl is Label)
  17.             {
  18.                 Label currLbl = (Label)currCtrl;

  19.                 /* 设置字体名、字体粗体与斜体写法 */
  20.                 string fontName = fontDialog.Font.FontFamily.Name;
  21.                 currLbl.FontFamily = new System.Windows.Media.FontFamily(fontName);
  22.                 currLbl.FontSize = fontDialog.Font.Size;
  23.                 //!!!!!!!!!!
  24.                 if(fontDialog.Font.Bold)
  25.                     currLbl.FontWeight = FontWeights.Bold;
  26.                 if (fontDialog.Font.Italic)
  27.                     currLbl.FontStyle = FontStyles.Italic;

  28.                 /* 字体颜色 */
  29.                 System.Windows.Media.SolidColorBrush scBrush = new System.Windows.Media.SolidColorBrush();
  30.                 System.Windows.Media.Color clr = new System.Windows.Media.Color();

  31.                 clr.A = fontDialog.Color.A;
  32.                 clr.B = fontDialog.Color.B;
  33.                 clr.G = fontDialog.Color.G;
  34.                 clr.R = fontDialog.Color.R;
  35.                 scBrush.Color = clr;
  36.                 currLbl.Foreground = scBrush;
  37.             }
  38.         }


二、WPF背景色TO WINFORM背景色及WINFORM背景色TO  WPF背景色


  1. private void setColor()
  2.         {
  3.             System.Windows.Forms.ColorDialog clrDialog = new System.Windows.Forms.ColorDialog();
  4.             clrDialog.AllowFullOpen = true;
  5.             clrDialog.FullOpen = true;
  6.             clrDialog.ShowHelp = false;
  7.             
  8.             /* WPF 背景色 TO WINFORM 背景色 */
  9.             Control currCtrl = ((Control)this.mSelectedElem);
  10.             byte r = ((SolidColorBrush)currCtrl.Background).Color.R;
  11.             byte g = ((SolidColorBrush)currCtrl.Background).Color.G;
  12.             byte b = ((SolidColorBrush)currCtrl.Background).Color.B;

  13.             clrDialog.Color = System.Drawing.Color.FromArgb(r, g, b);
  14.             clrDialog.ShowDialog();

  15.             /* WINFORM 背景色 TO WPF 背景色 */
  16.             System.Windows.Media.SolidColorBrush scBrush = new System.Windows.Media.SolidColorBrush();
  17.             System.Windows.Media.Color clr = new System.Windows.Media.Color();

  18.             clr.A = clrDialog.Color.A;
  19.             clr.B = clrDialog.Color.B;
  20.             clr.G = clrDialog.Color.G;
  21.             clr.R = clrDialog.Color.R;
  22.             scBrush.Color = clr;

  23.             if (currCtrl is Label)
  24.             {
  25.                 currCtrl.Background = scBrush;
  26.             }
  27.         }


相关参考代码:


  1. WPF中将System.Drawing.Color转换为System.Media.Brush
  2. System.Windows.Forms.ColorDialog cl = new System.Windows.Forms.ColorDialog();
  3.             if (cl.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  4.             {
  5.                 Brush br = new SolidColorBrush(Color.FromArgb(cl.Color.A, cl.Color.R, cl.Color.G, cl.Color.B));
  6.             }

  7. //color转为brush:
  8. Brush br = new SolidColorBrush(Color.FromRgb(0,0,0));
  9. //string转Color
  10. (Color)ColorConverter.ConvertFromString((string)str);

  11. //Color转string((Color)value).ToString();
  12. string和Brush的转换
  13. Brush color = newSolidColorBrush((Color)ColorConverter.ConvertFromString((string)str));

  14. //Brush转string
  15. ((Brush)value).ToString();
  16. //string转byte[]
  17. System.Text.UnicodeEncoding converter = newSystem.Text.UnicodeEncoding();

  18. byte[] stringBytes = converter.GetBytes(inputString);
  19. //byte[]转string
  20. System.Text.UnicodeEncoding converter = newSystem.Text.UnicodeEncoding();
  21. stringoutputString = converter.GetString(stringByte);

  22.   
  23. 1.由string的rgb数值"255,255,0"转换为color
  24. {
  25.   string[] color_params = e.Parameter.ToString().Split(',');
  26.   byte color_R = Convert.ToByte(color_params[0]);
  27.   byte color_G = Convert.ToByte(color_params[1]);
  28.   byte color_B = Convert.ToByte(color_params[2]);
  29. }
  30. 2.由颜色名称字符串("black") 转化为color
  31. {
  32.   //ColorConverter c = new ColorConverter();
  33.   //object obj = c.ConvertFrom();
  34.   //Color color = (Color)obj;
  35.   Color color = Color.FromRgb(color_R, color_G, color_B);
  36. }
  37. 3.将blend的 8位颜色值转为color
  38.         ///

  39.         /// 将blend的8位颜色值转为color
  40.         ///

  41.         ///
  42.         ///
  43.         public Color ToColor(string colorName)
  44.         {
  45.             if (colorName.StartsWith("#"))
  46.                 colorName = colorName.Replace("#", string.Empty);
  47.             int v = int.Parse(colorName, System.Globalization.NumberStyles.HexNumber);
  48.             return new Color()
  49.             {
  50.                 A = Convert.ToByte((v >> 24) & 255),
  51.                 R = Convert.ToByte((v >> 16) & 255),
  52.                 G = Convert.ToByte((v >> 8) & 255),
  53.                 B = Convert.ToByte((v >> 0) & 255)
  54.             };
  55.         }



参考网址:

http://blog.163.com/dream_perfect@yeah/blog/static/11625921420105732937606/

http://blog.csdn.net/hwt0101/article/details/8090987

阅读(4190) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~