Spread 8.0 Documentation
Example
Support Options

Glossary Item Box

Example


The following example sets the two cells to be of type Owner Drawn. The TypeOwnerDrawStyle property is set differently for the two cells. In the DrawItem event the cell is drawn with an ellipse if the TypeOwnerDrawStyle property is set to 0 and a rectangle if it is set to 1.

C++

BOOL CGeneral::OnInitDialog()
{
    CDialog::OnInitDialog();
    m_Spread.SetRow(1);
    m_Spread.SetCol(1);
    m_Spread.SetCellType(CellTypeOwnerDrawn);
    m_Spread.SetOwnerDrawStyle(0);
    m_Spread.SetRow(1);
    m_Spread.SetCol(2);
    m_Spread.SetCellType(CellTypeOwnerDrawn);
    m_Spread.SetOwnerDrawStyle(1);
}

void CGeneral::OnDrawItem(long Col, long Row, long hDC, long Left, long Top, long Right, long Bottom, long Style)
{
    int iTwipsPerPixelX = 1440 / GetDeviceCaps((HDC)hDC,LOGPIXELSX);
    int iTwipsPerPixelY = 1440 / GetDeviceCaps((HDC)hDC,LOGPIXELSY);
    int xPixels = (int)Left / iTwipsPerPixelX;
    int yPixels = (int)Top / iTwipsPerPixelY;
    int x2Pixels = (int)Right / iTwipsPerPixelX;
    int y2Pixels = (int)Bottom / iTwipsPerPixelY;
    HPEN hPen;
    HPEN hOldPen;
    hPen = ::CreatePen(PS_SOLID, 1, RGB(255,0,0));
    hOldPen = (HPEN)::SelectObject((HDC)hDC, hPen);
    switch( Style )
    {
        case 0:
        ::Ellipse((HDC)hDC, xPixels, yPixels, x2Pixels,y2Pixels);
        break;
        case 1:
        ::Rectangle((HDC)hDC, xPixels, yPixels, x2Pixels,y2Pixels);
        break;
    }
    ::SelectObject((HDC)hDC, hOldPen);
    ::DeleteObject(hPen);
    if (Style == 0)
        ::TextOut((HDC)hDC, (xPixels + x2Pixels)/3, yPixels,_T("Ellipse"), 7);
    else if (Style == 1)
        ::TextOut((HDC)hDC, (xPixels + x2Pixels)/3, yPixels,_T("Rectangle"), 9);
}

Visual Basic

Sub Form_Load()
    fpSpread1.Row = 1
    fpSpread1.Col = 1
    fpSpread1.CellType = CellTypeOwnerDrawn
    fpSpread1.Text = "Ellipse"
    fpSpread1.TypeOwnerDrawStyle = 0
    fpSpread1.Row = 1
    fpSpread1.Col = 2
    fpSpread1.CellType = CellTypeOwnerDrawn
    fpSpread1.Text = "Rectangle"
    fpSpread1.TypeOwnerDrawStyle = 1
End Sub

Sub fpSpread1_DrawItem(ByVal Col As Long, ByVal Row As Long, ByVal hDC As Long, ByVal left As Long, ByVal top As Long, ByVal right As Long, ByVal bottom As Long,ByVal style As Long)
    Dim s As Long
    Dim hBrush As Long
    Dim Rec As RECT
    Dim xMargin As Long
    Dim yMargin As Long
    ' Reduce the cell rectangle by 10 percent on each side
    Rec.left = left / Screen.TwipsPerPixelX
    Rec.right = right / Screen.TwipsPerPixelX
    Rec.top = top / Screen.TwipsPerPixelY
    Rec.bottom = bottom / Screen.TwipsPerPixelY

    ' Reduce the rectangle by 20 percent
    xMargin = (Rec.right - Rec.left) / 10
    yMargin = (Rec.bottom - Rec.top) / 10
    Rec.left = Rec.left + xMargin
    Rec.right = Rec.right - xMargin
    Rec.top = Rec.top + yMargin
    Rec.bottom = Rec.bottom - yMargin

    ' Use the style parameter (TypeOwnerDrawStyle property)
    ' of cell to determine the figure to paint in the cell
    If style = 0 Then
        s = Ellipse(hDC, Rec.left, Rec.top, Rec.right, Rec.bottom)
    ElseIf style = 1 Then
        s = Rectangle(hDC, Rec.left, Rec.top, Rec.right, Rec.bottom)
    End If

    ' Also use the Text property of cell
    fpSpread1.Col = Col
    fpSpread1.Row = Row
    s = DrawText(hDC, fpSpread1.Text, Len(fpSpread1.Text), Rec, DT_CENTER + DT_VCENTER + DT_SINGLELINE)
End Sub

Copyright © GrapeCity, inc. All rights reserved.