Page 1 of 1

Embedding Chromium in MFC application

PostPosted: Mon Oct 12, 2009 9:31 am
by StraySod
Hi,

I tried to embed Chromium using the Cef library into basic MFC SDI application. It doesn't work and I'm not sure what I'm doing wrong. I'm new to embedding chromium. What I have done until now:

1. I call CefInitialize(...) in Main Frame constructor:

Code: Select all
CMainFrame::CMainFrame()
{
   // TODO: add member initialization code here
   CefInitialize(false, std::wstring());
}


2. I try to create a browser in overriden OnInitialUpdate method of my CView derived class:

Code: Select all
void Ccef_testView::OnInitialUpdate()
{
   g_handler = new ClientHandler();//this is a global object declared like this: CefRefPtr<CefHandler> g_handler; ClientHandler is my implementation of CefHandler returning RV_CONTINUE in every method
   CefWindowInfo info;
   HWND hWnd = this->GetSafeHwnd();
   RECT rect;

   this->GetClientRect(&rect);
   info.SetAsChild(hWnd, rect);

   CefBrowser::CreateBrowser(info, false, static_cast<CefRefPtr<CefHandler>>(g_handler), _T("http://www.google.com"));//I assume now should I see the browser, but nothing happens
}


3. In Main Frames destructor I call CefShutdown():

Code: Select all
CMainFrame::~CMainFrame()
{
   CefShutdown();
}


according to what the debugger says it should work, browser coordinates and dimensions are well set. Probably I'm missing something. Please let me know if you had any suggestions. I'm using winXP pro and building in Visual Studio 2008. I set all linker dependencies like cefclient example has, copied needed dlls into applications root directory and included cef.h. Thank you for your suggestions.

Best regards,
Martin

Re: Embedding Chromium in MFC application

PostPosted: Tue Oct 13, 2009 9:17 am
by magreenblatt
Hi Martin,

Your problem is that the CEF message loop is not being run. The best way to accomplish this with MFC is to initialize CEF with a multi-threaded message loop by setting the first parameter of CefInitialize() to true.

Code: Select all
CefInitialize(true, std::wstring());

The other option is to initialize CEF with a single-threaded message loop as you are doing currently and then call CefDoMessageLoopWork() regularly to perform the CEF message loop processing. This approach can be complex with an MFC-based application because there's no direct way (that I've been able to identify) to make additional function calls as part of the MFC message loop.

Regards,
Marshall

Re: Embedding Chromium in MFC application

PostPosted: Tue Oct 13, 2009 11:52 am
by StraySod
Thank you very much Marshall. It's working as expected now.

Regards,
Martin

Re: Embedding Chromium in MFC application

PostPosted: Thu Oct 15, 2009 8:19 am
by renet
Thanks for posting this question and thanks for the answer as I was about to try embedding chrome in an MFC app myself. Nice work on this library! Its great to have an alternative to embedding IE or Mozilla.

Re: Embedding Chromium in MFC application

PostPosted: Thu Oct 15, 2009 2:34 pm
by renet
One other question. I was going to try to embed Chrome in an MFC application built using Visual Studio 2008 using MFC as a shared dll. Was this your general configuration or did you use VS2005? Perusing the forums and the issues list it seems as though some folks have had trouble using CEF with Visual Studio 2008, has anyone tried integrating CEF with an MFC application using MFC as a shared dll. I realize I might have to build Chromium and CEF using the instructions provided on their respective pages but other than that has anyone run into any major issues?

Thank you in advance,

Rene

Re: Embedding Chromium in MFC application

PostPosted: Thu Oct 15, 2009 2:58 pm
by magreenblatt
Hi Rene,

The VS2008 troubles should be a thing of the past now that the CEF configuration is GYP-based. Follow the instructions to generate the VS2008 project files and then edit the libcef_dll_wrapper project to have the same properties as your application ("Use of MFC", etc). Other than that you should be fine.

Regards,
Marshall

Re: Embedding Chromium in MFC application

PostPosted: Fri Oct 16, 2009 9:41 am
by renet
Marshall,

Thank you for the suggestion. It worked like a charm! If you would like we'd be happy to write up something about how we used CEF in a sort of CEF & MFC 101 and send it to you for use on the wiki if the write-up is appropriate and accurate. And whatever wrapper classes we end up creating (if any at all - we'll probably write up an MFC and a .Net wrapper class), we'll of course forward the code onto this project for use and improvement by others.

Thank you again,

Rene

Re: Embedding Chromium in MFC application

PostPosted: Fri Oct 16, 2009 9:49 am
by magreenblatt
If you would like we'd be happy to write up something about how we used CEF in a sort of CEF & MFC 101 and send it to you for use on the wiki if the write-up is appropriate and accurate. And whatever wrapper classes we end up creating (if any at all - we'll probably write up an MFC and a .Net wrapper class), we'll of course forward the code onto this project for use and improvement by others.


That would be great, thanks :-).

Re: Embedding Chromium in MFC application

PostPosted: Sat Oct 31, 2009 11:21 am
by jerryevans
Hello Rene

Any chance of sharing the VS2008 binaries? I was going to do the same last month but got sidetracked.

Many thanks

Jerry

Re: Embedding Chromium in MFC application

PostPosted: Wed May 26, 2010 6:59 pm
by rlscef
Did this (writing up a mini "CEF & MFC 101") ever happen? I'm also trying to get CEF working within an MFC application, and am having some difficulty.

I initially embedded a browser window using the Apple Webkit port. It worked almost perfectly, but the licensing restrictions preclude its use for almost any commercial purpose, so now I'm trying to get CEF going.

The browser window comes up nicely, but if I cover the application window with another window, and then flip back, the browser briefly is painted and then blanks out.

...and there are other problems, e.g., trying to remove browser windows doesn't work for me.