【问题标题】:Setting a window's background to an unusual pattern before drawing into it在绘制窗口之前将其背景设置为不寻常的图案
【发布时间】:2017-05-31 20:51:55
【问题描述】:

我有一个窗口(@98​​7654325@ 对象),我正在其中绘制位图图像。

在此之前,我想将窗口的背景设置为特定的图案。

模式可能会不时改变。

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

  1. 我走对了吗?

  2. 图案相当不规则。它由“斑马条纹”组成,所有这些条纹都具有相同的宽度,但(可能)具有不同的高度。这是一个图解示例:

甚至可以用这种图案制作画笔吗?

如果是,那么以下哪个函数最合适:

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

谢谢。

【问题讨论】:

标签: winapi mfc


【解决方案1】:

我什至不会用刷子打扰。此示例使用 FillSolidRect 和屏幕高度百分比中的条纹高度绘制一堆条纹。如果您使用绝对值,应该很容易调整。

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;
}

【讨论】:

    猜你喜欢
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多