private void Form1_Load(object sender, EventArgs e)
{
// テストデータの設定
fpSpread1.ActiveSheet.DefaultStyle.Font = new System.Drawing.Font("メイリオ", 12);
fpSpread1.ActiveSheet.Cells[0, 0].Value = "値札印刷";
fpSpread1.ActiveSheet.Cells[0, 1].Value = "値札印刷";
fpSpread1.ActiveSheet.Columns[0].Width = 200;
fpSpread1.ActiveSheet.Columns[1].Width = 200;
// カスタムセルの設定
//fpSpread1.ActiveSheet.DefaultStyle.CellType = new SP1GeneralCellType();
fpSpread1.ActiveSheet.Columns[0].CellType = new SP5GeneralCellType();
fpSpread1.ActiveSheet.Columns[1].CellType = new CustomGeneralCellType();
}
// SP5(v7.0.2019.2008)と類似の描画
public class SP5GeneralCellType : FarPoint.Win.Spread.CellType.GeneralCellType
{
public override void PaintCell(Graphics g, Rectangle r, FarPoint.Win.Spread.Appearance appearance, object value, bool isSelected, bool isLocked, float zoomFactor)
{
System.Drawing.Drawing2D.GraphicsState sate = g.Save();
g.SetClip(r, System.Drawing.Drawing2D.CombineMode.Intersect);
int offsetY = CalculateAddingSpace(g, appearance.Font);
r.Offset(0, -offsetY);
r.Inflate(0, offsetY);
base.PaintCell(g, r, appearance, value, isSelected, isLocked, zoomFactor);
g.Restore(sate);
}
private int CalculateAddingSpace(Graphics g, Font f)
{
return Convert.ToInt32(Math.Ceiling(GetGDIPlusFontEmHeight(g, f) / 8));
}
static internal float GetGDIPlusFontEmHeight(Graphics g, Font font)
{
FontStyle fontStyle = font.Style;
FontFamily fontFamily = font.FontFamily;
return (fontFamily.GetEmHeight(fontStyle)) * font.GetHeight(g) / fontFamily.GetLineSpacing(fontStyle);
}
}
// 文字列を描画しきれない場合に文字列を独自描画
public class CustomGeneralCellType : FarPoint.Win.Spread.CellType.GeneralCellType
{
public override void PaintCell(Graphics g, Rectangle r, FarPoint.Win.Spread.Appearance appearance, object value, bool isSelected, bool isLocked, float zoomFactor)
{
// 既定の描画の実行
base.PaintCell(g, r, appearance, value, isSelected, isLocked, zoomFactor);
/// 文字列の高さの取得
int hight = (int)g.MeasureString(Convert.ToString(value), appearance.Font).Height;
// 文字列を描画しきれない場合
if (r.Height < hight)
{
// 文字列の独自の描画
using (SolidBrush bb = new SolidBrush(appearance.BackColor), fb = new SolidBrush(appearance.ForeColor))
{
g.FillRectangle(bb, r);
r.Offset(0, -1);
g.DrawString(Convert.ToString(value), appearance.Font, fb, r);
}
}
}
}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' テストデータの設定
FpSpread1.ActiveSheet.DefaultStyle.Font = New System.Drawing.Font("メイリオ", 12)
FpSpread1.ActiveSheet.Cells(0, 0).Value = "値札印刷"
FpSpread1.ActiveSheet.Cells(0, 1).Value = "値札印刷"
FpSpread1.ActiveSheet.Columns(0).Width = 200
FpSpread1.ActiveSheet.Columns(1).Width = 200
' カスタムセルの設定
'FpSpread1.ActiveSheet.DefaultStyle.CellType = new SP1GeneralCellType();
FpSpread1.ActiveSheet.Columns(0).CellType = New SP5GeneralCellType()
FpSpread1.ActiveSheet.Columns(1).CellType = New CustomGeneralCellType()
End Sub
' SP5(v7.0.2019.2008)と類似の描画
Public Class SP5GeneralCellType
Inherits FarPoint.Win.Spread.CellType.GeneralCellType
Public Overrides Sub PaintCell(g As Graphics, r As Rectangle, appearance As FarPoint.Win.Spread.Appearance, value As Object, isSelected As Boolean, isLocked As Boolean, zoomFactor As Single)
Dim sate As System.Drawing.Drawing2D.GraphicsState = g.Save()
g.SetClip(r, System.Drawing.Drawing2D.CombineMode.Intersect)
Dim offsetY As Integer = CalculateAddingSpace(g, appearance.Font)
r.Offset(0, -offsetY)
r.Inflate(0, offsetY)
MyBase.PaintCell(g, r, appearance, value, isSelected, isLocked, zoomFactor)
g.Restore(sate)
End Sub
Private Function CalculateAddingSpace(g As Graphics, f As Font) As Integer
Return CInt(Math.Ceiling(GetGDIPlusFontEmHeight(g, f) / 8))
End Function
Friend Shared Function GetGDIPlusFontEmHeight(g As Graphics, font As Font) As Single
Dim fontStyle As FontStyle = font.Style
Dim fontFamily As FontFamily = font.FontFamily
Return (fontFamily.GetEmHeight(fontStyle)) * font.GetHeight(g) / fontFamily.GetLineSpacing(fontStyle)
End Function
End Class
' 文字列を描画しきれない場合に文字列を独自描画
Public Class CustomGeneralCellType
Inherits FarPoint.Win.Spread.CellType.GeneralCellType
Public Overrides Sub PaintCell(ByVal g As System.Drawing.Graphics, ByVal r As System.Drawing.Rectangle, ByVal appearance As FarPoint.Win.Spread.Appearance, ByVal value As Object, ByVal isSelected As Boolean, ByVal isLocked As Boolean, ByVal zoomFactor As Single)
'既定の描画の実行
MyBase.PaintCell(g, r, appearance, value, isSelected, isLocked, zoomFactor)
' 文字列の高さの取得
Dim hight As Integer = g.MeasureString(CStr(value), appearance.Font).Height
' 文字列を描画しきれない場合
If r.Height < hight Then
' 文字列の独自の描画
Using bb = New SolidBrush(appearance.BackColor), fb As New SolidBrush(appearance.ForeColor)
g.FillRectangle(bb, r)
r.Offset(0, -1)
g.DrawString(CStr(value), appearance.Font, fb, r)
End Using
End If
End Sub
End Class