/
RepositoryItemCustomFontEdit.cs
125 lines (117 loc) · 4.91 KB
/
RepositoryItemCustomFontEdit.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
namespace CustomFontEditControl
{
internal class RepositoryItemCustomFontEdit : RepositoryItemFontEdit
{
// static constructor which calls static registration method
static RepositoryItemCustomFontEdit() { RegisterCustomFontEdit(); }
// static register method
public static void RegisterCustomFontEdit()
{
EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(
CustomFontEditorName,
typeof(CustomFontEdit),
typeof(RepositoryItemCustomFontEdit),
typeof(ComboBoxViewInfo),
new ButtonEditPainter(),
true,
null));
}
internal static void DrawFontName(Graphics gr, string name, Font font, Font normalFont, Brush brush, Rectangle bounds, bool showPreview)
{
using (StringFormat sf = new StringFormat())
{
sf.FormatFlags |= StringFormatFlags.NoWrap;
if (font.Height < bounds.Height || font.Height / bounds.Height > 2)
sf.LineAlignment = StringAlignment.Center;
if (!DevExpress.Utils.ControlUtils.IsSymbolFont(font))
{
try
{
gr.DrawString(name, font, brush, bounds, sf);
}
catch
{
gr.DrawString(name, normalFont, brush, bounds, sf);
}
}
else
{
gr.DrawString(name, normalFont, brush, bounds);
if (showPreview)
{
SizeF size = gr.MeasureString(name + "w", normalFont);
RectangleF newBounds = bounds;
newBounds.Offset(size.Width, 0);
gr.DrawString(name, font, brush, newBounds, sf);
}
}
}
}
internal static void DrawFontBoxItem(ListBoxDrawItemEventArgs e, RepositoryItemCustomFontEdit item, bool IsDrawStyle, string sCurrentFontName)
{
if (sCurrentFontName == string.Empty) return;
using (SolidBrush foreBrush = new SolidBrush(e.Appearance.ForeColor), backBrush = new SolidBrush(e.Appearance.BackColor))
{
if (!FontItemPaintHelper.DrawItemBar(e, item as RepositoryItemComboBox))
e.Graphics.FillRectangle(backBrush, e.Bounds);
using (FontFamily family = new FontFamily(sCurrentFontName))
{
FontStyle currentStyle = FontStyle.Regular;
if (IsDrawStyle)
{
switch (e.Item.ToString())
{
case "Regular":
currentStyle = FontStyle.Regular;
break;
case "Italic":
currentStyle = FontStyle.Italic;
break;
case "Bold":
currentStyle = FontStyle.Bold;
break;
case "Bold Italic":
currentStyle = FontStyle.Bold | FontStyle.Italic;
break;
default:
break;
}
}
using (Font font = new Font(family, e.Appearance.Font.Size, currentStyle))
{
RepositoryItemCustomFontEdit.DrawFontName(e.Graphics, e.Item.ToString(), font, e.Appearance.Font, foreBrush, e.Bounds, true);
e.Handled = true;
}
}
}
}
// internal editor name
internal const string CustomFontEditorName = "CustomFontEdit";
// public constructor
public RepositoryItemCustomFontEdit() : base() { }
// ovverride property
public override string EditorTypeName
{
get { return CustomFontEditorName; }
}
public override string GetDisplayText(DevExpress.Utils.FormatInfo format, object editValue)
{
if (editValue != null && (editValue as Font) != null)
return String.Format("{0}, {1}", (editValue as Font).Name, (editValue as Font).Size);
else
return base.GetDisplayText(format, editValue);
}
}
}