PowerTools SPREAD for Windows Forms 8.0J 移行ガイド
テキストの描画が異なる

ステータス

バージョン ID 機能分類 対応策
7.0.2019.2008 70191 外観

詳細

旧バージョンでは、描画エリアが適切に確保されておらず、フォントの設定や文字によってはテキストの一部が欠ける場合がありました。これはテキストが全部表示されるように十分な行の高さを設定しても改善されません。そのため、バージョン7.0.2019.2008からは以下のように変更されています。

7.0.2018.2008 7.0.2019.2008

上記のような問題の対応のためバージョン7.0.2019.2008からは、以下のように描画エリアが変更されています。

7.0.2018.2008 7.0.2019.2008

描画エリアの変更にともない、バージョン7.0.2019.2008からはテキストの垂直方向の描画位置が下にずれます。以下はバージョン7.0.2018.2008とバージョン7.0.2019.2008の実行例です。

7.0.2018.2008 7.0.2019.2008

※この変更点はバージョン8.0.3502.2008(SP1)から8.0.3503.2008(SP2)でも変更されています。

なお、この動作を旧バージョンと同じにする直接的な方法は用意されていませんが、カスタムセル型を作成して独自にテキストを描画することで同様の外観を実現することができます。

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);
        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)
        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
関連トピック

 

 


© 2004-2015, GrapeCity inc. All rights reserved.