// CefContextMenuHandler methods
void BrowserHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model) {
REQUIRE_UI_THREAD();
// CM_TYPEFLAG_MEDIA 包含图片、视频、文件等等
// 可以再进行细化判断:params->GetMediaType() & CM_MEDIATYPE_IMAGE != 0
if ((params->GetTypeFlags() & CM_TYPEFLAG_MEDIA) != 0 && (params->GetMediaType() & CM_MEDIATYPE_IMAGE != 0)) {
if (model->GetCount() > 0) {
// 禁止右键菜单
model->Clear();
}
model->InsertItemAt(0, CLIENT_ID_COPY_IMAGE, L"复制图片");
model->InsertItemAt(1, CLIENT_ID_SAVE_IMAGE_AS, L"图片另存为...");
model->InsertSeparatorAt(2);
model->AddItem(MENU_ID_PRINT, L"打印");
}
}
// 处理点击事件
bool BrowserHandler::OnContextMenuCommand(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, int command_id, EventFlags event_flags) {
// 复制图片到剪切板
if (command_id == CLIENT_ID_COPY_IMAGE) {
frame->Copy();
} else if (command_id == CLIENT_ID_SAVE_IMAGE_AS) {
// 存储图片
frame->GetBrowser().get()->GetHost().get()->StartDownload(frame->GetURL());
}
if (handle_delegate_)
return handle_delegate_->OnContextMenuCommand(browser, frame, params, command_id, event_flags);
else
return false;
}
转载于:libcef3——自定义右键菜单,复制图片和图片另存为_Go和分布式IM的博客-CSDN博客
CEF 文件下载功能实现 – My Code