Am looking for a little bit of help to convert the following Visual C++ code into a delphi if its possible

#define MARGINX 24      // this much for "mon"
#define MARGINY 24      // this much for "00"

char *dayNames[7] = {
    "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"
};

#define TEXT_COLOR  RGB(128,128,128)
#define TEXT_HILITE RGB(255,255,255)

void CDlgSchedule::OnPaint() 
{
    CPaintDC dc(this); // device context for painting
    CDC* pDC = &dc;

    CRect r;
    CFont myFont;
    CFont* pOldFont;
    CPen myPen(PS_DOT, 1, RGB(255,255,0));
    CPen* pOldPen;
    int x, y, w, h;
    int left, top;
    int day, hour;
    int i;
    int f;
    char msg[80];
    CSize sz;

    if ( IsWindow(m_ctrl_frame.m_hWnd) )
    {
        m_ctrl_frame.GetWindowRect(&r);
        this->ScreenToClient(&r);
        r.DeflateRect(2,2);             // we are an indented thingy

        // set up some resources
        myFont.CreatePointFont(100, "Arial", pDC);
        pOldFont = pDC->SelectObject(&myFont);
        pOldPen = pDC->SelectObject(&myPen);
        pDC->SetBkMode(TRANSPARENT);
        pDC->SetTextColor(TEXT_COLOR);

        // erase the background
        pDC->FillSolidRect(&r, RGB(0,0,0)); 

        r.DeflateRect(MARGINX, MARGINY);                // this much clearance all around
        m_scheduleRect = r;                             // important for hit testing
        left = r.left;
        top = r.top;
        w = r.Width();
        h = r.Height();

        // annotate the cols and rows
        for ( i = 0; i<24; i++ )
        {
            if ( i == m_hour )
            {
                pDC->SetTextColor(TEXT_HILITE);
            }
            else
            {
                pDC->SetTextColor(TEXT_COLOR);
            }
            sprintf(msg, "%02d", i);
            sz = pDC->GetTextExtent(msg, strlen(msg));
            x = (i * w)/24;
            f = ((i+1) * w)/24 - x;
            f = (f - sz.cx)/2;
            if ( f < 0 ) f = 0;
            pDC->TextOut(left+x+f, top-sz.cy, msg, strlen(msg));
        }
        for ( i = 0; i < 7; i++)
        {
            if ( i == m_day )
            {
                pDC->SetTextColor(TEXT_HILITE);
            }
            else
            {
                pDC->SetTextColor(TEXT_COLOR);
            }
            sprintf(msg, "%s", dayNames[i]);
            sz = pDC->GetTextExtent(msg, strlen(msg));
            y = (i * h)/7;
            f = ((i+1) * h)/7 - y;
            f = (f - sz.cy)/2;
            if ( f < 0 ) f = 0;
            pDC->TextOut(left-(sz.cx+2), top+y+f, msg, strlen(msg));
        }

        // now draw some lines
        for ( i = 0; i<=24; i++ )
        {
            x = (i * w)/24;
            pDC->MoveTo(left+x, top+0);
            pDC->LineTo(left+x, top+h);
        }
        for ( i = 0; i <= 7; i++)
        {
            y = (i * h)/7;
            pDC->MoveTo(left+0, top+y);
            pDC->LineTo(left+w, top+y);
        }

        // Fill in the boxes
        CRect hourRect;
        for (day = 0; day < 7; day++)
        {
            for ( hour = 0; hour < 24; hour++ )
            {
                if ( IsHourSet(&m_week, day, hour) )
                {
                    this->GetHourRect(&hourRect, day, hour);
                    hourRect.DeflateRect(2,2);
                    pDC->FillSolidRect(&hourRect, RGB(0,128,0));
                }
                else
                {   // we just erased everything to black, so no work here
                }
            }
        }

        // restore the DC
        pDC->SelectObject(pOldFont);
        pDC->SelectObject(pOldPen);
    }

    // Do not call CDialog::OnPaint() for painting messages
}

Thanks

You could make a literal translation of that code into Delphi, but it would not be a sensible thing to do. Rather than doing a line-by-line conversion I suggest you consider what the code is doing and then think of the best way of doing that in Delphi.

The simplest approach would be to use an OwnerDraw TStringGrid and let Delphi handle most of this for you. No need to calculate the column widths & row heights, no need to draw the lines, etc etc. All you would really need to do is have the grid's OnDrawCell event handler implement this bit:

                if ( IsHourSet(&m_week, day, hour) )
                {
                    this->GetHourRect(&hourRect, day, hour);
                    hourRect.DeflateRect(2,2);
                    pDC->FillSolidRect(&hourRect, RGB(0,128,0));
                }
                else
                {   // we just erased everything to black, so no work here
                }

All that does is paint the cell a different color (Green?) if IsHourSet returns TRUE.

this is what its doing basicly am trying to recreate it.

I put a image on post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.