none
Drawing a rectangle RRS feed

  • Question

  •  Hi,
     
    I am writing an application in the doc-view arch with a CFormView. On the form there is a picture window. Currently this picture window does not have a class attached to it.

    Also, and this might be important, there are a few DialogBars (one on the top and one on the left side). The one on the left side has a PropertySheet with 2 PropertyPages in it. It is always shown and it pushes the formview (and the picture within) to the right.

    There are currently 2 threads in the app (besides the application thread) - one thread acquires the image and processes it (either from a camera or from a loaded file) and the other thread gets the processed image and displays it. The display currently is done through direct-draw7 using Blt as follows.

    hr = pDDSDest->Blt(&rcDest, pDDSSource, &rcSrc, DDBLT_WAIT | DDBLT_DDFX, &ddbltfx);

    The view has the mouse callbacks. So OnMouseMove and the buttondown/up callbacks are handled in the view, and the display thread can do zooms via the data in the view.

    So far so good (except that the redraws are only handled when the processing is finished which could take time and I would like to deal with this as well eventually).

    Now, I need to draw a rectangle on the image. For this, I thought that I would override OnDraw in the view.

    I thought I would do this as follows:

    void CRunnerView::OnDraw(CDC* pDC)

    {

    // TODO: Add your specialized code here and/or call the base class

    LOGBRUSH *lpLogBrush = new LOGBRUSH;

    lpLogBrush->lbStyle = BS_NULL;

    CBrush *pBrush = new CBrush();

    pBrush->CreateBrushIndirect(lpLogBrush);

    CPen *pPen = new CPen(PS_DASH, 1, RGB(255,255,255));

    CPen* pOldPen = pDC->SelectObject(pPen);

    CBrush* pOldBrush = pDC->SelectObject(pBrush);

    CRect rect(10,10,200,200);

     pDC->Rectangle(&rect);

    InvalidateRect(&rect,0); <<<---- SEE COMMENTS ABOUT THIS LINE

    pDC->SelectObject(pOldPen);

    pDC->SelectObject(pOldBrush);

    delete pPen;

    delete lpLogBrush;

    delete pBrush; 

    }

    If I don't put in the InvalidateRect, the rectangle is NOT SEEN, as if the image from the direct-draw draws over it. If I do put in the Invalidate rect, I see the rect, albeit shaking, and the original image also is over the part pushed over by the dialogbar- no refresh of the dialog bar.
    If I move the whole thing (refresh) the rectangle goes away again.

    I thought of drawing the rectangle in the code that calls the direct draw -- but it doesn't know about MFC things like CDC and stuff like that. I only get an HDC there and not a CDC.

    Is there a way to do this in the view? Should I do this in the place that calls direct draw?

    I would appreciate some help in this because I would like to get by this already and move on :)

    If more data is needed, I can provide it...

    Thanks,
    Jeff

    Thursday, December 11, 2008 2:34 PM

All replies

  • Hi,

    My two cents...

    When you are using DirectDraw, the API is communicating closely with the graphics card which outputs the surface to the screen. Where as the GDI commands are going through another layer (forgot for now), before eventually been shown by the graphics card. This is why when you move/refresh your window the rectangle dissappears. You can also test this by hitting alt-print screen or print-screen, the image captured will contain a black area where the DirectDraw surface is displayed (if you disable hardware acceleration and do print-screen you will get everything, but windows will be slow...).

    I think the solution for you is draw the rectangle on to the DirectDraw surface. This could be done relatively simply, by creating a 1x1 white image (that is BltBlt on to the surface at the appropriat size and color filter. Not sure on the exact api command but is certainly possible.

    As a side note, the mfc api is closely named to the windows sdk api, i.e. pWnd->ShowWindow( SW_SHOW), maps to a call ShowWindow( GetSafeHwnd(), SW_SHOW). This is similar for CDC to HDC (CDC is a MFC class wrapper around functions that take a HDC), i.e. dc.MoveTo( 10, 10) maps to MoveToEx( hdc, x, y, &point).

    Thanks,
    Mike.
    • Proposed as answer by Mike Appleby Monday, November 16, 2009 10:12 AM
    Sunday, November 15, 2009 4:10 PM