user1112008
user1112008

Reputation: 442

C++ creating window inside another

Is there any way(if is, please suggest something :) ) to create window inside another one that fills it and always be 100% width,height and stay always in same position as parent? In few words, create window that will act(size and movement) same as parent window. For now I have this:

hWnd = CreateWindowEx(WS_EX_TOOLWINDOW,L"Class", L"Title",WS_VISIBLE | WS_POPUP,
                      0, 0, 0, 0,hParent, NULL, GetModuleHandle(NULL), NULL );

and I'm checking WM_SIZE or WM_WINDOWPOSCHANGING for size change inside CallWndRetProc.

if(msg->message == WM_WINDOWPOSCHANGING && msg->hwnd == hParent){
    WINDOWPOS* pos = (WINDOWPOS*)msg->lParam;
    SetWindowPos(hWnd, 0, pos->x, pos->y, pos->cx, pos->cy, SWP_NOACTIVATE);
}

But there's problem, pos has coordinates relative to the hParents window and SetWindowPos sets position relative to the whole screen. Maybe there is easier way to hold hWnd window on same position as hParent?

Upvotes: 1

Views: 2226

Answers (3)

serup
serup

Reputation: 3830

Perhaps you could use the parent windows hWnd or Device Context to overtake writing in that window instead of creating a new window

I had a similar case where existing code was to be drawn inside a tab pane and what I did was to fetch the hWnd from the device context like this

example (the class OscilloScope is the class creating its own window) :

static bool bGridVisible = false;
HWND thisWindow = m_wndTabs.GetWindowDC()->GetWindow()->m_hWnd;

RECT rect;
GetWindowRect(&rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;

// draw the grid window
auto function = [&]() {
    bGridVisible = true;
    OscilloScope demo;
    demo.Construct(width, height, 2, 2, false, thisWindow);
    demo.Start();
    bGridVisible = false;
};


if (!bGridVisible) {
    std::thread t([&]() {
        function();
    });

    t.detach();
}

Inside the class OscilloScope I then removed the code for creating a window and passed on the "thisWindow" instead.

example output ( basic MFC project ) : enter image description here

Still it is a little problematic getting OnPaint or OnKey events propagated to the underlying class

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308432

You can use MapWindowPoints to convert a rectangle from one set of coordinates to another. To go from the parent client space to the screen space use the following:

MapWindowPoints(hParent, NULL, &rect, 2);

You also might find it easier to use MoveWindow rather than SetWindowPos.

Upvotes: 0

Attila
Attila

Reputation: 28772

You can use GetWindowInfo() to query the current screen position of the window. Using this, you can calculate the desired screen position from the relative coordinates

Upvotes: 1

Related Questions