How to disable right click in CEF simple?

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 disable right click in CEF simple?

Postby ysaliens » Tue Jan 16, 2018 2:26 pm

Same as title, how can the right click menus be disabled in the CEF simple example?
ysaliens
Techie
 
Posts: 20
Joined: Mon Oct 02, 2017 1:08 pm

Re: How to disable right click in CEF simple?

Postby Czarek » Tue Jan 16, 2018 4:03 pm

Implement CefContextMenuHandler and use model to clear the menu. See cefclient for an example.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: How to disable right click in CEF simple?

Postby ysaliens » Tue Jan 16, 2018 4:54 pm

Got it working. Changes below for anyone interested. This is with cef simple app referenced in the example CEF tutorials.

simple_handler.h, changes are commented
Code: Select all
class SimpleHandler : public CefClient,
                 public CefContextMenuHandler, //Add to allow overwrite of context menu functions
                      public CefDisplayHandler,
                      public CefLifeSpanHandler,
                      public CefLoadHandler {
 public:
  explicit SimpleHandler(bool use_views);
  ~SimpleHandler();

  // Provide access to the single global instance of this object.
  static SimpleHandler* GetInstance();

  // Allow overwrite of context menu functions
  // CefClient methods:
  virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() OVERRIDE {
    return this;
  }
  virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE {
    return this;
  }
  virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE {
    return this;
  }
  virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE {
    return this;
  }

  // Headers for methods we will overwrite
  // CefContextMenuHandler methods
  void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
     CefRefPtr<CefFrame> frame,
     CefRefPtr<CefContextMenuParams> params,
     CefRefPtr<CefMenuModel> model) OVERRIDE;
  bool OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
     CefRefPtr<CefFrame> frame,
     CefRefPtr<CefContextMenuParams> params,
     int command_id,
     EventFlags event_flags) OVERRIDE;

  // CefDisplayHandler methods:
  virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
                             const CefString& title) OVERRIDE;


In simple_handler.cc add the following:
Code: Select all
//Disable context menu
//Define below two functions to essentially do nothing, overwriting defaults
//See change in simple_handler.h
void SimpleHandler::OnBeforeContextMenu(
   CefRefPtr<CefBrowser> browser,
   CefRefPtr<CefFrame> frame,
   CefRefPtr<CefContextMenuParams> params,
   CefRefPtr<CefMenuModel> model) {
   CEF_REQUIRE_UI_THREAD();

   model->Clear();
}

bool SimpleHandler::OnContextMenuCommand(
   CefRefPtr<CefBrowser> browser,
   CefRefPtr<CefFrame> frame,
   CefRefPtr<CefContextMenuParams> params,
   int command_id,
   EventFlags event_flags) {
   CEF_REQUIRE_UI_THREAD();

   MessageBox(browser->GetHost()->GetWindowHandle(), L"The requested action is not supported", L"Unsupported Action", MB_OK | MB_ICONINFORMATION);
   return false;
}
ysaliens
Techie
 
Posts: 20
Joined: Mon Oct 02, 2017 1:08 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 34 guests