3

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?

foobar
  • 2,887
  • 2
  • 30
  • 55
Nika_Rika
  • 613
  • 2
  • 6
  • 29
  • 1
    *"I created child window (dialog) end set it's parent the window of another process"* - There's your error. Don't. Ever. Don't even think about trying. You'll fail. Notepad is not prepared to share an input queue with your process. This is generally true for all applications. – IInspectable May 21 '15 at 08:50
  • I see. But I need to attach my window to another process' window, actually to move mine with the main window. I tried SetWinEventHook with EVENT_OBJECT_LOCATIONCHANGE, and catching this event redraw my window. But it hangs horribly. This issue made to hang the whole system... – Nika_Rika May 21 '15 at 09:07
  • You cannot safely attach your window to another process' window, unless that other process is controlled by you, and prepared to share an input queue. It will fail in many subtle ways, and eventually deadlock both your process and the target process. You need to look into another alternative. – IInspectable May 21 '15 at 09:20
  • Can this not be done if both the windows are children of a container window that you have full control of? – ACProctor Jan 11 '19 at 11:03

1 Answers1

2

With using the SetWindowPos method all works perfectly!

Nika_Rika
  • 613
  • 2
  • 6
  • 29
  • 1
    It's good SO etiquette to post the actual solution (including code snippets) rather than simply "I solved it!". Would help future people like myself looking for assistance :) – Nick Fisher Nov 02 '17 at 01:21