使用direct2D在childwindow上绘制图像

时间:2017-03-11 06:41:51

标签: c++ api direct2d

它的C ++代码。 首先,我会告诉你我的代码。

#include "stdafx.h"
#include "CuttingImage.h"
CuttingImage* F_PCutImageWndCss = 0;
LRESULT CALLBACK Ch_CuttingImageProc(HWND _hWnd, UINT _msg, WPARAM _wParam, LPARAM _lParam)
{
    switch (_msg) {
    case WM_PAINT:
        F_PCutImageWndCss->render();
        break;
    default:

        return DefWindowProc(_hWnd, _msg, _wParam, _lParam);
        break;
    }
}

HRESULT CuttingImage::create(HWND parent)
{
    D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
        &m_fac);
    F_PCutImageWndCss = this;
    WNDCLASS wc;
    ZeroMemory(&wc, sizeof(wc));
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = Ch_CuttingImageProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = WinApp::getInstance()->getHInstance();
    wc.hIcon = LoadIcon(wc.hInstance, NULL);
    wc.lpszClassName = L"CutImgWnd";
    wc.hCursor = LoadCursor(NULL, IDC_HELP);
    wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
    wc.lpszMenuName = NULL;
    RegisterClassW(&wc);

    m_Hwnd = CreateWindowW(wc.lpszClassName, NULL, WS_POPUP| WS_VISIBLE|WS_BORDER|WS_CAPTION|WS_SYSMENU, 100, 100, 700, 700, parent, NULL, wc.hInstance, NULL);
    RECT rect;
    GetClientRect(m_Hwnd, &rect);
    D2D1_SIZE_U size;
    size = D2D1::SizeU(rect.right - rect.left, rect.bottom - rect.top);
    m_fac->CreateHwndRenderTarget(D2D1::RenderTargetProperties(),
        D2D1::HwndRenderTargetProperties(m_Hwnd, size, D2D1_PRESENT_OPTIONS_IMMEDIATELY),
        &m_RTarget);


    m_fac->Release();
    m_fac = 0;


    if (m_Hwnd) {
        isCreated = true;
        isactive = true;
        return S_OK;
    }
    else {
        return S_FALSE;
    }
}

CuttingImage::CuttingImage()
{
    m_Hwnd = NULL;
    m_imgToCut = NULL;
    m_RTarget = NULL;
    isCreated = false;
    isactive = false;
}

void CuttingImage::render()
{
    m_RTarget->BeginDraw();
    m_RTarget->Clear(ColorF(1, 1, 1, 1.0f));
    m_RTarget->DrawBitmap(m_imgToCut, m_Rect);
    m_RTarget->EndDraw();
}

CuttingImage::~CuttingImage()
{
    m_RTarget->Release();
}
void CuttingImage::setImgToCut(ID2D1Bitmap* img)
{
    m_imgToCut = img;
    m_Rect.left = 0;
    m_Rect.top = 0;
    m_Rect.right = m_Rect.left + img->GetSize().width;
    m_Rect.bottom = m_Rect.top + img->GetSize().height;
}

问题在于,如果我在这个窗口上画一些东西,那就是它没有工作的子窗口之一。真的没有用,它根本没有清除窗口。不允许绘图。我认为位图有问题,所以我画线或矩形,它不起作用。

我有2个RenderTarget。一个是由mainwindowHWND制作的,最后一个是在上面的代码中。我用WIC编码图像,图像由mainwindowRenderTarget制作。这个类只需要图像指针。 请帮忙!

0 个答案:

没有答案