从CWnd派生的一个窗口,启动后窗口无法自动刷新鼠标,仍然为沙漏状,必须要移除这个子窗口之外才可以自动刷新。太奇怪了

解决方案 »

  1.   

    ///////////////////////////////////////////////////////////////////////////
    // CurveWnd.cpp
    #include "stdafx.h"
    #include <math.h>
    #include "Gamma.h"
    #include "CurveWnd.h"
    #include "MemoryDC.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    ///////////////////////////////////////////////////////////////////////////
    // Globals
    CONST FLOAT CCurveWnd::fCoordinate[CCW_COORDINATE_COUNT] =
    {
    1, 2, 4, 8, 16, 24, 32, 48, 64, 80, 96 ,128, 160, 192, 224, 255
    };
    ///////////////////////////////////////////////////////////////////////////
    // CCurveWnd
    IMPLEMENT_DYNAMIC(CCurveWnd, CWnd)
    BEGIN_MESSAGE_MAP(CCurveWnd, CWnd)
    //{{AFX_MSG_MAP(CCurveWnd)
    ON_WM_PAINT()
    ON_WM_CREATE()
    ON_WM_ERASEBKGND()
    ON_WM_LBUTTONDOWN()
    ON_WM_MOUSEMOVE()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    ///////////////////////////////////////////////////////////////////////////
    // Constructor/Destructor
    CCurveWnd::CCurveWnd()
    {
    m_bDragging = FALSE;
    m_fGamma = 1.0f;
    CalcBrightnessValue();
    }
    CCurveWnd::~CCurveWnd()
    {}
    ///////////////////////////////////////////////////////////////////////////
    // Overrides
    BOOL CCurveWnd::Create(DWORD dwStyle, const CRect& rect, CWnd* pParentWnd, UINT nID)
    {
    //Must have a parent
    ASSERT_VALID(pParentWnd);

    //Create window
    CString strWndClass = AfxRegisterWndClass(CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW);
    dwStyle |= WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN;
    return CWnd::Create(strWndClass, NULL, dwStyle, rect, pParentWnd, nID);
    }
    ///////////////////////////////////////////////////////////////////////////
    // Internal Helpers
    VOID CCurveWnd::CalcBrightnessValue()
    {
    ASSERT(m_fGamma > 0);
    for(INT i=0; i<CCW_COORDINATE_COUNT; ++i)
    {
    m_fBrightness[i] = powf(fCoordinate[i], m_fGamma);
    }
    }
    VOID CCurveWnd::DrawCoordinates(CDC* pDC, CRect& rect)
    {
    CSize sizeText = pDC->GetTextExtent(_T("255"));
    rect.DeflateRect(sizeText.cx+2, sizeText.cy, sizeText.cx, sizeText.cy);

    //Draw axis
    CPen penAxis(PS_SOLID, 2, RGB(0,0,0));
    CPen* pOldPen = pDC->SelectObject(&penAxis);
    pDC->MoveTo(rect.left, rect.bottom);
    pDC->LineTo(rect.BottomRight());
    pDC->MoveTo(rect.left, rect.bottom);
    pDC->LineTo(rect.TopLeft());

    //Draw scale
    FLOAT fXStep = (FLOAT)rect.Width() / CCW_XASIS_SCALECOUNT;
    FLOAT fYStep = (FLOAT)rect.Height() / CCW_YASIS_SCALECOUNT;
    FLOAT f = (FLOAT)rect.left;
    for(INT i=0; i<=CCW_XASIS_SCALECOUNT*50; i+=50,f+=fXStep)//x
    {
    if(i == CCW_XASIS_SCALECOUNT*50) 
    {
    f = (FLOAT)rect.right;
    }
    pDC->MoveTo((INT)f, rect.bottom);
    pDC->LineTo((INT)f, rect.bottom-4);

    TCHAR szScale[5] = {0};
    sprintf(szScale, "%d", i);

    FLOAT fHalfWidth = (FLOAT)pDC->GetTextExtent(szScale).cx / 2;
    pDC->TextOut((INT)(f-fHalfWidth), rect.bottom+1, szScale);

    }
    f = (FLOAT)rect.bottom;
    for(i=0; i<=CCW_YASIS_SCALECOUNT*50; i+=50, f-=fYStep) //y
    {
    if(i == CCW_YASIS_SCALECOUNT*50) 
    {
    f = (FLOAT)rect.top+1;
    }
    pDC->MoveTo(rect.left, (INT)f);
    pDC->LineTo(rect.left+4, (INT)f);

    if(i == 0) 
    {
    continue;
    }

    TCHAR szScale[5] = {0};
    sprintf(szScale, "%d", i);

    CSize sizeScale = pDC->GetTextExtent(szScale);
    pDC->TextOut(rect.left-sizeScale.cx-2, (INT)f-sizeScale.cy/2, szScale);
    }
    pDC->SelectObject(pOldPen);
    }
    VOID CCurveWnd::DrawCurve(CDC* pDC, CRect& rect)
    {
    //  pDC->Draw3dRect(&rect, RGB(255,0,0), RGB(255,0,0));
    for(INT i=0; i<CCW_COORDINATE_COUNT; ++i)
    {
    FLOAT x = fCoordinate[i] / CCW_XASIS_MAX * rect.Width() + rect.left;
    FLOAT y = (1 - m_fBrightness[i] / CCW_YASIS_MAX) * rect.Height() + rect.top;

    m_rectDrag[i] = CRect((INT)x-3, (INT)y-3, (INT)x+3, (INT)y+3);
    pDC->Ellipse(&m_rectDrag[i]); if(i >= 1) 
    {
    pDC->MoveTo(m_rectDrag[i-1].CenterPoint());
    pDC->LineTo(m_rectDrag[i].CenterPoint());
    }
    }
    }
    VOID CCurveWnd::SetGammaValue(FLOAT fGamma)
    {
    m_fGamma = fGamma; CalcBrightnessValue();
    }
    ///////////////////////////////////////////////////////////////////////////
    // Message handlers
    VOID CCurveWnd::OnPaint() 
    {
    CRect rect(0,0,0,0);
    CPaintDC dc(this);
    CMemoryDC dcMemory;

    GetClientRect(&rect);
    dcMemory.Create(&dc, &rect);
    CFont* pOldFont = dcMemory.SelectObject(&m_fntLabel); DrawCoordinates(&dcMemory, rect);
    DrawCurve(&dcMemory, rect); dcMemory.SelectObject(pOldFont);
    dcMemory.Flush();
    }
    int CCurveWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if(CWnd::OnCreate(lpCreateStruct) == -1)
    {
    return -1;
    } //Create label font
    LOGFONT stLogFont = {0};
    ::SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &stLogFont, 0);
    m_fntLabel.CreateFontIndirect(&stLogFont);
    SetFont(&m_fntLabel);

    return 0;
    }
    BOOL CCurveWnd::OnEraseBkgnd(CDC* pDC) 
    {
    return FALSE;
    }
    void CCurveWnd::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    for(INT i=0; i<CCW_COORDINATE_COUNT; ++i)
    {
    if(m_rectDrag[i].PtInRect(point))
      {
      m_bDragging = TRUE;
      break;
      }
      }
    CWnd::OnLButtonDown(nFlags, point);
    }
    void CCurveWnd::OnMouseMove(UINT nFlags, CPoint point) 
    {
    if(m_bDragging && nFlags&MK_LBUTTON) 
    {
    SetCapture();
    ASSERT(this == CWnd::GetCapture());

    while(this == CWnd::GetCapture())
    {
    MSG msg = {0};
    if(!::GetMessage(&msg, NULL, 0, 0))
    {
    AfxPostQuitMessage(msg.wParam);
    break;
    } switch(msg.message)
    {
    case WM_LBUTTONUP: //鼠标左键弹起时,表示Drag结束
    {
    if(m_bDragging)
    {

    }
    break;
    }

    case WM_MOUSEMOVE:
    {
    if(m_bDragging)
    { }
    break;
    } default:
    {
    DispatchMessage(&msg);
    break;
    }
    }
    }
    ReleaseCapture();
    }

    CWnd::OnMouseMove(nFlags, point);
    }