在c#中以编程方式创建字体样式 [英] Creation of font styles programmatically in c#

查看:95
本文介绍了在c#中以编程方式创建字体样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我是4周前开始编程的新手。

我是创建一个类似于Excel的电子表格软件。



我的问题如下。



当人物是谁将使用我的软件单击一个单元格

,然后通过按一些图标更改字体的样式,此样式将保存在内存中的表格中,并带有单元格的名称。



打开保存的电子表格或重新显示包含单元格的网格时(例如在水平或垂直滚动​​期间),我将获取每个信息的信息我的表中的单元格在内存中,然后传递给存根,显示每个单元格在图片框上。



我的问题是我不知道如何更改在绘制图片框之前我的字体样式我的单元格中包含的信息(文本,日期,值...)。



对于pa关于存储在内存表中的fontstyle的rt,因为它存储为字符串,我可以轻松地从该字符串中提取每个样式,这将给我一个样式列表。



现在有了样式列表,我需要改变字体的样式,它将显示包含在单元格中的信息。



我可以做一个



if(cellPoliceStyle =Bold)

{

Font fontToUse = new Font(cellColumnSize,cellRowSize,style粗体

//我没有在这个例子中写出正确的风格,但这不是问题,只是为了表明我想做什么

抽绳(textToDisplay,locationX,locationY ....)

}



真正的问题是我我不想写一个if用Italic,另一个用Bold,另一个用Underline,另一个用Bold + Underline ......



我不想写每一个组合风格。



我真的在这个问题上苦苦挣扎,如果有人能在c#
中提出想法和代码示例,我将不胜感激。


非常感谢你的帮助

Hi everyone.

I''m brand new to programming as I have started 4 weeks ago.
I''m creating a spreadsheet software similar to Excel.

My problem is the following.

When the personne who will be using my software is clicking on a cell
and then changing the style of the font by pressing some icones, this style is saved in a table in memory with the name of the cell.

On opening of a saved spreadsheet or during the redisplay of the grid which contains the cells (for example during a horizontal or vertical scroll), I''m getting the informations for each cells from my table in memory and then pass them to a stub which display each cell on a picture box.

My probleme is that I do not know how to change the style of my font before drawing on the picture box the information (texte, date, value...) contained in my cell.

For the part concerning the fontstyle stored in the memory table, as it is stored as a string, I can easily extract each style from that string which would give me a list of style.

Now with that list of styles, i need to change the style of the font which will display the information contained into the cell.

I could do a

if (cellPoliceStyle = "Bold")
{
Font fontToUse = new Font(cellColumnSize, cellRowSize, style bold
//I didn''t write the proper way of putting a style in this example, but it''s not a problem, it''s just to show what I want to do
drawstring (textToDisplay, locationX, locationY ....)
}

The real problem is that I don''t want to have to write a if with Italic, another one with Bold, another one with Underline, another one with Bold + Underline...

I don''t want to write every single combination of styles.

I''m really struggling on that problem and would appreciate if somebody could come up with an idea and a code example in c#

Thankyou very much for your help

推荐答案

FontStyle 是标有 FlagsAttribute



这意味着你可以'或''在一起。





FontStyle is marked with FlagsAttribute.

This means you can ''or'' them together.


private FontStyle GetCellStyle(object yourOptions)
{
    FontStyle style = FontStyle.Regular;
    style = yourOptions.IsBold ? style | FontStyle.Bold : style | FontStyle.Regular;
    style = yourOptions.IsItalic ? style | FontStyle.Italic : style| FontStyle.Regular;

    return style;
}





如果 IsBold IsItalic 是真的,你会在这里得到一个粗体+斜体 FontStyle



Regular = 0,Bold = 1,Italic = 2等。



干杯。



If IsBold and IsItalic are true, you''ll get a bold+italic FontStyle here.

Regular = 0, Bold = 1, Italic = 2, etc.

Cheers.


我建​​了一个班级给假设bolBold,bolUnderline等始终可用,可以轻松使用。



I built a class to enable easy ussage assuming that the bolBold, bolUnderline ... etc were always available.

public static Font SetFontStyle(Font sourcefont, bool bolBold, bool bolUnderline, bool bolItalic, bool bolStrikeout)
            {
            Font ffont = new Font(sourcefont.Name, sourcefont.Size, FontStyle.Regular);

            if (bolBold)
                {
                ffont = new Font(sourcefont.Name, sourcefont.Size, ffont.Style | FontStyle.Bold);
                }
            if (bolUnderline)
                {
                ffont = new Font(sourcefont.Name, sourcefont.Size, ffont.Style | FontStyle.Underline);
                }
            if (bolItalic)
                {
                ffont = new Font(sourcefont.Name, sourcefont.Size, ffont.Style | FontStyle.Italic);
                }
            if (bolStrikeout)
                {
                ffont = new Font(sourcefont.Name, sourcefont.Size, ffont.Style | FontStyle.Strikeout);
                }
            return ffont;
            }


这篇关于在c#中以编程方式创建字体样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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