我有个信号灯的图,1秒绿,1秒变红,可是因为不是在onpaint里面画的,所以最小化后,或者移出频幕再移回来就消失了。因为图是动态的,不知道怎样画在onpaint里面,所以就画在外面了,有没有方法可以既不再onpaint里面写绘图函数,又可以不被刷新掉的方法?

解决方案 »

  1.   

    在onpaint里面画,另外,你根据需要保存对应的变量,在onpaint里面用!
    然后,在定时器里面,也要画,避免每次调用刷新!
      

  2.   

    在OnPaint里面画呀,或者传递参数自己加个MyDraw函数在OnPaint里面调用
      

  3.   

    不知道怎样画到onpaint里面,我需要在线程里面控制绘图啊
      

  4.   

    必须在onndraw或者onpaint里面把绘制的代码写一遍了
      

  5.   

    画图函数写在onpaint里,在线程里调用onpaint就是了。
      

  6.   

    把图片弄成类的成员,每次需要画的时候,图片成员赋值,调用onpaint画
      

  7.   

    在OnPaint里画,每次在别处需要画的时候都用InvalidateRect代替。只有这种程序架构是最合理的。
      

  8.   

    画图都是在OnPaint里的,你可以在其他线程里对是否画进行控制,if(BOOL)就可以了
      

  9.   

    在OnPaint里绘图
    在线程里控制变量然后Invalidate
      

  10.   

    设置一个BOOL类型的判断变量,在定时器里改变这个变量是TRUE或FALSE,然后在OnPaint里根据判断这个变量来进行绘制,你可以还是在外部进行绘制,因为你只是想使当窗口重绘的时候图象也进行重绘,那么你只需要在OnSize里加上一个Invalidate就行了,并不需要一直在OnPaint里绘,也就是说,OnPaint只起到了初始化和当窗口进行重绘的时候的图形的重绘。
      

  11.   

    基本思想是必须在OnPaint中画图,把刷新交给系统去维护.
    设你要画不同的图片
      1.在OnPaint 所在窗口建立一图片路径变量m_a;
      2.在onpaint 根据m_a把图片画上
      3.当你获得了其他图片后,把图片路径赋给变量m_a
      然后让OnPaint所在的窗口重绘界面:
       pMainWnd->Invalidate();
       
      

  12.   

    void CYourWnd::OnPaint()
    {
        CPaintDC dc(this);
        if (m_bRedLight)
        {
            // Draw Red light
        }
        else
        {
            // Draw green light
        }
    }void CYourWnd::OnTimer(...)
    {
        m_bRedLight = TRUE or FALSE;
        Invalidate();
    }
      

  13.   

    把信号灯做成一个控件,如 CLed ,
    然后再实现SetState函数SetState(0)//设为绿色状态
    SetState(1)//设为红色状态这样以后调用,就比较简单,而且可以复用。如果需要我可以发一个给你
      

  14.   

    建议在定时器,或其他绘图函数里面将invalidaterect函数的第三个参数弄成FALSE,这样每次颜色变换的时候不会将其原本的颜色擦出,或者将你的绘图函数里的变量设成全局变量,在ONPAINT里面调用
      

  15.   

    我就把代码贴出来
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    led.cpp
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Led.cpp : implementation file
    //#include "stdafx.h"
    #include "DataProcess.h"
    #include "Led.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CLedCLed::CLed()
    {
    m_LedBitmap.LoadBitmap(IDB_LEDS);
    m_nLedColor = LED_COLOR_RED;
    m_nLedMode = LED_OFF;
    m_nLedShape = LED_ROUND;
    }CLed::~CLed()
    {
    VERIFY(m_LedBitmap.DeleteObject());
    }
    BEGIN_MESSAGE_MAP(CLed, CStatic)
    //{{AFX_MSG_MAP(CLed)
    ON_WM_CREATE()
    ON_WM_ERASEBKGND()
    ON_WM_PAINT()
    ON_WM_TIMER()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CLed message handlersint CLed::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CStatic::OnCreate(lpCreateStruct) == -1)
    return -1;

    // TODO: Add your specialized creation code here

    return 0;
    }BOOL CLed::OnEraseBkgnd(CDC* pDC) 
    {
    // TODO: Add your message handler code here and/or call default

    return TRUE;
    }void CLed::OnPaint() 
    {
    CPaintDC dc(this); // device context for painting
    DrawLed(&dc,m_nLedColor,m_nLedMode,m_nLedShape);
    // TODO: Add your message handler code here

    // Do not call CStatic::OnPaint() for painting messages
    }void CLed::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    if(nIDEvent == PING_TIMER)
    {
    SetLed(m_nLedColor,CLed::LED_OFF,m_nLedShape);
    KillTimer(nIDEvent);
    m_bPingEnabled = FALSE;
    }
    CStatic::OnTimer(nIDEvent);
    }///////////////////////////////////////////////////////////////////////////////
    // Name: SetLed
    // Description: This method will draw the LED to the specified DC.
    //
    // Entry:
    // CDC *pDC - DC to draw to
    //
    // int iLedColor - Where color is defined by:
    //   LED_COLOR_RED
    // LED_COLOR_GREEN
    // LED_COLOR_YELLOW
    // LED_COLOR_BLUE
    //
    // int iMode - where mode is defined by:
    // LED_ON
    // LED_OFF
    // LED_DISABLED
    //
    // int iShape - where shape is defined by:
    // LED_ROUND
    // LED_SQUARE
    ///////////////////////////////////////////////////////////////////////////////
    void CLed::DrawLed(CDC *pDC,int nLEDColor, int nMode, int nShape)
    {
    CRect rect;
    GetClientRect(&rect); //
    // Center led within an oversized window
    //
    if(rect.Width() >= LED_SIZE && rect.Height() >= LED_SIZE)
    {
    int nWidth = rect.Width();
    int nHeight = rect.Height();
    rect.left += (nWidth - LED_SIZE)/2;
    rect.right -= (nWidth - LED_SIZE)/2;
    rect.top += (nHeight - LED_SIZE)/2;
    rect.bottom -= (nHeight - LED_SIZE)/2;
    } //
    // Prepare temporary DCs and bitmaps
    //
    CBitmap TransBitmap;
    TransBitmap.CreateBitmap(LED_SIZE,LED_SIZE,1,1,NULL);
    CBitmap bitmapTemp;
    CBitmap* pBitmap = &m_LedBitmap;
    CDC srcDC;
    CDC dcMask;
    CDC TempDC;
    TempDC.CreateCompatibleDC(pDC);
    srcDC.CreateCompatibleDC(pDC);
    dcMask.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = srcDC.SelectObject(pBitmap);
    CBitmap* pOldMaskbitmap = dcMask.SelectObject(&TransBitmap);
    bitmapTemp.CreateCompatibleBitmap(pDC,LED_SIZE,LED_SIZE); //
    // Work with tempDC and bitmapTemp to reduce flickering
    //
    CBitmap *pOldBitmapTemp = TempDC.SelectObject(&bitmapTemp);
    TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, pDC, rect.left, rect.top, SRCCOPY);  //
    // Create mask
    //
    COLORREF OldBkColor = srcDC.SetBkColor(RGB(255,0,255));
    dcMask.BitBlt(0, 0, LED_SIZE, LED_SIZE,&srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCCOPY); 
    TempDC.SetBkColor(OldBkColor); //
    // Using the IDB_LEDS bitmap, index into the bitmap for the appropriate
    // LED. By using the mask color (RGB(255,0,255)) a mask has been created
    // so the bitmap will appear transparent.
    //
    TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, &srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCINVERT); 
    TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE,&dcMask, 0, 0, SRCAND); 
    TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, &srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCINVERT);  //
    // Since the actual minipulation is done to tempDC so there is minimal
    // flicker, it is now time to draw the result to the screen.
    //
    pDC->BitBlt(rect.left, rect.top, LED_SIZE, LED_SIZE, &TempDC, 0, 0, SRCCOPY); 

    //
    // House cleaning
    //
    srcDC.SelectObject(pOldBitmap);
    dcMask.SelectObject(pOldMaskbitmap);
    TempDC.SelectObject(pOldBitmapTemp);
    VERIFY(srcDC.DeleteDC());
    VERIFY(dcMask.DeleteDC());
    VERIFY(TempDC.DeleteDC());
    VERIFY(TransBitmap.DeleteObject());
    VERIFY(bitmapTemp.DeleteObject());
    }///////////////////////////////////////////////////////////////////////////////
    // Name: SetLed
    // Description: This method will draw and set led parameters.
    //
    // Entry: int iLedColor - Where color is defined by:
    //   LED_COLOR_RED
    // LED_COLOR_GREEN
    // LED_COLOR_YELLOW
    // LED_COLOR_BLUE
    //
    // int iMode - where mode is defined by:
    // LED_ON
    // LED_OFF
    // LED_DISABLED
    //
    // int iShape - where shape is defined by:
    // LED_ROUND
    // LED_SQUARE
    ///////////////////////////////////////////////////////////////////////////////
    void CLed::SetLed(int nLedColor, int nMode, int nShape)
    {
    m_nLedColor = nLedColor;
    m_nLedMode = nMode;
    m_nLedShape = nShape; CDC *pDC;
    pDC = GetDC();
    DrawLed(pDC,m_nLedColor,m_nLedMode,m_nLedShape);
    ReleaseDC(pDC);
    }///////////////////////////////////////////////////////////////////////////////
    // Name: Ping
    // Description: This method will turn the led on for dwTimeout milliseconds and
    // then turn it off.
    //
    // Entry: DWORD dwTimeout - Time out in  milliseconds
    ///////////////////////////////////////////////////////////////////////////////
    void CLed::Ping(DWORD dwTimeout)
    {
    // Return if pinging
    if(m_bPingEnabled == TRUE)
    {
    KillTimer(PING_TIMER);
    } m_bPingEnabled = TRUE;
    SetLed(m_nLedColor,CLed::LED_ON,m_nLedShape);
    SetTimer(PING_TIMER,dwTimeout,NULL);
    }
    **************************************
    led.h
    **************************************
    #if !defined(AFX_LED_H__BABC9426_EF66_49D5_B54F_EA10DD7A0344__INCLUDED_)
    #define AFX_LED_H__BABC9426_EF66_49D5_B54F_EA10DD7A0344__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // Led.h : header file
    //
    #define LED_SIZE 16 // Led are 16 X 16 pixels
    #define PING_TIMER  10
    /////////////////////////////////////////////////////////////////////////////
    // CLed windowclass CLed : public CStatic
    {
    protected:
    int m_nLedColor, m_nLedMode, m_nLedShape;
    DWORD m_dwPingTimeout;
    BOOL m_bPingEnabled;
    CBitmap m_LedBitmap;public:

    enum {
    LED_ROUND = 0, // Circle starts at row 0
    LED_SQUARE = LED_SIZE * 4, // squares start at row 4
    };
    enum {
    LED_COLOR_RED = 0 * LED_SIZE, // Row 0
    LED_COLOR_GREEN = 1 * LED_SIZE, // Row 1
    LED_COLOR_YELLOW = 2 * LED_SIZE, // Row 2
    LED_COLOR_BLUE = 3 * LED_SIZE, // Row 3
    };
    enum {
    LED_ON = 0, // Column 0
    LED_OFF = 1, // Column 1
    LED_DISABLED = 2, // Column 2
    };
    // Construction
    public:
    CLed();// Attributes
    public:// Operations
    public:
    void SetLed(int nLedColor, int nMode, int nShape);
    int GetLedMode(){return m_nLedMode;}
    void Ping(DWORD dwTimeout = 1000);
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CLed)
    //}}AFX_VIRTUAL// Implementation
    public:
    void DrawLed( CDC *pDC, int nLEDColor, int nMode, int nShape );
    virtual ~CLed(); // Generated message map functions
    protected:
    //{{AFX_MSG(CLed)
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    afx_msg void OnPaint();
    afx_msg void OnTimer(UINT nIDEvent);
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_LED_H__BABC9426_EF66_49D5_B54F_EA10DD7A0344__INCLUDED_)********************
    调用方法
    ********************
    CLed m_led1;
    // 设为红色
    m_led1.SetLed(CLed::LED_COLOR_RED,CLed::LED_ON,CLed::LED_SQUARE);
    // 设为绿色
    m_led1.SetLed(CLed::LED_COLOR_GREEN,CLed::LED_ON,CLed::LED_SQUARE);