How to set/change top-level window for popup

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

How to set/change top-level window for popup

Postby lolovo » Sun Nov 12, 2017 1:08 pm

Hello!

Is it possible to set/change top-level window (i.e. using my delegate class) for popup before it will created? Is there a convenient way to realize it? I found only OnBeforePopup and I can try to create my own top-level window here and interrupt initial popup creation, but I don't know how cef will react for this trick...

Thanks in advance!
lolovo
Mentor
 
Posts: 67
Joined: Fri Aug 12, 2016 6:11 am

Re: How to set/change top-level window for popup

Postby magreenblatt » Sun Nov 12, 2017 6:25 pm

It is possible using OnBeforePopup. See cefclient for an example.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: How to set/change top-level window for popup

Postby hkurra » Mon Nov 13, 2017 3:01 am

On Windows platform, We are using onBeforePopup() to set our own main application window as the owner window of pop up
First get the "chromeWindow/HostWindow" from the browser instance as follow (this browser instance represent the browser from which window.open() request initiated)
Code: Select all
HWND chromeTabWindow = browser->GetHost()->GetWindowHandle();

In our case, we are also setting one parent window in which chrome window gets rendered so we also need to get its instance too but in your case, it could be your main application window.
Code: Select all
HWND myParentWindow = GetParent(chromeTabWindow);

Now, get the Main Application window as follow
Code: Select all
HWND myOwnerWindow = ::GetWindow(myParentWindow, GW_OWNER);

Now, pass owner window instance in SetAsPopup() method
Code: Select all
windowInfo.SetAsPopup(myOwnerWindow, target_frame_name);

Then we have some window position logic you can ignore it or change it as per your needs
Code: Select all
RECT rect = { 0,0,600,600 };
   if (GetWindowRect(myOwnerWindow, &rect)) {
      windowInfo.x = abs(abs(rect.right - rect.left) - (popupFeatures.width)) / 2 ;
      windowInfo.y = abs(abs(rect.bottom - rect.top) - (popupFeatures.height)) / 2;
   }
   if (popupFeatures.height > 0) {
      windowInfo.height = popupFeatures.height;
   }
   if (popupFeatures.width > 0) {
      windowInfo.width = popupFeatures.width;
   }
   if (popupFeatures.x > 0) {
      windowInfo.x = popupFeatures.x;
   }
   if (popupFeatures.y > 0) {
      windowInfo.y = popupFeatures.y;
   }

Now you need to disable the Main application window to make your pop up dialog as modal window(i.e no other window should get focus)
Code: Select all
EnableWindow(myOwnerWindow, FALSE);

Now, In "OnBeforeClose()" you need to enable your main application window if pop gets closed
Code: Select all
CefWindowHandle chromeTabWindow = browser->GetHost()->GetWindowHandle();
   CefWindowHandle ownerWinHandle = GetParentWindow(chromeTabWindow);

   if (browser->IsPopup()) {
      EnableWindow(ownerWinHandle, TRUE);
      SetForegroundWindow(ownerWinHandle);
          }
hkurra
Techie
 
Posts: 22
Joined: Wed Jun 21, 2017 8:13 am

Re: How to set/change top-level window for popup

Postby lolovo » Mon Nov 13, 2017 5:49 am

Thank you for your replies hkurra & Marshall !
lolovo
Mentor
 
Posts: 67
Joined: Fri Aug 12, 2016 6:11 am


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 71 guests