基於OpenCV的視頻圖像組態 (9):CEF瀏覽器初步

背景

之前,一直用WebBrowser,感覺網頁的應用需求夠用了。

直到Html5的出現,那麼炫的功能,WebBrowser卻展示不出來。就想有沒有什麼好的方法。不過一直沒騰出手來,其次需求也不是太強烈,就沒怎麼琢磨。

本週,接了航天與軍方兩個項目,需要做故障診斷,突然發現,如果把HTML5的功能結合進來,管理系統與圖形軟件整合,效果會很好。

趁着週末,鑽研了一下,居然OK了。

 

思路

先找支持HTML5的瀏覽器,花了一個小時,確定採用CEF(Chromium Embedded Framework),感覺比較靠譜。

靠譜的原因是有源碼。

如果有源碼還搞不定,那也就枉編這麼多年的代碼了。

下載並編譯,卻突然發現有個cef4delphi,直接裝上就OK了。

 

在 Delphi下很容易就調用成功了,但在CB下卻折騰好久,最終解決後卻發現,白折騰,把Dll/resource等放到目標目錄下即可。

 

幾點:

1) 初始化

    GlobalCEFApp = new TCefApplication;

    GlobalCEFApp->FrameworkDirPath = THelper::File::GetApplicationPath();

    GlobalCEFApp->ResourcesDirPath = GlobalCEFApp->FrameworkDirPath;

    GlobalCEFApp->LocalesDirPath = GlobalCEFApp->FrameworkDirPath +

        L"locales\\";

    GlobalCEFApp->Cache = GlobalCEFApp->FrameworkDirPath + L"cef\cache";

    GlobalCEFApp->Cookies = GlobalCEFApp->FrameworkDirPath + L"cef\cookies";

    GlobalCEFApp->UserDataPath = GlobalCEFApp->FrameworkDirPath +

        L"cef\User Data";

    GlobalCEFApp->EnableGPU = True; // Enable hardware acceleration

    GlobalCEFApp->MultiThreadedMessageLoop = false;

    GlobalCEFApp->FlashEnabled = false;

    GlobalCEFApp->FastUnload = true;

 

2) 創建瀏覽器

bool __fastcall NewBrowser(TWinControl * control) {

    if(Chromium) {

        int times = 0;

        while(!Chromium->CreateBrowser(control) && ++times < 5)

            THelper::Util::Delay(1000);

        if(times >= 5)

            return false;

        FBrowserParent = control;

        return true;

    }

    return false;

}

 

3) 載入URL

Chromium->LoadURL(url);

這樣就實現了CEF瀏覽器。後續再加入元素之間的交互功能。

演示效果

 

 

沒有對比就沒有傷害,看看這個瀏覽器(下圖左)與WebBrowser(下圖右)就知道了。

 

網上溜達的時候,發現一個求助帖:https://www.freelancer.hk/projects/CPlusPlus-Programming/Sample-VCL-application-with-embedded/

I need someone to write a sample VCL application in C++ Builder XE8 (or newer) aiming to demonstrate:

1. How to embed Chromium Embedded Framework (CEF - [url removed, login to view]) into an Win32 application.

2. How to navigate CEF component to a specified URL.

3. How to execute arbitrary (sample alert) Javascript within CEF component.

4. How to receive an event called before navigation (similar to OnBeforeNavigate2 in TCppWebBrowser).

5. How to receive an event called when page finishes loading (similar to OnDocumentComplete in TCppWebBrowser).

6. How to receive an event called when error occurs during page load (similar to OnNavigateError in TCppWebBrowser).

7. How to intercept popup windows (i.e. javascript alerts) called within CEF component HTML page.

The sample code should be compiled solely within C++ Builder IDE, no additional compilers should be involved.

The sample code should have comments regarding executed actions and intentions.

 

項目報價大概$400,可惜項目關閉了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章