c++mfcchildwindow

the disappearance of the child window when when the parent window redrawn


I created child window (dialog) end set it's parent the window of another process (Notepad for example) by its handle.

HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
    PROCESS_VM_READ,
    FALSE, processID );

if (NULL != hProcess )
{
    HWND hw;
    hw = find_main_window(processID); //some function of getting win handle through process ID
}

................

CMyHud *mhDlg = new CMyHud();
CWnd* pWnd = CWnd::FromHandle(hw);


//if(mhDlg->m_hWnd != 0) 
if (!mhDlg->GetSafeHwnd())
{
    if (mhDlg != NULL)
    {
        ret = mhDlg->Create(IDD_DIALOG1, pWnd);
    }

    if (!ret)   //Create failed.
    {
        AfxMessageBox(_T("Error creating Dialog"));
        return FALSE;
    } 
}

Then I set styles for parent and child windows

LONG t = GetWindowLong(hw,GWL_STYLE) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
SetWindowLong(hw,GWL_STYLE,t);

LONG t1 = GetWindowLong(mhDlg->m_hWnd,GWL_STYLE) | WS_CLIPSIBLINGS | WS_OVERLAPPED;
SetWindowLong(mhDlg->m_hWnd,GWL_STYLE,t1);
::BringWindowToTop(mhDlg->m_hWnd);
mhDlg->ShowWindow(SW_SHOW);

The child window appears in the client area of the parent window (Notepad).

Good.

BUT! It's disappears when I set focus on parent window. Well. Physically it's still there, but its background merges with parent's window background, and it seems like the child window is gone.

When you find child window and set focus on it, it's appearing again. But it is redrawing bad, still having a parts of parent's window background (look at the picture). image is here

What have i done wrong?? What should I do for appearing child window over the parent ALWAYS, regardless of redrawing the parent window?


Solution

  • With using the SetWindowPos method all works perfectly!