1

我正在尝试使用 MFC 制作交互式图形,其中可以使用鼠标单击更改图形中样本点的 y 轴。我使用本教程实现了双缓冲
在此处输入链接描述. 我还应该指出,我需要不时为我的程序更改视口的来源。但是,当我单击要更新的样本点的图形时,我仍然可以看到它在闪烁。这不是一个不便,但我需要扩展这个图以包含许多样本点和其他特征,如网格线、轴、标签、边界区域等,我担心闪烁可能会成为我未来的问题,因为这个项目的规模越来越大。实施双缓冲似乎对输出没有任何改变。此外,既然我已经实现了双缓冲,程序似乎在执行过程中停止(当我在 Visual Studio 中以调试模式运行它时)并出现以下错误:

Unhandled exception at 0xffffff3a in graph_on_dlgbox.exe: 0xC0000005: Access violation reading location 0xffffff3a.

我仍然不确定是什么导致它出现,但如果我开始快速随机点击图形区域似乎会发生。由于我在不使用双缓冲的代码中(还)没有看到这个错误,我假设它与双缓冲代码有关,但我不确定。
无论如何,我想一次解决这个问题,第一个问题是闪烁。这是我没有双缓冲的代码:

void Cgraph_on_dlgboxDlg::OnPaint()
{
    CPaintDC dc(this);
    if (IsIconic())
    {
        // CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }

    CPen pen;
    COLORREF pencolour = RGB(0, 0, 0);
    COLORREF brushcolour = RGB(0, 0, 255);
    COLORREF graphColour = RGB(0, 0, 150);

    // Draw boarder
    pen.CreatePen(PS_SOLID, 3, pencolour);
    // CBrush brush(HS_CROSS, brushcolour);
    dc.SetBkMode(TRANSPARENT);
    dc.SetMapMode(MM_TEXT);
    dc.SetViewportOrg(theGraph.x1, theGraph.y1);
    // Dc.SetViewportExt(theGraph.width, theGraph.height);
     dc.SelectObject(&pen);
    // dc.SelectObject(&brush);

    // Draw graph boundary
    CPoint point1(0,0);
    point1.x = 0;
    point1.y = 0;
    CPoint point2(0,0);
    point2.x = point1.x + theGraph.width;
    point2.y = point1.y + theGraph.height;
    dc.Rectangle(CRect(point1, point2));
    pen.DeleteObject();

    // Draw Horizontal at 0
    pen.CreatePen(PS_SOLID, 1, pencolour);
    dc.SelectObject(&pen);
    dc.MoveTo(0, theGraph.height - ORG_DIST_FROM_BOTTOM);
    dc.LineTo(theGraph.width, theGraph.height - ORG_DIST_FROM_BOTTOM);
    pen.DeleteObject();

    // Shift overall graph origin from top left corner to beginning of this horizontal line
    dc.SetViewportOrg(theGraph.x1, theGraph.y1 + theGraph.height - ORG_DIST_FROM_BOTTOM); // dc.SetViewportOrg() always works relative to the clinet origin

    // Draw graph line
    pen.CreatePen(PS_SOLID, 2, graphColour);
    dc.SelectObject(&pen);
    for(int i = 0; i<NUM_OF_SECTIONS_IN_GRAPH; i++){
        dc.MoveTo(graphSamplePoints[i].x, graphSamplePoints[i].y);
        dc.LineTo(graphSamplePoints[i+1].x, graphSamplePoints[i+1].y);
    }

    // draw circles at graph sample points
    for(int i = 0; i<NUM_OF_POINTS_IN_GRAPH; i++){
        CIRCLE(dc, graphSamplePoints[i].x, graphSamplePoints[i].y, GRP_SMP_RAD);        
    }
}

这是带有双缓冲的修改版本:

void Cgraph_on_dlgboxDlg::OnPaint()
{

    // /*****
    CPaintDC dc_blt(this);
    CDC dc;
    CBitmap bmpDC;
    // CRect rcClient;
    // GetClientRect(rcClient);

    if (IsIconic())
    {
        // CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }

    dc.CreateCompatibleDC(&dc_blt);
    // bmpDC.CreateCompatibleBitmap(&dc_blt, rcClient.Width(),rcClient.Height());
    bmpDC.CreateCompatibleBitmap(&dc_blt, theGraph.width,theGraph.height );
    dc.SelectObject(&bmpDC);

    // ----------- After this point, do all operations considering (0,0) to be the origin of the bitmap
    // consider bitmap coordinates a device coordinates for Viewport operations

    CPen pen;
    COLORREF pencolour = RGB(0, 0, 0);
    COLORREF brushcolour = RGB(0, 0, 255);
    COLORREF graphColour = RGB(0, 0, 150);

    // Draw boarder
    pen.CreatePen(PS_SOLID, 3, pencolour);
    // CBrush brush(HS_CROSS, brushcolour);
    dc.SetBkMode(TRANSPARENT);
    dc.SetMapMode(MM_TEXT);
    // dc.SetViewportOrg(theGraph.x1, theGraph.y1);
    // dc.SetViewportExt(theGraph.width, theGraph.height);
     dc.SelectObject(&pen);
    // dc.SelectObject(&brush);

    // Draw graph boundary
    CPoint point1(0,0);
    point1.x = 0;
    point1.y = 0;
    CPoint point2(0,0);
    point2.x = point1.x + theGraph.width;
    point2.y = point1.y + theGraph.height;
    dc.Rectangle(CRect(point1, point2));
    pen.DeleteObject();

    // Draw Horizontal at 0
    pen.CreatePen(PS_SOLID, 1, pencolour);
    dc.SelectObject(&pen);
    dc.MoveTo(0, theGraph.height - ORG_DIST_FROM_BOTTOM);
    dc.LineTo(theGraph.width, theGraph.height - ORG_DIST_FROM_BOTTOM);
    pen.DeleteObject();

    // Shift overall graph origin from top left corner to beginning of this horizontal line
    // dc.SetViewportOrg(theGraph.x1, theGraph.y1 + theGraph.height - ORG_DIST_FROM_BOTTOM); // dc.SetViewportOrg() always works relative to the client area origin
    // New origin defined in terms of the Bitmap's origin
    dc.SetViewportOrg(0, theGraph.height - ORG_DIST_FROM_BOTTOM);

    // Draw graph line
    pen.CreatePen(PS_SOLID, 2, graphColour);
    dc.SelectObject(&pen);
    for(int i = 0; i<NUM_OF_SECTIONS_IN_GRAPH; i++){
        dc.MoveTo(graphSamplePoints[i].x, graphSamplePoints[i].y);
        dc.LineTo(graphSamplePoints[i+1].x, graphSamplePoints[i+1].y);
    }

    // draw circles at graph sample points
    for(int i = 0; i<NUM_OF_POINTS_IN_GRAPH; i++){
        CIRCLE(dc, graphSamplePoints[i].x, graphSamplePoints[i].y, GRP_SMP_RAD);        
    }

    dc.SetViewportOrg(0, 0);
    // dc_blt.BitBlt(rcClient.left+100,rcClient.top+50,rcClient.Width(), rcClient.Height(), &dc, 0, 0, SRCCOPY);
    // dc_blt.BitBlt(0,0,rcClient.Width(), rcClient.Height(), &dc, theGraph.x1, theGraph.y1, SRCCOPY);

    dc_blt.BitBlt(theGraph.x1,theGraph.y1, theGraph.width, theGraph.height, &dc, 0, 0, SRCCOPY);
    // --- Bring the bitmap to this particular location on screen specified by (theGraph.x1,theGraph.y1, theGraph.width, theGraph.height)

    // dc_blt.BitBlt(0,0,theGraph.width, theGraph.height, &dc, 0, 0, SRCCOPY);
    // dc_blt.BitBlt(theGraph.x1,theGraph.y1,theGraph.width, theGraph.height, &dc, 0, 0, SRCCOPY);
    // *****/

     m_bMyDraw = FALSE;
}

这是输出的示例屏幕截图: 在此处输入图像描述

点击可以改变图形上采样点的y轴值,每次点击后程序都会重绘图形,调用InvalidateRect()时将图形的面积作为要重绘的矩形。
在此处输入图像描述 样本点的坐标存储在 CPoint 对象数组中,每次在相应区域单击图形时都会更新其成员。然后,由于对 的调用,图形会重新绘制InvalidateRect(),但会闪烁;当然,除非程序在调试模式下崩溃并出现以下错误:
在此处输入图像描述

如何去除闪烁?

- - 更新 - -

BOOL Cgraph_on_dlgboxDlg::OnEraseBkgnd(CDC* pDC)
{
    // TODO: Add your message handler code here and/or call default
    if ( m_bMyDraw )
        return TRUE;
    else
        return CDialogEx::OnEraseBkgnd(pDC);
}

这个函数是这样制作的,因为它在我之前提到的教程中是这样完成的
------ UPDATE 2 ---------- If I just put return TRUE; 在上述函数的主体中,闪烁似乎消失了,但现在输出看起来像这样 在此处输入图像描述
对话框背景似乎已经取走了我的 Visual Studio 窗口的内容。我该如何防止这种情况?

4

3 回答 3

3

你很近!双缓冲的想法是将窗口中的每个像素只绘制一次。如果它被绘制零次,像 Visual Studio 这样的工件仍然存在。如果第一次画,然后用不同的颜色再次画,你会看到闪烁。因此,为确保绘制每个像素,请创建兼容的 dc 窗口的完整宽度和高度,以便在将其复制到 时CPaintDC,它会覆盖整个区域而不仅仅是theGraph. 继续返回TRUEOnEraseBkgnd这样像素就不会先画进去OnEraseBkgnd,然后再画进去OnPaint

于 2013-07-06T01:41:26.663 回答
2

防止闪烁的工作方式是首先从 OnEraseBkgnd 返回 TRUE 以抑制默认擦除。但是您的 OnPaint 代码必须包含对窗口的完全擦除。您不这样做是为了获得源代码的背景图像或之前的任何内容。因此,向您的 OnPaint 添加一个 FillSolidRect 调用以清除窗口。

在调用 CDialogEx::OnPaint 之前创建 CPaintDC 会破坏对话框正确绘制自身的能力,因为该函数也会创建 CPaintDC。但每条绘制消息只允许调用一次 CPaintDC。为了避免这个问题,您需要一种完全不同的方法。对话框上应该有一个图片控件(CStatic),并且您应该在从 CStatic 派生的类中绘制图形。

于 2013-07-05T14:24:34.087 回答
2

两件事情 :

  1. 您是否确保 OnEraseBkgnd() 只返回 TRUE 并且不调用基类来擦除视图?

  2. 您不需要为 OnPaint() 中的双缓冲进行所有绘制。在 OnPaint() 中你需要做的就是 BitBlt。您可以在 UpdateRect() 函数中绘制内存位图,该函数在您需要更新屏幕时调用,然后调用 InvalidateRect() 来更新屏幕。我已经发布了一些关于我在这里多次使用的无闪烁双缓冲方法的代码,这可能会有所帮助。

于 2013-07-05T07:37:59.783 回答