• CEF 右键添加开发者选项菜单项


    在项目开发过程中,有时候需要进行调试测试,然后我们可以在cef上下文菜单中添加自定义开发者工具菜单项,这样会比较方便,最后效果:

     实现过程:

    让自己的MyClientHandler来继承 CefContextMenuHandler这个抽象类,然对其下面的纯虚函数进行重写

    1.获得事件处理器

    virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() 
    {
      return this;
    }

    2. 重写CefContextMenuHandler 的方法

    virtual void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
            CefRefPtr<CefFrame> frame,
            CefRefPtr<CefContextMenuParams> params,
            CefRefPtr<CefMenuModel> model);
    
        virtual bool OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
            CefRefPtr<CefFrame> frame,
            CefRefPtr<CefContextMenuParams> params,
            int command_id,
            EventFlags event_flags);

    在MyClientHandler 类中添加 创建开发者工具窗口函数

    void ShowDevelopTools(CefRefPtr<CefBrowser> browser);

    实现:

    enum MyEnum
    {
        MENU_ID_USER_OPENLINK = MENU_ID_USER_FIRST + 200,
        MENU_ID_USER_SHOWDEVTOOLS,
    };
    
    void CCefBrowserEventHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model)
    {
        model->Remove(MENU_ID_PRINT);
        model->Remove(MENU_ID_VIEW_SOURCE);
    
        if ((params->GetTypeFlags() & (CM_TYPEFLAG_PAGE | CM_TYPEFLAG_FRAME)) != 0) {
            // Add a separator if the menu already has items.
            if (model->GetCount() > 0)
            {
                model->RemoveAt(2);
                model->Remove(MENU_ID_BACK);
                model->Remove(MENU_ID_FORWARD);
                //model->Clear();
                //model->AddSeparator();
    #ifdef _DEBUG
                model->AddItem(MENU_ID_USER_SHOWDEVTOOLS, L"开发者工具"); //"&Show DevTools");
    #else
    
    #endif
            }
        }
    }
    
    bool CCefBrowserEventHandler::OnContextMenuCommand(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, int command_id, EventFlags event_flags)
    {
        switch (command_id)
        {
            case MENU_ID_USER_SHOWDEVTOOLS:
            {
                ShowDevelopTools(browser);
                return true;
        }
        default:
            break;
        }
    
        return false;
    }
    
    void CCefBrowserEventHandler::ShowDevelopTools(CefRefPtr<CefBrowser> browser)
    {
        CefWindowInfo windowInfo; 
        CefBrowserSettings settings;
    
    #if defined(OS_WIN)
        //windowInfo.SetAsPopup(browser->GetHost()->GetWindowHandle(), "DevTools");
        windowInfo.SetAsPopup(NULL, "DevTools");
    
        //RECT rc = { 0, 0, 800, 600 };
        //windowInfo.SetAsChild(*theApp.m_pMainWnd, rc);
    #endif
    
        browser->GetHost()->ShowDevTools(windowInfo, this, settings, CefPoint()); 
    }

     参考:https://segmentfault.com/q/1010000013209473/a-1020000013211845

     参考:https://github.com/cztomczak/cef2go/issues/22

     参考:https://blog.csdn.net/markbruce/article/details/78846985

  • 相关阅读:
    base64 web前端js解码与转码
    react常见组件问题Can't perform a React state update on an unmounted component
    Git详细的多人开发心得
    数组中filter方法配合字符串方法trim使用
    Ant Design实现表格搜索、分页、多选处理
    Ant Design中日期选择器的disabledDate用法
    react学习资源汇总
    react开发项目到上线全过程详解
    MySQL-5.6.30-linux-glibc2.5-x86_64.tar.gz 安装教程
    linux下配置mysql主从复制配置
  • 原文地址:https://www.cnblogs.com/chechen/p/10251375.html
Copyright © 2020-2023  润新知