将字符串转换为字体 [英] Converting a String to Font

查看:96
本文介绍了将字符串转换为字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我希望能够将FontDialog窗口生成的字符串转换回字体。



我需要存储的东西FontDialog生成的信息到Listbox中。列表框只接受字符串而不是字体,因此我必须将字体转换为字符串。那没问题。转换为字符串时字体的输出如下:



[字体:名称= Corbel,大小= 14.25,单位= 3,GdiCharSet = 0,GdiVerticalFont = False]



现在我要做的就是将上面的字符串转换回字体,但是不像将整数转换为颜色,它似乎有点难度而且我正在努力。



请有人可以粉碎一些灯并帮助我吗?



谢谢你:)

解决方案

字符串和字体无关,无法以任何明智的方式进行转换。



你可以解析一些文本信息并基于它创建一个字体,请参阅类 System.Drawing.Font 及其构造函数 http://msdn.microsoft.com/en-us/library/system.drawing.font.aspx [ ^ ]。



-SA


有人创建了 FontConverter 类,允许您将Font转换为字符串和从字符串转换(Font类是可序列化的)。它非常简单......您只需从XML字符串序列化和反序列化。本文还有另一个示例: .NET XML序列化 - 设置类


var fC = new FontConverter(); //< - =酷酷的技巧,现在看...



//来自一个字符串是:

PrintFont = fC.ConvertFromString (yourString)as Font;



//字符串是:

string yourString = fC.ConvertToString(PrintFont);





//现在如果你想恢复所说的字体的脚本。

//有再多做几步。首先,你必须保存价值。

PrintSettings.Default.GdiCharSet = _font.Dialog.Font.GdiCharSet; //字节类型。



//或

byte gdiValue = PrintFont.GdiCharSet; //类似的东西。



// OR,你可以把它存储为一个int:

int gdiValue = PrintFont.GdiCharSet。 GetHashCode();



//但是无论如何,脚本都没有带回字体是否

//你把它保存为文本文件字符串或Settings.setting,无论如何。

// byte或int。并且_fontDialog.Font.GdiCharSet没有设置。所以你是

//几乎搞砸了......(原文如此)......除非你按我的方式去做。 LoL jk ...



//所以你使用上面的内容重新加载你的PrintFont。但是你想要

//来恢复已保存字体的脚本。保存了GdiCharSet

//值后,您只需将它们放在一起即可。我的方法简单如下:



PrintFont = GetFontWithScript();



//之后^电话,你需要做的就是给它分配:

_fontDialog.Font = PrintFont; //你将完全被设置。



//这就是它如何将两者组合在一起。



private Font GetFontWithScript()

{

PrintFont = PrintSettings.Default.PrintFont;

var name = PrintFont.Name;

var size = PrintFont.Size;

var style = PrintFont.Style;

var unit = PrintFont.Unit;

var charSet = PrintSettings.Default.GdiCharSet;



返回新字体(名称,大小,样式,单位,字符集);

/ *

*字体(String,Single,FontStyle,GraphicsUnit,Byte)

*使用指定的大小,样式,单位和字符集初始化一个新字体。 />
*

* /

}



//此外,转换颜色一个字符串和后面就是这么简单。

//哪里......

System.Drawing.Color _your颜色; //从FontDialog或其他任何地方分配它......

//然后......

string colorString = _yourColor.ToArgb()。ToString();



//然后,将字符串转换为颜色只是...

_yourColor = Color.FromArgb(Convert.ToInt32(colorString));



//或者只是使用int或byte并忘记字符串。 ; - )

I have a problem, I want to be able to convert a string generated by the FontDialog window back into a font.

The thing is I have to store the information generated by the FontDialog into a Listbox. Listboxes only accepts Strings not fonts, therefore I have to convert the font into a string. That is no problem. The output of the font when converted to a string is as follows:

[Font: Name=Corbel, Size=14.25, Units=3, GdiCharSet=0, GdiVerticalFont=False]

Now what I want to do is convert the above string back into a font, however unlike converting integers to colors it seems alot harder and I am struggling.

Please can anyone shred some light and help me?

Thank-You :)

解决方案

String and Font has nothing to do in common, cannot be converted in any sensible way.

You could parse some text information and create a font based on it, see the class System.Drawing.Font and its constructors, http://msdn.microsoft.com/en-us/library/system.drawing.font.aspx[^].

—SA


Somebody has created a FontConverter class that allows you to convert the Font to and from a string (the Font class is serializable). It's pretty simple... you just serialize to and deserialize from an XML string. There is another example in this article: .NET XML Serialization - a settings class.


var fC = new FontConverter(); // <-= Cool Trick, now watch...

// From a string is:
PrintFont = fC.ConvertFromString(yourString) as Font;

// To a string is:
string yourString = fC.ConvertToString(PrintFont);


// NOW if you wanted to bring back the Script for said Font as well.
// there are a few more steps. First, you will have to Save the value.
PrintSettings.Default.GdiCharSet = _font.Dialog.Font.GdiCharSet;// byte Type.

// or
byte gdiValue = PrintFont.GdiCharSet; // something like that.

// OR, you can store it as an int with:
int gdiValue = PrintFont.GdiCharSet.GetHashCode();

// But whatever, the Script is not brought back in with the Font whether
// you save it as a text file string or Settings.setting, either way.
// byte or int. And _fontDialog.Font.GdiCharSet HAS NO SETTER. So you are
// pretty much screwed...(sic) ...unless you do it my way. LoL jk...

// So you have your PrintFont loaded back in using the above. But you want
// to also bring back the Script for the saved Font. Having saved that GdiCharSet
// value as well, you simply put them together. My method makes it as simple as:

PrintFont = GetFontWithScript();

// After this ^ call, all you need to do is assign it with:
_fontDialog.Font = PrintFont; // and you will be completely set.

// And here is how it puts the two together again.

private Font GetFontWithScript()
{
PrintFont = PrintSettings.Default.PrintFont;
var name = PrintFont.Name;
var size = PrintFont.Size;
var style = PrintFont.Style;
var unit = PrintFont.Unit;
var charSet = PrintSettings.Default.GdiCharSet;

return new Font(name,size,style,unit,charSet);
/*
* Font(String, Single, FontStyle, GraphicsUnit, Byte)
* Initializes a new Font using a specified size, style, unit and character set.
*
*/
}

// Also, to convert a color to a string and back is just as simple.
// Where...
System.Drawing.Color _yourColor; // Assign it from FontDialog or whatever...
// Then...
string colorString = _yourColor.ToArgb().ToString();

// Then, to convert a string to a Color just...
_yourColor = Color.FromArgb(Convert.ToInt32(colorString));

// or just work with the int or byte and forget the string. ;-)


这篇关于将字符串转换为字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆