Javascript required
Skip to content Skip to sidebar Skip to footer

Draw Pixels on a Dialog Box

Thread: Draw an image continously using pixel value in vc++

  1. #one

    Depict an image continously using pixel value in vc++

    Hullo,

    I drawn an image(384W 10 384 H) by pixel value using setpixel method in Onpaint().

    It drawn an image but i want to depict continuously. so i used InvalidateRect(0);

    Subsequently using this method, the image was drawn only my other controls (Button, Tab) are hidden in the window.

    1. is Setpixel method correct to draw an image continuously using Onpaint() and InvalidateRect(0) method ?

    2. With out using InvalidateRect(0) method, how to draw an image continuously by pixel value ?

    three. Which method is all-time to draw an image using pixel value ?

    Regards,

    SaraswathiSrinath.


  2. #2

    Re: Draw an image continously using pixel value in vc++

    Where do you call InvalidateRect from? Hope, non from the OnPaint handler?

    Victor Nijegorodov


  3. #3

    Re: Draw an paradigm continously using pixel value in vc++

    ...and I hope you lot don't call InvalidateRect(0) for each pixel.

  4. #four

    Re: Depict an image continously using pixel value in vc++

    sorry. I'm fresher in MFC. I used Invalidate(0); method in Onpaint method itself.

    i used two method. 1. Setpixel & 2. BitBlt method.

    Setpixel lawmaking as follows & Bitblt method lawmaking was uploaded. Kindly notice & give a solution for these kind of problems.

    void MainDlg::OnPaint()
    {

    CPaintDC pdc(this);

    int a = 1, b = i, c = 1;
    for(int X=0;Ten<384;X++)
    {
    for(int Y=0;Y<384;Y=Y+iv)
    {
    SetPixelV(pdc,400+10,45+Y,RGB(a,b,c));
    a = a+b; b = b+c; c = c+a;
    }
    }

    // UpdateWindow();

    Invalidate(0);

    }

    Regards,

    Saraswathisrinath.


  5. #five

    Re: Draw an image continously using pixel value in vc++

    The right identify to draw a background prototype is in the OnEraseBkgnd handler. In that location is no need to call Invalidate().

    Your code has 2 things (at least) wrong: painting inside OnPaint for a dialog (which is well-nigh ever wrong), and calling Invalidate inside OnPaint (which will crusade an infinite loop).

    Mike


  6. #6

    Re: Draw an image continously using pixel value in vc++

    Give thanks you and so much.

    can u requite any simple example lawmaking using OnEraseBkgnd handler.

    But i like to describe an image continuously using pixels values, Its not a back ground image.

    it will exist draw continuously minimum xxx times per minute.

    is possible to draw these type of paradigm using OnEraseBkgnd handler method?

    Pls articulate me.

    Regards

    Saraswathisrinath.

    Last edited by saraswathisrinath; November 30th, 2011 at 12:28 AM.

  7. #7

    Re: Depict an image continously using pixel value in vc++

    "thirty times per minute" means every 2 seconds.
    Just create a timer with 2 seconds interval and call Invalidate from within the timer handler...

    Victor Nijegorodov


  8. #eight

    Re: Draw an image continously using pixel value in vc++

    Hi,

    Could you please give us more information abot what you're trying to achieve (not how)?

    Quote Originally Posted by saraswathisrinath View Post

    Howdy,

    I drawn an image(384W x 384 H) by pixel value using setpixel method in Onpaint().

    No, in the lawmaking you've posted you draw some lines of pixels. You lot neither have a bitmap/epitome in that location nor are y'all drawing one.

    Quote Originally Posted by saraswathisrinath View Post

    Information technology fatigued an prototype simply i want to depict continuously. so i used InvalidateRect(0);

    I don't know what y'all mean by "continously". I can judge with the help of my magic assurance that yous want to paint a color gradient or something like.

    Search codeguru or other programming sites for "color gradient". There are a lot of working examples.

    Like VictorN alredy stated calling InvalidateRect (and UpdateWindow) inside an OnPaint handler is wrong.

    Please get some more than cognition about how drawing has to be implemented in Windows (shortended/simplified): Windows "knows" when a certain window (or a office of information technology) has to exist redrawn. It and so sends a WM_PAINT to that window. That results - in the case of a MFC window - in a call of OnPaint.
    What is Invalidate() / InvalidateRect() / UpdateWindow() for?
    These functions take to be called if the contents (the stuff to exist drawn) has inverse. An application will tell the Windows window manager that a window (or a role of it) has to exist re-drawn. Since they will (unremarkably) force sending WM_PAINT'south yous must not call these functions inside an OnPaint handler!

    Quote Originally Posted by saraswathisrinath View Post

    After using this method, the image was fatigued but my other controls (Button, Tab) are subconscious in the window.

    A dialog (window) is a special example. Although information technology is possible to describe something in it'south OnPaint handler, that is (most always) the incorrect approach (like MikeAThon stated). Consider a dialog window as a container property some controls. The appearance (drawing) of the dialog should be implemented within the controls. To brand a long story short: That is the all-time way to handle unlike sizes of the same dialog in different circumstances (installed font sizes, ...).

    With regards
    PA


  9. #ix

    Re: Depict an epitome continously using pixel value in vc++

    My two cents notes, completing what is already discussed.

    Generally it's not a brilliant idea to direct draw in the client area of a dialog, except the case you desire to make full it with some fancy brush.
    That instance, indeed, the best place is in WM_ERASEBKGND message handler.

    Example

    Code:

    BOOL CMyDialog::OnEraseBkgnd(CDC* pDC)  {    CBrush* pbrushOld = pDC->SelectObject(&m_brushBack);    CRect rect;    pDC->GetClipBox(&rect);    pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);    pDC->SelectObject(pbrushOld);	 	    return TRUE; // no further default processing }
    I take given the above example only FYI.
    If you lot have a graph/epitome to show in a dialog, information technology'due south better to make a custom, ActiveX or simpler, a static command.
    For a static control, do the following:
    1. Derive your own class from CStatic, allow's say it CStaticImage.
    2. In CStaticImage form handle WM_ERASEBKGND and WM_PAINT messages.
    3. In the WM_ERASEBKGND office handler do nada; just return True to foreclose default processing.

      Code:

      BOOL CStaticImage::OnEraseBkgnd(CDC* pDC)  {    return TRUE; }
    4. Perform the drawing in WM_PAINT handler. If the drawing is complex, it's preferable to employ a retentivity device context to depict and then fleck-blit its contents into the destination device context.

      Lawmaking:

      void CStaticImage::OnPaint()  {    CPaintDC dc(this); // device context for painting     CDC dcMem;    dcMem.CreateCompatibleDC(&dc);    const int nSavedDC = dcMem.SaveDC();        CRect rc;    GetClientRect(rc);     CBitmap bmp;    bmp.CreateCompatibleBitmap(&dc, rc.Width(), rc.Peak());    dcMem.SelectObject(&bmp);     // merely for demo purpose:    dcMem.Rectangle(0, 0, rc.Width(), rc.Elevation());    dcMem.Ellipse(two, ii, 100, l);    dcMem.SetPixel(ten, 10, RGB(0,0,255));    // do all other drawing here...     // when the drawing is consummate, bit-blit into the target DC    dc.BitBlt(0, 0, rc.Width(), rc.Height(), &dcMem, 0, 0, SRCCOPY);     dcMem.RestoreDC(nSavedDC); }
    5. Finally, put the motion picture control into the dialog window.
      • In the resources editor, add a static (picture control) to dialog template; change its ID from IDC_STATIC to something else, due east.g. IDC_STATIC_IMAGE.
      • Using the Wizard, add to dialog form a control fellow member variable for IDC_STATIC_IMAGE, let'southward say it m_staticImage; be sure that m_staticImage is of type CStaticImage and "StaticImage.h" header is included.

    That's all.
    Note that above lawmaking is simply for demo purpose and you have to adapt it for your concrete needs.
    Further, you tin can add other methods to CStaticImage in order to pass drawig parameters, force redraw, and so on.

    Another one annotation:
    InvalidateRect(0) invalidates (marks for painting) the whole client area of the window. If the cartoon is very complex, may be preferable to pass only the area which effectively has to be painted at one moment. And, as already discussed here, it has no sense to call InvalidateRect inside the WM_PAINT message handler (it does not pb into an infinite loop, as long equally it does non send WM_PAINT message but, it has no sense).

    And a final one:
    Cartoon something pixel past pixel may be not a expert practical idea. Try to find and use "higher level" GDI functions.

    Last edited by ovidiucucu; November 30th, 2011 at 05:25 AM.

  10. #10

    Re: Draw an image continously using pixel value in vc++

    Hi Victor,

    sorry for the late answer. i already used the bitblt method & created a timer and call Invalidate(0); from within the timer handler (Kindly refer : MainDlg.cpp).

    But other controls of the window has hidden like buttons.

    Tin can you give any sample projection.

    Regards,

    Saraswathisrinath


  11. #11

    Re: Draw an image continously using pixel value in vc++

    Quote Originally Posted by ProgramArtist View Post

    How-do-you-do,

    Could you lot please give us more information abot what yous're trying to achieve (non how)?

    No, in the lawmaking yous've posted you describe some lines of pixels. You neither have a bitmap/image there nor are you cartoon ane.

    I don't know what you hateful by "continously". I tin guess with the assist of my magic balls that yous want to paint a color gradient or something similar.

    Search codeguru or other programming sites for "color gradient". There are a lot of working examples.

    Like VictorN alredy stated calling InvalidateRect (and UpdateWindow) within an OnPaint handler is incorrect.

    Please become some more knowledge about how drawing has to exist implemented in Windows (shortended/simplified): Windows "knows" when a certain window (or a part of it) has to be redrawn. It so sends a WM_PAINT to that window. That results - in the case of a MFC window - in a call of OnPaint.
    What is Invalidate() / InvalidateRect() / UpdateWindow() for?
    These functions have to be chosen if the contents (the stuff to be drawn) has inverse. An application volition tell the Windows window manager that a window (or a function of it) has to be re-drawn. Since they will (normally) force sending WM_PAINT'south you must not phone call these functions inside an OnPaint handler!

    A dialog (window) is a special case. Although it is possible to describe something in information technology's OnPaint handler, that is (almost e'er) the wrong approach (like MikeAThon stated). Consider a dialog window as a container holding some controls. The appearance (drawing) of the dialog should exist implemented within the controls. To make a long story short: That is the all-time manner to handle unlike sizes of the same dialog in different circumstances (installed font sizes, ...).

    With regards
    PA

    hullo,

    sad for the tardily reply.

    i. I'thousand receiving data from (Using series communication) RS232.

    I don't know vc++. At present just studying with the help of google.

    2. continuously means, I'm continuously receiving information from the serial port.

    So i like to draw that data in Onpaint() method.

    3. For that, start i used setpixel() method + invalidate(0); in onpaint of method.

    Then i used Bitblt method & created a timer and called the invalidate(0); within the timer.

    Now data are drawn continuously merely other controls of the dialog box similar buttons are hidden in the main window.

    Regards,

    Saraswathisrinath


  12. #12

    Re: Describe an prototype continously using pixel value in vc++

    Quote Originally Posted by ovidiucucu View Post

    My 2 cents notes, completing what is already discussed.

    More often than not information technology's not a brilliant thought to directly draw in the client area of a dialog, except the instance yous desire to fill it with some fancy castor.
    That case, indeed, the all-time place is in WM_ERASEBKGND message handler.

    Instance

    Code:

    BOOL CMyDialog::OnEraseBkgnd(CDC* pDC)  {    CBrush* pbrushOld = pDC->SelectObject(&m_brushBack);    CRect rect;    pDC->GetClipBox(&rect);    pDC->PatBlt(rect.left, rect.meridian, rect.Width(), rect.Peak(), PATCOPY);    pDC->SelectObject(pbrushOld);	 	    render TRUE; // no further default processing }

    I accept given the above example just FYI.
    If you accept a graph/paradigm to show in a dialog, it'south meliorate to make a custom, ActiveX or simpler, a static control.
    For a static command, practise the following:

    1. Derive your own form from CStatic, let's say information technology CStaticImage.
    2. In CStaticImage grade handle WM_ERASEBKGND and WM_PAINT letters.
    3. In the WM_ERASEBKGND part handler practise cypher; just return Truthful to foreclose default processing.

      Code:

      BOOL CStaticImage::OnEraseBkgnd(CDC* pDC)  {    return TRUE; }
    4. Perform the drawing in WM_PAINT handler. If the cartoon is circuitous, it's preferable to utilize a memory device context to describe and then bit-blit its contents into the destination device context.

      Lawmaking:

      void CStaticImage::OnPaint()  {    CPaintDC dc(this); // device context for painting     CDC dcMem;    dcMem.CreateCompatibleDC(&dc);    const int nSavedDC = dcMem.SaveDC();        CRect rc;    GetClientRect(rc);     CBitmap bmp;    bmp.CreateCompatibleBitmap(&dc, rc.Width(), rc.Height());    dcMem.SelectObject(&bmp);     // merely for demo purpose:    dcMem.Rectangle(0, 0, rc.Width(), rc.Height());    dcMem.Ellipse(two, 2, 100, l);    dcMem.SetPixel(10, 10, RGB(0,0,255));    // do all other drawing here...     // when the drawing is complete, bit-blit into the target DC    dc.BitBlt(0, 0, rc.Width(), rc.Height(), &dcMem, 0, 0, SRCCOPY);     dcMem.RestoreDC(nSavedDC); }
    5. Finally, put the picture control into the dialog window.
      • In the resource editor, add a static (picture control) to dialog template; change its ID from IDC_STATIC to something else, e.g. IDC_STATIC_IMAGE.
      • Using the Sorcerer, add to dialog course a control fellow member variable for IDC_STATIC_IMAGE, let's say it m_staticImage; exist sure that m_staticImage is of type CStaticImage and "StaticImage.h" header is included.

    That's all.
    Annotation that above code is simply for demo purpose and yous have to adapt it for your concrete needs.
    Further, you can add other methods to CStaticImage in order to pass drawig parameters, force redraw, and and then on.

    Another one note:
    InvalidateRect(0) invalidates (marks for painting) the whole client area of the window. If the cartoon is very circuitous, may be preferable to laissez passer only the area which effectively has to be painted at i moment. And, as already discussed hither, it has no sense to call InvalidateRect inside the WM_PAINT bulletin handler (information technology does non lead into an space loop, every bit long as it does non send WM_PAINT message but, it has no sense).

    And a final one:
    Drawing something pixel past pixel may be not a good applied thought. Effort to find and utilise "higher level" GDI functions.


    how-do-you-do,

    Sorry for the filibuster.

    Thank you and then much. i will endeavor this logic in my project.

    Regards,

    Saraswathisrinath


  13. #thirteen

    Re: Draw an image continously using pixel value in vc++

    Quote Originally Posted by saraswathisrinath View Post

    2. continuously means, I'yard continuously receiving data from the series port.

    So i similar to draw that data in Onpaint() method.

    The display of data is the whole raison d'�tre of a control. Dialogs merely firm controls. Dialogs should never be treated as if they WERE controls.

    Then, it's generally wrong to display changing information in the OnPaint method of the dialog.

    1 correct approach would be to derive a grade from (say) a static control, and change the OnPaint of the static control so that it had access to your data and could display your data equally desired (graphic, text, compass rose, whatever). Then, place the static control as a kid of the dialog. When new information is received, just phone call Invalidate on the static, and y'all're done.

    Mike


Posting Permissions

  • Yous may not post new threads
  • Yous may non post replies
  • You may not post attachments
  • You may non edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] lawmaking is On
  • [VIDEO] code is On
  • HTML code is Off

Click Here to Expand Forum to Full Width



strangewittappona.blogspot.com

Source: https://forums.codeguru.com/showthread.php?518794-Draw-an-image-continously-using-pixel-value-in-vc