protected override bool OnContextMenuCommand(CefBrowser browser, CefFrame frame, CefContextMenuParams state, int commandId, CefEventFlags eventFlags) { switch (commandId) { case 134: Clipboard.SetText(state.LinkUrl); return(true); case 133: //在新窗口中打开这个链接 if (state.LinkUrl != null) { linkrul = state.LinkUrl; } else if (IsUrl(state.SelectionText)) { linkrul = state.SelectionText; } MainForm.Instance.Invoke(new Action(() => { MainForm.Instance.AddNewBrowserTab(linkrul); })); linkrul = ""; GC.Collect(); return(true); case 135: //在新窗口中打开链接并搜索选中的内容 MainForm.Instance.Invoke(new Action(() => { FATabStripItem fATab = new FATabStripItem(); fATab.TabIndex = MainForm.Instance.faTabStrip1.SelectedItem.TabIndex + 1; MainForm.Instance.faTabStrip1.InsetTab(fATab, true, fATab.TabIndex); MainForm.Instance.PubSeach(MainForm.Instance.faTabStrip1.SelectedItem, SearStr); fATab = null; GC.Collect(); })); return(true); case 136: //图片另存为 SaveFileDialog savedialog = new SaveFileDialog(); savedialog.Filter = "Jpg 图片|*.jpg|Bmp 图片|*.bmp|Gif 图片|*.gif|Png 图片|*.png|Wmf 图片|*.wmf"; savedialog.FilterIndex = 0; savedialog.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录 savedialog.CheckPathExists = true; //检查目录 savedialog.FileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "-"; //设置默认文件名 if (savedialog.ShowDialog() == DialogResult.OK) { browser.GetHost().DownloadImage(state.SourceUrl, false, 0, false, new CefWebDownloadImageCallbackHandler(savedialog.FileName, -1, null, "")); //WriteBytesToFile(savedialog.FileName, GetBytesFromUrl(state.SourceUrl)); //Image.Save(, System.Drawing.Imaging.ImageFormat.Jpeg);// image为要保存的图片 MessageBox.Show("图片保存成功!", "信息提示"); } savedialog = null; GC.Collect(); return(true); case 100: Clipboard.SetText(state.SelectionText); return(true); case 114: frame.Paste(); return(true); case 113: browser.StopLoad(); return(true); case 200: browser.GoBack(); return(true); case 300: browser.GoForward(); return(true); case 350: //刷新 PublicClass.ReflashBrowser(browser, _core); return(true); case 400: //browser; //var host = browser.GetHost(); //var wi = CefWindowInfo.Create(); //wi.SetAsPopup(IntPtr.Zero, "DevTools"); //host.ShowDevTools(wi, new DevToolsWebClient(), new CefBrowserSettings(), new CefPoint(0, 0)); PublicClass.DevTools(_core, browser); return(true); case 401: SaveFileDialog pdfsave = new SaveFileDialog(); pdfsave.Filter = "PDF文件|*.pdf"; pdfsave.FilterIndex = 0; pdfsave.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录 pdfsave.CheckPathExists = true; //检查目录 pdfsave.FileName = _core.Title; // System.DateTime.Now.ToString("yyyyMMddHHmmss") + "-"; ;//设置默认文件名 if (pdfsave.ShowDialog() == DialogResult.OK) { _core.Browser.GetHost().PrintToPdf(pdfsave.FileName, new CefPdfPrintSettings(), new CefWebPdfPrinerHandler()); } pdfsave = null; return(true); case 402: _core.Browser.GetHost().Print(); //print web page return(true); default: // Allow default handling, if any. return(false); } }
protected override void OnDownloadUpdated(CefBrowser browser, CefDownloadItem downloadItem, CefDownloadItemCallback callback) { ////base.OnDownloadUpdated(browser, downloadItem, callback); //PublicClass.DownloadProgress(browser, callback, downloadItem);// downloadItem.Url.Substring(downloadItem.Url.LastIndexOf("/") + 1, downloadItem.Url.Length - downloadItem.Url.LastIndexOf("/"))); ////if (downloadItem.IsComplete) ////{ //// // MessageBox.Show("下载成功!"); //// //if (browser.IsPopup && !browser.HasDocument) //// //{ //// if (downloadItem.IsComplete) //// browser.GetHost().CloseBrowser(); //// //} ////} CefDownloadItem cefDownloadItem = downloadItem; //long reciv = downloadItem.ReceivedBytes; try { uint ID = downloadItem.Id; int dPercentComplete = downloadItem.PercentComplete; if (cefDownloadItem.IsInProgress) { CefConstHelper.IsDownloading = true; if (MainForm.Instance.DownloadPanel3.Controls.Count > 2) { MainForm.Instance.Invoke(new Action(() => { for (int i = 0; i < MainForm.Instance.faTabStrip1.Items.Count; i++) { if (MainForm.Instance.faTabStrip1.Items[i].Title == CefConstHelper.CefDownloadTitle) { for (int k = 1; i < MainForm.Instance.faTabStrip1.Items[i].splic.Panel1.Controls.Count; k++) { if (k == MainForm.Instance.faTabStrip1.Items[i].splic.Panel1.Controls.Count) { break; } Jdownload mjdownload = ((Jdownload)MainForm.Instance.faTabStrip1.Items[i].splic.Panel1.Controls[k]); if (mjdownload.IsDownloading && ID.ToString() == mjdownload.DownloadID) { mjdownload.pictureBox1.Image = null; mjdownload.label1.Text = dPercentComplete.ToString() + "%"; // sender.FinishedRate.ToString() + "%"; // break; } } break; } } for (int y = 0; y < MainForm.Instance.DownloadPanel3.Controls.Count - 2; y++) { Jdownloading yDown = ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[y + 2])); if (yDown.DownloadIndex == ID.ToString()) { ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[y + 2])).label2.Text = dPercentComplete.ToString() + "%"; // sender.FinishedRate.ToString() + "%"; if (dPercentComplete < 0) { ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[y + 2])).label2.Text = (cefDownloadItem.ReceivedBytes / 1024000f).ToString("0.00") + "MB"; // sender.FinishedRate.ToString() + "%"; } if (cefDownloadItem.PercentComplete <= 0 && cefDownloadItem.ReceivedBytes == 0 && cefDownloadItem.TotalBytes > 0) { ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[y + 2])).label2.Text = "100%"; } if (((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[y + 2])).CancelDowning) { callback.Cancel();//取消下载 ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[y + 2])).label2.ForeColor = Color.Red; ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[y + 2])).label2.Text = "停止"; ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[y + 2])).CancelDown.Enabled = false; } } } })); } } //当下载完成时 if (cefDownloadItem.IsComplete) { MainForm.Instance.Invoke(new Action(() => { bool downloadTabOpened = false; for (int i = 0; i < MainForm.Instance.faTabStrip1.Items.Count; i++) { if (MainForm.Instance.faTabStrip1.Items[i].Title == CefConstHelper.CefDownloadTitle) { for (int k = 1; i < MainForm.Instance.faTabStrip1.Items[i].splic.Panel1.Controls.Count - 1; k++) { if (k == MainForm.Instance.faTabStrip1.Items[i].splic.Panel1.Controls.Count) { break; } Jdownload jdownloadR = (Jdownload)(MainForm.Instance.faTabStrip1.Items[i].splic.Panel1.Controls[k]); if (jdownloadR.DownloadID == ID.ToString() && jdownloadR.IsDownloading) { jdownloadR.label1.Text = ""; jdownloadR.label1.Hide(); jdownloadR.ImageBase64str = PublicClass.IamgeToBase64(System.Drawing.Icon.ExtractAssociatedIcon(jdownloadR.FullFilePaths).ToBitmap()); jdownloadR.FileAlreadyDele = false; jdownloadR.IsDownloading = false; jdownloadR.pictureBox1.Image = System.Drawing.Icon.ExtractAssociatedIcon(jdownloadR.FullFilePaths).ToBitmap(); jdownloadR.Invalidate(); mDownloadRecode.Update(jdownloadR); downloadTabOpened = true; } } break; } } if (MainForm.Instance.DownloadPanel3.Visible) { for (int h = 0; h < MainForm.Instance.DownloadPanel3.Controls.Count - 2; h++) { if (((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[h + 2])).DownloadIndex == ID.ToString()) { ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[h + 2])).buttonXP1.ImageDefault = Resources.up1; ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[h + 2])).label2.Visible = false; ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[h + 2])).pictureBox1.Image = Icon.ExtractAssociatedIcon(((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[h + 2])).FilePaths).ToBitmap();; ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[h + 2])).IsDownloaded = true; ((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[h + 2])).Opendownfile.Enabled = true; MainForm.Instance.DownloadPanel3.Invalidate(); if (!downloadTabOpened) //当没有打开下载页面的时候也要更新一部分内容 { string jsonStr = File.ReadAllText(PublicClass.currDirectiory + @"\UserData\mDownloadRecodes"); Jdownload downitem = new Jdownload();// = new FavirteButton(); List <Jdownload> downitemBTs; try { var jobInfoList = JsonConvert.DeserializeObject <List <Jdownload> >(jsonStr); if (jobInfoList.Count > 0) { downitemBTs = jobInfoList; foreach (Jdownload jobInfo in downitemBTs) { if (jobInfo.DownloadID == ID.ToString() && jobInfo.IsDownloading) { jobInfo.ImageBase64str = PublicClass.IamgeToBase64(((Jdownloading)(MainForm.Instance.DownloadPanel3.Controls[h + 2])).pictureBox1.Image); downitem = jobInfo; break; } } mDownloadRecode.Update(downitem); } } catch (Exception e) { MessageBox.Show(e.Message); } } break; } } } })); if (downloadItem.IsComplete) { CefConstHelper.IsDownloading = false; } } } catch (Exception e) { } }
protected override void OnBeforeDownload(CefBrowser browser, CefDownloadItem downloadItem, string suggestedName, CefBeforeDownloadCallback callback) { string DownloadID = downloadItem.Id.ToString(); //base.OnBeforeDownload(browser, downloadItem, suggestedName, callback); if (suggestedName.Contains("@")) { System.Windows.Forms.MessageBox.Show(CefConstHelper.CefDownLoadWarling, "CefiBrowser", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); return; } string JsuggestedName = suggestedName.Substring(0, suggestedName.LastIndexOf(".")); string GsuggestedName = JsuggestedName; string JsuggestedEndName = suggestedName.Substring(suggestedName.LastIndexOf("."), suggestedName.Length - suggestedName.LastIndexOf(".")); string Jfilepath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\downloads"; Jdownloading dwitem = new Jdownloading(); Jdownload WrDownloadListItem = new Jdownload(); MainForm.Instance.Invoke(new Action(() => { MainForm.Instance.faTabStrip1.SelectedItem.BrowserIsLoading = false; //下载的时候不显示Gif动画 if (MainForm.Instance.DownloadPanel3.Visible == false) { MainForm.Instance.DownloadPanel3.Visible = true; MainForm.Instance.faTabStrip1.Height = MainForm.Instance.Height - MainForm.Instance.DownloadPanel3.Height;//修正下载时进度控件位置错误 } for (int i = 0; i < 1000; i++) { if (CheckFileExists(Jfilepath + "\\" + JsuggestedName + JsuggestedEndName) != true) { if (CheckFileExists(Jfilepath + "\\" + JsuggestedName + JsuggestedEndName + ".cfg") != true || CheckFileExists(Jfilepath + "\\" + JsuggestedName + JsuggestedEndName + ".tfg") != true) { break; } else { JsuggestedName = GsuggestedName + "(" + (i + 1) + ")"; } } else { JsuggestedName = GsuggestedName + "(" + (i + 1) + ")"; } } dwitem.label1.Font = Label1Font; dwitem.label2.Font = Label2Font; dwitem.label1.ForeColor = Color.DimGray; dwitem.label1.Text = JsuggestedName + JsuggestedEndName; dwitem.FileName = JsuggestedName + JsuggestedEndName; dwitem.label2.Text = downloadItem.PercentComplete + "%"; //dwitem.Startime = downloadItem.StartTime.ToString().ToLower().Trim(); dwitem.Height = 36; //dwitem.DownloadIndex = downloadItem.Id; dwitem.FilePaths = Jfilepath + "\\" + JsuggestedName + JsuggestedEndName; dwitem.DownloadIndex = DownloadID; dwitem.IsDownloaded = false; dwitem.Location = new Point((MainForm.Instance.DownloadPanel3.Controls.Count - 2) * dwitem.Width + 6, 2); MainForm.Instance.DownloadPanel3.Controls.Add(dwitem); if (MainForm.Instance.faTabStrip1.SelectedItem.Title == "加载中...") { //MainForm.Instance.faTabStrip1.SelectedItem.Title = "下载..."; //MainForm.Instance.faTabStrip1.SelectedItem.ItemIcon = Resources.AppIconNormal; MainForm.Instance.faTabStrip1.RemoveTab(MainForm.Instance.faTabStrip1.SelectedItem); } })); callback.Continue(Jfilepath + "\\" + JsuggestedName + JsuggestedEndName, false); //这里true就是显示弹框,如果为False就不用弹 //PublicClass.mDownload( downloadItem.Url, Jfilepath , JsuggestedName + JsuggestedEndName,(int)dwitem.DownloadIndex); //callback.Dispose(); ////写入下载记录表中 WrDownloadListItem.DownLoadTime = DateTime.Now.TimeOfDay.ToString(); WrDownloadListItem.FileName = JsuggestedName + JsuggestedEndName; WrDownloadListItem.FullFilePaths = dwitem.FilePaths; WrDownloadListItem.DownloadUrl = downloadItem.Url; WrDownloadListItem.DownloadID = DownloadID; //WrDownloadListItem.Startime = downloadItem.StartTime.ToString().ToLower().Trim(); WrDownloadListItem.IsDownloading = true; WrDownloadListItem.FileAlreadyDele = false; WrDownloadListItem.ImageBase64str = PublicClass.IamgeToBase64(Resources.AppIconNormal); //要修改这个默认的APP图标 WrDownloadListItem.Width = MainForm.Instance.faTabStrip1.SelectedItem.Width / 3; mDownloadRecode.WritemDownloadRecode(WrDownloadListItem); //判断下载Tab是否打开 bool DownloadTabIsOpen = false; FATabStripItem fATab = new FATabStripItem(); MainForm.Instance.Invoke(new Action(() => { for (int i = 0; i < MainForm.Instance.faTabStrip1.Items.Count; i++) { if (MainForm.Instance.faTabStrip1.Items[i].Title == CefConstHelper.CefDownloadTitle) { fATab = MainForm.Instance.faTabStrip1.Items[i]; DownloadTabIsOpen = true; break; } } if (DownloadTabIsOpen) { MainForm.Instance.LoadDownList_Info(); } })); Jfilepath = null; JsuggestedEndName = null; JsuggestedName = null; //dwitem = null; GsuggestedName = null; WrDownloadListItem = null; GC.Collect(); }
//重载鼠标松开的事件 protected override void OnMouseUp(MouseEventArgs mevent) { if (mDownPiont != PointToScreen(new Point(mevent.X, mevent.Y))) { m_bMouseDown = false; //this.Invalidate(); return; } MainForm.Date_Added = this.Date_Added; Point p = new Point(this.Location.X, this.Location.Y + this.Bottom); if (mevent.Button != MouseButtons.Left) { //增加右键菜单功能 IsSelect = true; //判断是否弹出子菜单 if (this.Type == "FaviFolder") { MainForm.Instance.Invoke(new Action(() => { MainForm.Instance.CopyFavriBtURL.Visible = false; MainForm.Instance.OpenRULInNewTab.Visible = false; MainForm.Instance.DelCurrentFavriBT.Visible = true; MainForm.Instance.Favi_3_Line.Visible = true; MainForm.Instance.Favi_Modif_Item.Visible = true; MainForm.Instance.Favri_BarMenu.Show(MousePosition); } )); } else { MainForm.Instance.Invoke(new Action(() => { MainForm.Instance.CopyFavriBtURL.Visible = true; MainForm.Instance.DelCurrentFavriBT.Visible = true; MainForm.Instance.OpenRULInNewTab.Visible = true; MainForm.Instance.Favi_3_Line.Visible = true; MainForm.Instance.Favi_Modif_Item.Visible = true; MainForm.Instance.Favri_BarMenu.Show(MousePosition); } )); } } else { //判断是否弹出子菜单 if (this.Type == "FaviFolder") { IsSelect = false; MainForm.Instance.CopyFavriBtURL.Visible = false; MainForm.Instance.DelCurrentFavriBT.Visible = false; MainForm.Instance.OpenRULInNewTab.Visible = false; MainForm.Instance.Favi_3_Line.Visible = false; MainForm.Instance.Favi_Modif_Item.Visible = false; MainForm.Instance.Invoke(new Action(() => { MainForm.Instance.fMenuStrip1.Items.Clear(); //MainForm.Instance.fMenuStrip1.Width = 266; string jsonStr = File.ReadAllText(PublicClass.currDirectiory + @"\UserData\Bookmarks"); try { var jobInfoList = JsonConvert.DeserializeObject <List <FavireBT> >(jsonStr); if (jobInfoList.Count > 0) { favireBTs = jobInfoList; int countfMenuItem = 0; int fWidth = 0; foreach (FavireBT jobInfo in favireBTs) { if (jobInfo.FatherID == this.ID) { countfMenuItem++; FMenuItem fMenuItem = new FMenuItem(); fMenuItem.Width = CefConstHelper.FmenuItem_TextRect_Width; fMenuItem.Height = CefConstHelper.FMenuItem_Height; fMenuItem.Title = jobInfo.Title; if (GetPixelByStr(fMenuItem.Title) < fMenuItem.Width) { fMenuItem.Width = CefConstHelper.FmenuItem_TextRect_Width; // GetPixelByStr(fMenuItem.Title); if (fMenuItem.Width > fWidth) { fWidth = fMenuItem.Width; } } else { fMenuItem.Width = GetPixelByStr(fMenuItem.Title); if (fMenuItem.Width > CefConstHelper.FmenuItem_TextRect_Width * 1.4) { fMenuItem.Width = (int)(CefConstHelper.FmenuItem_TextRect_Width * 1.4); } if (fMenuItem.Width > fWidth) { fWidth = fMenuItem.Width; } } fMenuItem.URL = jobInfo.URL; fMenuItem.ItemIcon = PublicClass.Base64ToImage(jobInfo.IconBase64str); // fMenuItem.Image = fMenuItem.ItemIcon; //这里如果给值就导致图标变大成24*24的 //fMenuItem.Text = fMenuItem.Title; fMenuItem.ToolTipText = jobInfo.Title; fMenuItem.Invalidate(); MainForm.Instance.fMenuStrip1.Items.Add(fMenuItem); } } MainForm.Instance.fMenuStrip1.Width = fWidth; //先不处理吧,还没想好?umm...:) // if(countfMenuItem * CefConstHelper.FMenuItem_Height + 4 > MainForm.Instance.Height - MainForm.Instance.ToolsPanel.Height ) //自动调整个快捷菜单的高度 MainForm.Instance.fMenuStrip1.Height = countfMenuItem * CefConstHelper.FMenuItem_Height + 6; for (int i = 0; i < MainForm.Instance.fMenuStrip1.Items.Count; i++) { MainForm.Instance.fMenuStrip1.Items[i].Width = fWidth; } } MainForm.Instance.fMenuStrip1.Show(MainForm.Instance.FavPanel.PointToScreen(p)); } catch (Exception e) { MessageBox.Show(e.Message); } } )); } else if (MainForm.Instance.faTabStrip1.SelectedItem.splic.Panel1.Controls.Count > 0) { MainForm.Instance.Invoke(new Action(() => { PublicClass.BrowserLoadingInfo(MainForm.Instance.faTabStrip1.SelectedItem); if (MainForm.Instance.faTabStrip1.SelectedItem.Title == CefConstHelper.CefDownloadTitle) { MainForm.Instance.faTabStrip1.SelectedItem.splic.Panel1.Controls.Clear(); // PublicClass.BrowserCSH(MainForm.Instance.faTabStrip1.SelectedItem); MainForm.Instance.faTabStrip1.SelectedItem.splic.Panel1.Controls.Add(MainForm.Instance.NewChromiumWeb(this.URL, MainForm.Instance.faTabStrip1.SelectedItem.TabIndex)); } else { if (MainForm.Instance.faTabStrip1.SelectedItem.ItemBrowser != null) { if (MainForm.Instance.faTabStrip1.SelectedItem.ItemBrowser.IsLoading) { MainForm.Instance.faTabStrip1.SelectedItem.ItemBrowser.StopLoad(); } } //当鼠标松开时,标示变量为按下并松开了控件 IsSelect = false; //刷新面板触发OnPaint重绘 if (MainForm.Instance.faTabStrip1.SelectedItem.IsPoPWindow) { MainForm.Instance.faTabStrip1.SelectedItem.splic.Panel1.Controls.Clear(); MainForm.Instance.faTabStrip1.SelectedItem.splic.Panel1.Controls.Add(MainForm.Instance.NewChromiumWeb(this.URL, MainForm.Instance.faTabStrip1.SelectedItem.TabIndex)); } else { MainForm.Instance.faTabStrip1.SelectedItem.ItemBrowser.GetMainFrame().LoadUrl(this.URL); } GC.Collect(); } } )); } else { MainForm.Instance.Invoke(new Action(() => { PublicClass.BrowserLoadingInfo(MainForm.Instance.faTabStrip1.SelectedItem); if (MainForm.Instance.faTabStrip1.SelectedItem.ItemBrowser != null) { if (MainForm.Instance.faTabStrip1.SelectedItem.ItemBrowser.IsLoading) { MainForm.Instance.faTabStrip1.SelectedItem.ItemBrowser.StopLoad(); } } //当鼠标松开时,标示变量为按下并松开了控件 IsSelect = false; //刷新面板触发OnPaint重绘 MainForm.Instance.faTabStrip1.SelectedItem.splic.Panel1.Controls.Add(MainForm.Instance.NewChromiumWeb(this.URL, MainForm.Instance.faTabStrip1.SelectedItem.TabIndex)); } )); } } m_bMouseDown = false; if (MainForm.Instance.faTabStrip1.SelectedItem != null && this.Type != "FaviFolder" && mevent.Button != MouseButtons.Right) { MainForm.Instance.textBoxXP1.Text = MainForm.Instance.faTabStrip1.SelectedItem.URL = this.uRL; } base.OnMouseUp(mevent); }