[SOLVED?] Win32 parent app loses focus after CEF Shutdown

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.

[SOLVED?] Win32 parent app loses focus after CEF Shutdown

Postby ldamis » Mon Jul 25, 2022 10:07 am

Hi.

My CEF build (windows 32 bit) drive the browser inside a parent window created by another Win32 application.

This is how I create browser window:
Code: Select all
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = AppManager::MainBrowserWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = _hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL; //MAKEINTRESOURCE(IDC_MXWEBAPP);
wcex.lpszClassName = L"MyAppWindow";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

_hwnd_browser_window = CreateWindow(L"MyApp", L"MyApp", WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, 0, 0, _hWndParent, NULL, _hInstance, NULL);


And this is how I create browser:
Code: Select all
_cef_handler = new MyCefHandler(debug);
SetWindowPos(_hwnd_browser_window, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

GetClientRect(_hwnd_browser_window, &rect);
windowBounds->Set(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
info.SetAsChild(_hwnd_browser_window, *windowBounds);

CefBrowserHost::CreateBrowser(info, _cef_handler.get(), url, browser_settings, extra_info, nullptr);



I am updating CEF from a very old version (73.1.11+ge6986dc) to version 103.0.11+gb1e93e1, but I've experienced a focus problem (I've experienced the same problem months ago trying to update to CEF version ~93): parent window loses focus when I close browser from a keyboard command.

With the actual version (~73), after CefShutdown, the focus always remain to parent application.
After CEF update, when I close using a command triggered from keyboard, the main application loses focus and another opened application comes on front taking the focus.
It does not happens whet I hit the custom close button with a mouse click (the custom close button lives in the host application).
I manage the browser' close in the same way as I manage it when I close with a keyboard command.

I cannot reproduce this behaviour whit ceflient because it is a standalone application and create windows not as child of another aplpication.

Sorry for my english; I hope I've been clear explaining it all... :?

Anyone can help me telling me what can I do or what can I investigate to try to solve my problem?

Thank you in advance.
Last edited by ldamis on Thu Sep 15, 2022 8:22 am, edited 1 time in total.
ldamis
Techie
 
Posts: 33
Joined: Fri Jan 19, 2018 3:36 am

Re: Win32 parent application loses focus after CEF Shutdown

Postby HarmlessDave » Wed Jul 27, 2022 1:00 pm

Is your code running shutdown, or does it happen automatically when the user closes the browser?

If it is your code, you could use Win32 SetFocus() to move the focus to the parent before calling shutdown.

If the user is closing the browser, there are events during shutdown. You can add the Win32 SetFocus() there. This would also work for when you call shutdown in your code.
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Win32 parent application loses focus after CEF Shutdown

Postby ldamis » Fri Jul 29, 2022 4:03 am

HarmlessDave wrote:Is your code running shutdown, or does it happen automatically when the user closes the browser?

Yes it is.

HarmlessDave wrote:If it is your code, you could use Win32 SetFocus() to move the focus to the parent before calling shutdown.

If the user is closing the browser, there are events during shutdown. You can add the Win32 SetFocus() there. This would also work for when you call shutdown in your code.


Thank you for the suggestion; I've already tried, but it didn't work.
I have only reference of host window that is destroyd as soon as that my application closes and maybe is not enough for parent application to get focus.

I keep investigating this behaviour and I will try to find out what I do wrong.
ldamis
Techie
 
Posts: 33
Joined: Fri Jan 19, 2018 3:36 am

Re: Win32 parent application loses focus after CEF Shutdown

Postby HarmlessDave » Sun Jul 31, 2022 8:19 pm

You might need to delay your own SetFocus until after some or all of the CEF + Chromium code has run, in case that code causes focus changes.

That might mean doing the SetFocus() in one of the later CEF closing events, or possibly using a timer interrupt to give it time to finish.

You might also be calling SetFocus() with the wrong HWND. Normally the browser's direct parent is another CEF window, not one of your program's windows.

Adding logging using your own framework not CEF logging might help.
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Win32 parent application loses focus after CEF Shutdown

Postby ldamis » Mon Aug 01, 2022 10:34 am

HarmlessDave wrote:You might need to delay your own SetFocus until after some or all of the CEF + Chromium code has run, in case that code causes focus changes.

I've put SetFocus() after CEFShutdown, before the end of WinMain()

HarmlessDave wrote:That might mean doing the SetFocus() in one of the later CEF closing events, or possibly using a timer interrupt to give it time to finish.

OK, I will try ASAP to delay SetFocus() after CEFShutdown.

HarmlessDave wrote:You might also be calling SetFocus() with the wrong HWND. Normally the browser's direct parent is another CEF window, not one of your program's windows.

I've tried to set focus on parent window created and owned by parent application.
But I think I need more collaboration by the parent application, maybe sending request in order to self-focus...

HarmlessDave wrote:Adding logging using your own framework not CEF logging might help.

Already did ;)


After investigating, I've found a difference in behviour between older and newer version of CEF: my cef application with newer CEF, receives 2 times WM_SETFOCUS during CEFShutown.
I've tried both to manage it and let DefWindowProc to execute, but it does not effect at all in my "losing focus" problem.
ldamis
Techie
 
Posts: 33
Joined: Fri Jan 19, 2018 3:36 am

Re: Win32 parent application loses focus after CEF Shutdown

Postby ndesktop » Tue Aug 02, 2022 2:08 am

Maybe you can do an OpenProcess from the parent app, wait for it, and when the handle is signaled, then handle the focus?
ndesktop
Master
 
Posts: 754
Joined: Thu Dec 03, 2015 10:10 am

Re: Win32 parent application loses focus after CEF Shutdown

Postby ldamis » Fri Aug 05, 2022 4:52 am

ndesktop wrote:Maybe you can do an OpenProcess from the parent app, wait for it, and when the handle is signaled, then handle the focus?


Thanks for suggestion.
I've already considered this as my last chance in case I won't solve problem in the CEF child app.

For now I still trying to understand why my app changes its close' behaviour with CEF update.
ldamis
Techie
 
Posts: 33
Joined: Fri Jan 19, 2018 3:36 am

Re: Win32 parent application loses focus after CEF Shutdown

Postby ldamis » Thu Sep 15, 2022 8:21 am

An update about my problem.

I've stopped investigation due to other duties.
Later on I've update my Win32 CEF Application from 103.0.11+gb1e93e1 to CEF_104.4.25+gd80d467.
With this update, the problem disappeared without changing anything.

As I said, the focus problem was detected with CEF from 93 till 103.
With this update, everything works fine when closing child CEF application: parent application never loses focus on CEF close.

This is a good news to me.
I am only asking what could be changed in CEF close procedure from version ~93 to version ~104... :?:

I will update soon to last CEF version (105.*) hoping not to face the issue again.

Thanks everybody for suggestions you gave to me.

Have a nice day ;)
ldamis
Techie
 
Posts: 33
Joined: Fri Jan 19, 2018 3:36 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 27 guests