[英]Convert string to font & Color
有人可以幫我一個正則表達式(或其他東西),我真的很難完成這項工作,找不到任何可以幫助我完成它的東西。
我有一個程序,用戶在表單上放置一些控件。 當他們點擊保存按鈕時,它會瀏覽表單上的所有控件並將其詳細信息保存到文本文件中(我知道該怎么做)..就像這樣:
Label
"This is text on a label"
20, 97
Tahoma, 7.5, Regular
-778225617
說明:
Control Type
Text property of that control
Location of the control
Font properties for that control
ForeColor for that control.
...用戶保存時創建的此文本文件可能只包含單個控件的信息,如上所示,甚至包含多個控件,如下所示:
Label
"This is text on a label"
20, 97
Tahoma, 7.5, Regular
-778225617
LinkLabel
"This text belongs to the linklabel text property."
Arial, 20, Bold
-119045893
說明:
Control
Text Property
Location
Font Properties
ForeColor
Control
Text Property
Location
Font Properties
ForeColor
......等......我發現這對我來說很難,因為到目前為止我還不是專家。 有人能幫幫我嗎? 我還需要將字體屬性行從字符串轉換為Font對象,以便在運行時將其分配給指定控件的Font屬性。
我非常感謝任何幫助。 非常感謝。
謝謝傑伊
你會做這樣的事情:
using System;
using System.Drawing;
class Example
{
static void Main()
{
String fontName = "Tahoma, Regular, Size";
String[] fontNameFields = fontName.Split(',');
Font font = new Font(fontNameFields[0],
Single.Parse(fontNameFields[2]),
(FontStyle)Enum.Parse(typeof(FontStyle), fontNameFields[1]));
}
}
您可以從文件中讀取文本並將字符串拆分為數組,然后使用字體類的重載構造函數來創建新的字體對象。
有關字體構造函數的列表,請參閱
大小參數是EM-大小,以點 ,新的字體。 因此,對於其他單位的字體大小,您必須要處理它。
這似乎是一個說得不好的問題......我看到它有幾個漏洞。 (我假設你在談論WinForms)我稍后會解決這些問題。
我不知道.NET中的任何功能將為您完成所有這些組合解析。 但是,您可以使用CSSName屬性[它是一個接近此屬性的屬性]使用WinForms進行格式調整,並在GUI上使用CSS文件。 [有點奇怪,但它的工作原理]
順便說一句,負整數是一個有符號整數,表示顏色集:RGB:
255 255 255
問題:
為什么你沒有使用XmlSerialization。 你只需要創建一個內存流,然后調用它的點Save方法; 然后你可以隨時重新加載數據。
例如,你有一個名為Canvas的類。
繼續像Canvas.AddControls(controlTypes.Label,“這是標簽上的文字”,20,97,Tahoma,7.5,Regular,-778225617);
請參閱最簡單的XmlSerializer示例。
如果您不希望您的文件是xml類型? 使用二進制序列化。 看到這個 。
就像是:
public static void Save(object obj)
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
// Serialize an object into the storage referenced by 'stream' object.
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// Serialize multiple objects into the stream
formatter.Serialize(stream, obj);
// If you want to put the stream into Array of byte use below code
// byte[] buffer = stream.ToArray();
}
}
10分鍾解決方案:
好吧,以下是我真正意味着的5分鍾“快速努力”; 我希望這能解決問題。
見下文。
第1步:畫布類
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
namespace SaveControls
{
[Serializable()]
public class CCanvas
{
List<CDrawing> _listControls;
public List<CDrawing> Controls
{
get { return _listControls; }
}
public CCanvas()
{
_listControls = new List<CDrawing>();
}
public void AddControls(CDrawing theControls)
{
_listControls.Add(theControls);
}
public void ReloadControl(Form frm)
{
//foreach (CDrawing draw in _listControls) -- requires IEnumerable implementation.
for (int i = 0; i < _listControls.Count; i++)
{
CDrawing d = (CDrawing)_listControls[i];
d.Draw(frm);
}
}
public void Save()
{
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, this);
}
}
catch (IOException)
{
}
}
public CCanvas Open()
{
CCanvas LoadedObj = null;
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
LoadedObj = (CCanvas)bin.Deserialize(stream);
}
return LoadedObj;
}
}
}
第2步:添加圖紙
using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Drawing;
namespace SaveControls
{
[Serializable()]
public class CDrawing
{
public enum ControlTypes { Label, TextBox, None };
private ControlTypes _controlType;
public ControlTypes ControlType
{ get { return _controlType; } }
private string _strControlText;
public string Text
{ get { return _strControlText; } }
private int _xPosition;
public int X
{ get { return _xPosition; } }
private int _yPosition;
public int Y
{ get { return _yPosition; } }
private string _strFontName;
public string Font
{ get { return _strFontName; } }
double _fFontSize;
public double Size
{ get { return _fFontSize; } }
string _strStyle;
public string Style
{ get { return _strStyle; } }
decimal _dForegroundColor;
public decimal Color
{ get { return _dForegroundColor; } }
public CDrawing(ControlTypes controlType, string strControlText, int xPosition, int yPosition,
string strFontName, double fFontSize, string strStyle, decimal dForegroundColor)
{
_controlType = controlType;
_strControlText = strControlText;
_xPosition = xPosition;
_yPosition = yPosition;
_strFontName = strFontName;
_fFontSize = fFontSize;
_strStyle = strStyle;
_dForegroundColor = dForegroundColor;
}
public void Draw(Form frm)
{
if (_controlType == ControlTypes.Label)
{
Label lbl = new Label();
lbl.Text = _strControlText;
lbl.Location = new Point(_xPosition, _yPosition);
System.Drawing.FontStyle fs = (_strStyle == System.Drawing.FontStyle.Regular.ToString()) ? System.Drawing.FontStyle.Regular : System.Drawing.FontStyle.Bold;
lbl.Font = new System.Drawing.Font(_strFontName, (float)_fFontSize, fs);
lbl.ForeColor = SystemColors.Control;// _dForegroundColor;
lbl.Visible = true;
frm.Controls.Add(lbl);
}
}
}
}
第3步:使用,序列化,保存,重新加載
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Save()
{
//Create a canvas object
CCanvas Canvas1 = new CCanvas();
//Add controls
Canvas1.AddControls(new CDrawing(CDrawing.ControlTypes.Label, "This is text on a label1", 10, 100, "Tahoma", 7.5, "Regular", -778225617));
Canvas1.AddControls(new CDrawing(CDrawing.ControlTypes.Label, "This is text on a label11", 20, 200, "Verdana", 7.5, "Regular", -778225617));
Canvas1.AddControls(new CDrawing(CDrawing.ControlTypes.Label, "This is text on a label111", 30, 300, "Times New Roman", 7.5, "Regular", -778225617));
//Save the object
Canvas1.Save();
}
private void button1_Click(object sender, EventArgs e)
{
Save();
}
private void btnLoad_Click(object sender, EventArgs e)
{
Load();
}
public void Load()
{
//Retrieve
CCanvas Canvas2 = new CCanvas();
//opens the binary file
Canvas2 = Canvas2.Open();
//loads control to this form.
Canvas2.ReloadControl(this);
}
}
如果您打算討厭這個解決方案,請告訴我們原因。 同時我正在尋找上傳的地方。 Googlecode,但我沒有安裝subversion客戶端。 :0(
這對你有用嗎?
Font font = new Font("Tahoma",12,FontStyle.Regular);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.