如何在 child window 中重绘 UI?

How to redraw the UI in a child window?

我正在创建一个 child window 来移动带有滚动条的 UI。 UI指的是滚动条的位置和运行s MoveWindow().

我希望在 运行 UpdateWindow() 时重绘 UI,但残像仍然存在。 重绘的正确方法应该是什么?

Parent Window

BOOL CScrollWindowDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    InnerWindow* inwin = new InnerWindow();
    inwin->Create(NULL, NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL, CRect(50, 50, 600, 450), this, NULL);
    inwin->Init();

    return TRUE;
}

Child Window

#pragma once
#include <afxwin.h>
#include <memory>

class InnerWindow : public CWnd
{
public:
    InnerWindow();
    ~InnerWindow();

    void Init();

protected:
    afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
    DECLARE_MESSAGE_MAP()

private:
    std::unique_ptr<CStatic> m_label;
    SCROLLINFO m_scrollinfo;
};

void InnerWindow::Init()
{
    m_label = std::unique_ptr<CStatic>(new CStatic());
    m_label->Create(L"Sample My Label", WS_CHILD | WS_VISIBLE, CRect(10, 10, 300, 30), this, 0x1010);
    m_label->ShowWindow(TRUE);

    ZeroMemory(&m_scrollinfo, sizeof(m_scrollinfo));
    m_scrollinfo.fMask = SIF_DISABLENOSCROLL | SIF_ALL;
    m_scrollinfo.nMin = 0;
    m_scrollinfo.nMax = 400 * 2;
    m_scrollinfo.nPage = 400;
    m_scrollinfo.nPos = 0;
    SetScrollInfo(SB_VERT, &m_scrollinfo);
}

void InnerWindow::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    if (pScrollBar == nullptr)
    {
        switch (nSBCode)
        {
        case SB_THUMBTRACK:
            m_label->MoveWindow(10, 10 + nPos, 290, 20);
            m_scrollinfo.nPos = nPos;
            SetScrollInfo(SB_VERT, &m_scrollinfo);

            Invalidate();
            UpdateWindow();
            break;

        default:
            break;
        }
    }

    CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
}

您没有在此处为您的 InnerWindow 指定 window class:

inwin->Create(NULL, NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL, CRect(50, 50, 600, 450), this, NULL);

没有人绘制 window 或擦除其背景。

如果您对此没有任何特殊需求 window,我会使用 STATIC:

inwin->Create(L"STATIC", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL, CRect(50, 50, 600, 450), this, NULL);