Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串转换为字体&;颜色_C#_Regex_Winforms_Fonts - Fatal编程技术网

C# 将字符串转换为字体&;颜色

C# 将字符串转换为字体&;颜色,c#,regex,winforms,fonts,C#,Regex,Winforms,Fonts,有人能帮我弄一个正则表达式(或者别的什么东西)吗?我真的很难完成这件事,但在任何地方都找不到任何能帮我完成它的东西 我有一个程序,用户在窗体上放置一些控件。当他们单击“保存”按钮时,它会遍历表单上的所有控件,并将其详细信息保存到文本文件中(我知道如何操作)…如下所示: Label "This is text on a label" 20, 97 Tahoma, 7.5, Regular -778225617 说明: Control Type Text property of that cont

有人能帮我弄一个正则表达式(或者别的什么东西)吗?我真的很难完成这件事,但在任何地方都找不到任何能帮我完成它的东西

我有一个程序,用户在窗体上放置一些控件。当他们单击“保存”按钮时,它会遍历表单上的所有控件,并将其详细信息保存到文本文件中(我知道如何操作)…如下所示:

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.
Control
Text Property
Location
Font Properties
ForeColor
Control
Text Property
Location
Font Properties
ForeColor
。。。 用户保存时创建的此文本文件可能仅包含单个控件的信息,如上图所示,甚至包含多个控件的信息,如下图所示:

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 Type
Text property of that control
Location of the control
Font properties for that control
ForeColor for that control.
Control
Text Property
Location
Font Properties
ForeColor
Control
Text Property
Location
Font Properties
ForeColor
……等等。。。我发现这对我来说很难,因为到目前为止我还不是专家。谁能帮帮我吗?我还需要将字体属性行从字符串转换为字体对象,以便在运行时将其分配给指定控件的字体属性

我真的非常感谢任何帮助。非常感谢你

谢谢
jay这对你有用吗

Font font = new Font("Tahoma",12,FontStyle.Regular);

你应该这样做:

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]));
    }
}

您可以从文件中读取文本并将字符串拆分为数组,然后使用字体类的重载构造函数创建新的字体对象

有关字体构造函数的列表,请参见


size参数是新字体的em大小,以点为单位。因此,对于其他单位的字体大小,您必须注意这一点。

这似乎是一个陈述不当的问题。。。我看到上面有几个洞。(我假设您谈论的是WinForms)稍后我将讨论这些问题

我不知道.NET中有任何功能可以为您完成所有这些组合解析。但是,您可以使用CSSName属性[这是一个与此相近的属性]对WinForms进行格式调整,并在GUI上使用CSS文件。[有点奇怪,但它起作用了]

顺便说一句,负整数是一个有符号整数,表示以下颜色集: RGB:

255 255 255

问题:

  • 字体和格式的数据规范似乎表明没有控件可以嵌入另一个控件,这通常是通过WinForms的按钮、标签和面板来实现的。(XML是嵌入和避免此问题的一个很好的建议)
  • 这不是标准格式。为什么不使用RTF呢。有了RTF,它看起来很简单,你就有了一个观看者
  • 属性定义和值分离。看起来您使用的是属性表格式,但这并不意味着属性符合您的建议,解析时容易出错

  • 为什么不使用XmlSerialization呢。你需要做的就是创建一个内存流,并调用它的点保存方法;然后您可以随时重新加载数据

    例如,您有一个名为Canvas的类

    继续类似Canvas.AddControls(controlTypes.Label,“这是标签上的文本”, 20,97,塔荷马,7.5,普通,-778225617)

    请看样品

    如果不希望文件是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:获取画布-画布对象
    • 步骤2:向其中添加图形/控件
    • 步骤3:序列化、保存和重新加载对象
    见下文

    步骤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;
        }
    }
    
    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);
    
    
        }
    
    }
    

    如果您计划讨厌此解决方案,请告诉我们原因。与此同时,我正试图寻找一个地方上传。谷歌代码,但我没有安装subversion客户端:0(

    what environment?winforms/webforms?描述您希望在其中执行此类转换的场景?这如何帮助他们解析“Tahoma,Regular,Size”从文本文件?正如你所见,我在10月5日回答了这个问题,也就是在你编辑问题并添加更多细节的3天前。你的原始问题没有任何关于文件格式的细节。是的,原始问题确实有足够的细节。这当然是假设“大小”实际上是一个浮点数:)您好,andrew,谢谢您的回答:)在使用上述示例时,我收到一个错误,指出索引超出了数组的边界。它具体指的是什么?这意味着您没有解析包含两个逗号的字符串。调试应用程序并检查包含字体信息的字符串。我以前从未使用过xmlserializer,我不知道用户将添加到画布中保存哪些控件,也不知道他们将为这些控件指定什么颜色、大小、字体名和/或文本。因此,我不能手动添加此信息。请查看我的以下评论。如果您希望使用windows窗体解决方案文件,请告诉我。哦,是的,如果您可以将解决方案文件发送给我,那将非常好:请参阅我的最新帖子!找不到快速上传的位置。有没有办法上传代码文件在这里非常感谢你,凯曼!!!那代码工作得很好!非常感谢D:D:D:D:DGlad,我能帮忙。快乐编码(0: