/// <summary> /// 傳入文章頁面 解析出圖片再popup出來 /// </summary> /// <param name="url"></param> public void ImagePopup(List <Image> imageFileList, DataGridViewRow Row, Model.RssObj RssObj) { try { //如果有解析到圖片 if (imageFileList.Count > 0) { Form form = new Form(); form.StartPosition = FormStartPosition.CenterScreen; //popup視窗標題 form.Text = "預覽圖 [" + RssObj.Size + "] " + RssObj.Title; //因為已經有按鈕了 所以從按鈕高度開始插入picturebox int TotalHeight = FormSetting.PopupWindow.btnDownloadHeight; int MaxWidth = 0; //依據imageList的個數 產生出數個picturebox foreach (Image imageFile in imageFileList) { PictureBox eachPictureBox = new PictureBox(); form.Controls.Add(eachPictureBox); eachPictureBox.SizeMode = PictureBoxSizeMode.AutoSize; //將圖片下移總高度 eachPictureBox.Top = TotalHeight; //載入圖片 eachPictureBox.Image = imageFile; //如果總高度小於螢幕高度 將總高度加上此圖片的高度 if (TotalHeight + eachPictureBox.Size.Height < Screen.PrimaryScreen.Bounds.Height) { TotalHeight += eachPictureBox.Size.Height; } else { TotalHeight = Screen.PrimaryScreen.Bounds.Height - 50; } //取得最大圖片的寬度 if (eachPictureBox.Size.Width > MaxWidth) { MaxWidth = eachPictureBox.Size.Width; } } //改變form的大小 如果小於setting裡的最小寬度 就設為最小寬度 form.Size = new Size(MaxWidth < FormSetting.PopupWindow.popWindowMinWidth ? FormSetting.PopupWindow.popWindowMinWidth : MaxWidth, TotalHeight); //增加下載連結按鈕 Button btnDownload = new Button(); btnDownload.Width = FormSetting.PopupWindow.btnDownloadWidth; btnDownload.Height = FormSetting.PopupWindow.btnDownloadHeight; btnDownload.ForeColor = Color.Black; btnDownload.Text = "下載"; //讓button置於最上面的中間 btnDownload.Left = (form.ClientSize.Width - btnDownload.Width) / 2; form.Controls.Add(btnDownload); //增加熱鍵說明的label Label info = new Label(); info.TextAlign = ContentAlignment.TopLeft; //C#顏色表 http://www.flounder.com/csharp_color_table.htm info.BackColor = Color.SkyBlue; info.Font = new Font("Arial", 10, FontStyle.Bold); string text = "熱鍵:" + Environment.NewLine + "(D):下載種子 (C):關閉視窗 (A):下載種子並關閉視窗"; info.Text = text; info.AutoSize = true; form.Controls.Add(info); //綁定下載按鈕點擊事件 btnDownload.Click += (sender, EventArgs) => buttonDownload_Click(sender, EventArgs, RssObj.DownloadLink); //啟用form的點擊事件 form.KeyPreview = true; //綁定form的熱鍵 form.KeyDown += (sender, e) => this.PopFormHotKey(sender, e, form, RssObj.DownloadLink); //如果預覽圖大於螢幕高度 就增加捲軸 if (TotalHeight >= (Screen.PrimaryScreen.Bounds.Height - 50)) { form.AutoScroll = true; } //表單透明度 記得拿掉 form.Opacity = FormSetting.FormOpaticy; //根據設定要不要顯示彈出視窗 debug用 if (FormSetting.IsEnablePopup) { //form.Show(); //讓視窗出現時不要取得focus FormControl.ShowInactiveTopmost(form); } //end of FormSetting.IsEnablePopup } else { //彈出視窗詢問是否直接開啟網頁 ImageNotFindBehavior(RssObj.ArticleLink, "na"); } }catch (Exception ex) { //彈出視窗詢問是否直接開啟網頁 ImageNotFindBehavior(RssObj.ArticleLink, "error"); } }
/// <summary> /// Row Cell Button Click Event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { try { //不是head row if (e.RowIndex != -1) { DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex]; //建立Rss物件 Model.RssObj RssObj = new Model.RssObj( row.Cells["articleLink"] == null ? "" : row.Cells["articleLink"].Value.ToString(), row.Cells["DownloadLink"] == null ? "" : row.Cells["DownloadLink"].Value.ToString(), row.Cells["Size"] == null ? "" : row.Cells["Size"].Value.ToString(), row.Cells["Title"] == null ? "" : row.Cells["Title"].Value.ToString()); #region GridView的預覽按鈕Click //============== //如果按下的是預覽按鈕 if (e.ColumnIndex == dataGridView1.Columns["btnView"].Index && e.RowIndex >= 0) { if (!string.IsNullOrEmpty(RssObj.ArticleLink)) { //取得圖片網址List List <string> imageFileList = new List <string>(); List <Image> imageFiles = new List <Image>(); var bw = new BackgroundWorker(); bw.DoWork += (sender1, args) => { //取得大圖的url imageFileList = new GetPreViewImage().CallImageHanderdle(RssObj.ArticleLink); imageFileList.ForEach(delegate(String imgurl) { //讀取大圖存進list Image tempFile = LoadBitmap(imgurl); imageFiles.Add(tempFile); }); args.Result = imageFiles; }; bw.RunWorkerCompleted += (sender1, args) => { //如果取得圖片有exception if (args.Error != null) { //彈出視窗詢問是否直接開啟網頁 ImageNotFindBehavior(RssObj.ArticleLink, "error"); } else { //彈出視窗 ImagePopup((List <Image>)args.Result, row, RssObj); } }; bw.RunWorkerAsync(); // starts the background worker } else { MessageBox.Show("文章連結為空"); } } #endregion #region GridView的下載按鈕click //============== //如果按下的是下載按鈕 if (e.ColumnIndex == dataGridView1.Columns["btnDownload"].Index && e.RowIndex >= 0) { if (!string.IsNullOrEmpty(RssObj.DownloadLink)) { var t = new Thread(() => DownloadTorr(RssObj.DownloadLink)); t.IsBackground = true; t.Start(); } else { MessageBox.Show("下載連結為空"); } } #endregion } } catch (Exception ex) { MessageBox.Show("發生錯誤" + ex.Message); } }