Winapi 设置窗口';在画入一个不寻常的图案之前,它的背景是什么

Winapi 设置窗口';在画入一个不寻常的图案之前,它的背景是什么,winapi,mfc,Winapi,Mfc,我有一个窗口(CWndobject),我正在其中绘制位图图像 在此之前,我想将窗口的背景设置为特定的模式 这种模式可能会不时改变 如果理解正确,则需要重写窗口的OnCtlColor函数,并返回与所需图案兼容的画笔(根据内部数据结构重新计算) 我走对了吗 图案很不规则。它由“斑马条纹”组成,所有条纹的宽度相同,但(可能)高度不同。下面是一个示例: 甚至可以用这样的图案创建笔刷吗 如果是,则以下哪项功能最合适: CBrush::CreateBrushIndirect CBrush::Create

我有一个窗口(
CWnd
object),我正在其中绘制位图图像

在此之前,我想将窗口的背景设置为特定的模式

这种模式可能会不时改变

如果理解正确,则需要重写窗口的
OnCtlColor
函数,并返回与所需图案兼容的画笔(根据内部数据结构重新计算)

  • 我走对了吗

  • 图案很不规则。它由“斑马条纹”组成,所有条纹的宽度相同,但(可能)高度不同。下面是一个示例:

  • 甚至可以用这样的图案创建笔刷吗

    如果是,则以下哪项功能最合适:

    • CBrush::CreateBrushIndirect
    • CBrush::CreateDIBPatternBrush
    • CBrush::CreateHatchBrush
    • CBrush::CreatePatternBrush

    谢谢。

    我连刷子都不用了。此示例使用FillSolidRect和屏幕高度百分比中的条纹高度绘制一组条纹。如果您使用的是absolutes,应该很容易调整

    BOOL CChildView::OnEraseBkgnd(CDC* pDC)
    {
        CRect clientRect;
        GetClientRect(clientRect);
    
        const auto savedDC = pDC->SaveDC();
    
        // Make the co-ordinates system behave like percentages
        {
            pDC->SetMapMode(MM_ANISOTROPIC);
            pDC->SetWindowExt(100, 100);
            pDC->SetViewportExt(clientRect.right, clientRect.bottom);
        }
    
        // pair requires #include <utility>
        std::pair<int, COLORREF> stripeData[] =
        {
            { 8, RGB(220,220,220) },    // 8% of window height, light grey
            { 17, RGB(165,165,165) },   // 17% of window height, dark grey
            { 12, RGB(220,220,220) },   // etc. These should total 100%
            { 7, RGB(165,165,165) },
            { 23, RGB(220,220,220) },
            { 33, RGB(165,165,165) }
        };
    
        // NOTE: FillSolidRect changes the background color, so restore it at the
        // end of the function. RestoreDC will handle this; otherwise save the
        // GetBkColor return value and restore it by calling SetBkColor
    
        //auto oldBkColor = pDC->GetBkColor();
    
        // Draw the stripes
        CRect stripeRect{0,0,100,0};
        for (auto const& data : stripeData)
        {
            stripeRect.bottom = stripeRect.top + data.first;
            pDC->FillSolidRect(stripeRect, data.second);
            stripeRect.OffsetRect(0, data.first);
        }
    
        //pDC->SetBkColor(oldBkColor);
    
        pDC->RestoreDC(savedDC);
    
        return TRUE;
    }
    
    boolcchildview::OnEraseBkgnd(CDC*pDC)
    {
    正确的clientRect;
    GetClientRect(clientRect);
    const auto savedDC=pDC->SaveDC();
    //使坐标系统的行为类似于百分比
    {
    pDC->SetMapMode(毫米);
    pDC->SetWindowExt(100100);
    pDC->SetViewportExt(clientRect.right,clientRect.bottom);
    }
    //配对需要#包括
    std::成对条带数据[]=
    {
    {8,RGB(220220)},//窗高的8%,浅灰色
    {17,RGB(165165165)},//17%的窗高,深灰色
    {12,RGB(220220)},//等等。这些总数应为100%
    {7,RGB(165165165)},
    {23,RGB(220220)},
    {33,RGB(165165165)}
    };
    //注意:FillSolidRect会更改背景色,因此在
    //函数结束。RestoreDC将处理此问题;否则保存
    //GetBkColor返回值,并通过调用SetBkColor将其还原
    //自动oldBkColor=pDC->GetBkColor();
    //划清界限
    正确的{0,0100,0};
    用于(自动常量和数据:条带数据)
    {
    strippright.bottom=strippright.top+data.first;
    pDC->FillSolidRect(条带竖立,数据秒);
    StripInstitute.OffsetRect(0,data.first);
    }
    //pDC->SetBkColor(oldBkColor);
    pDC->RestoreDC(savedDC);
    返回TRUE;
    }
    
    我甚至连刷子都不用。此示例使用FillSolidRect和屏幕高度百分比中的条纹高度绘制一组条纹。如果您使用的是absolutes,应该很容易调整

    BOOL CChildView::OnEraseBkgnd(CDC* pDC)
    {
        CRect clientRect;
        GetClientRect(clientRect);
    
        const auto savedDC = pDC->SaveDC();
    
        // Make the co-ordinates system behave like percentages
        {
            pDC->SetMapMode(MM_ANISOTROPIC);
            pDC->SetWindowExt(100, 100);
            pDC->SetViewportExt(clientRect.right, clientRect.bottom);
        }
    
        // pair requires #include <utility>
        std::pair<int, COLORREF> stripeData[] =
        {
            { 8, RGB(220,220,220) },    // 8% of window height, light grey
            { 17, RGB(165,165,165) },   // 17% of window height, dark grey
            { 12, RGB(220,220,220) },   // etc. These should total 100%
            { 7, RGB(165,165,165) },
            { 23, RGB(220,220,220) },
            { 33, RGB(165,165,165) }
        };
    
        // NOTE: FillSolidRect changes the background color, so restore it at the
        // end of the function. RestoreDC will handle this; otherwise save the
        // GetBkColor return value and restore it by calling SetBkColor
    
        //auto oldBkColor = pDC->GetBkColor();
    
        // Draw the stripes
        CRect stripeRect{0,0,100,0};
        for (auto const& data : stripeData)
        {
            stripeRect.bottom = stripeRect.top + data.first;
            pDC->FillSolidRect(stripeRect, data.second);
            stripeRect.OffsetRect(0, data.first);
        }
    
        //pDC->SetBkColor(oldBkColor);
    
        pDC->RestoreDC(savedDC);
    
        return TRUE;
    }
    
    boolcchildview::OnEraseBkgnd(CDC*pDC)
    {
    正确的clientRect;
    GetClientRect(clientRect);
    const auto savedDC=pDC->SaveDC();
    //使坐标系统的行为类似于百分比
    {
    pDC->SetMapMode(毫米);
    pDC->SetWindowExt(100100);
    pDC->SetViewportExt(clientRect.right,clientRect.bottom);
    }
    //配对需要#包括
    std::成对条带数据[]=
    {
    {8,RGB(220220)},//窗高的8%,浅灰色
    {17,RGB(165165165)},//17%的窗高,深灰色
    {12,RGB(220220)},//等等。这些总数应为100%
    {7,RGB(165165165)},
    {23,RGB(220220)},
    {33,RGB(165165165)}
    };
    //注意:FillSolidRect会更改背景色,因此在
    //函数结束。RestoreDC将处理此问题;否则保存
    //GetBkColor返回值,并通过调用SetBkColor将其还原
    //自动oldBkColor=pDC->GetBkColor();
    //划清界限
    正确的{0,0100,0};
    用于(自动常量和数据:条带数据)
    {
    strippright.bottom=strippright.top+data.first;
    pDC->FillSolidRect(条带竖立,数据秒);
    StripInstitute.OffsetRect(0,data.first);
    }
    //pDC->SetBkColor(oldBkColor);
    pDC->RestoreDC(savedDC);
    返回TRUE;
    }
    
    请看这里:@KonstantinL:好的,这是个不错的主意。这就回答了我的第一个问题,我可以在
    OnEraseBkgnd
    上画出我想要的图案。但现在我知道如何使用
    CDC::Rectangle
    。有没有什么方法可以轻松地创建一个画笔,然后用它画出整个背景?谢谢。可能是这个:CreateDIBPatternBrushPt()。再看看这里:@KonstantinL:好的,这是个不错的主意。这就回答了我的第一个问题,我可以在
    OnEraseBkgnd
    上画出我想要的图案。但现在我知道如何使用
    CDC::Rectangle
    。有没有什么方法可以轻松地创建一个画笔,然后用它画出整个背景?谢谢。可能是这个:CreateDIBPatternBrushPt()。