# Drawing on an existing window

## Recommended Posts

Nonameo    122
Hi guys. You probably get this a lot but all the examples i've found are very misleading. I dont know much (at all) about directx etc.. so please bear with me. What i'm trying to do: I want to draw a red square onto an existing window. Say for example, i have it's hWnd, i then want to render the square onto that window without the WM_PAINT method of the window erasing my square (thus my square will not flicker). Is this possible? Can anyone provide an example (or point me to one) that i can work with to produce this? Thanks a lot -Nonameo-

##### Share on other sites
gdunbar    2198
I'm doing exactly this, though in MFC, and it's actually pretty straightforward. The big gotcha for me was to override the WM_ERASEBKGND message to do nothing; the default window proc will erase the whole window for you, which is likely to cause flickering.

My rudimentary creation code is something like this:

    CRect rect;    GetClientRect( &rect );    HWND hWnd = GetSafeHwnd();    m_pD3D = Direct3DCreate9( D3D_SDK_VERSION );    if( NULL == m_pD3D )    {        AfxMessageBox( "Failed to init D3D\n" );        return;    }    ZeroMemory( &m_d3dpp, sizeof(m_d3dpp) );    m_d3dpp.Windowed = TRUE;    m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    m_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;    m_d3dpp.EnableAutoDepthStencil = TRUE;    m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;    // Create the D3DDevice    if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,                                      &m_d3dpp, &m_pd3dDevice ) ) )    {        AfxMessageBox( "Failed to init D3D\n" );        return;    }

Good luck!
Geoff