/// <summary> /// /// </summary> /// <param name="m"></param> public W_截圖(MainWindow m, e_截圖類型 e_type) { this.M = m; //取得螢幕總其實坐標、總大小 int_螢幕起始坐標_x = 0; foreach (var screen in System.Windows.Forms.Screen.AllScreens) //列出所有螢幕資訊 { if (screen.Bounds.X < int_螢幕起始坐標_x) { int_螢幕起始坐標_x = screen.Bounds.X; } if (screen.Bounds.Y < int_螢幕起始坐標_y) { int_螢幕起始坐標_y = screen.Bounds.Y; } int yy = screen.Bounds.Y + screen.Bounds.Height; if (yy > d_螢幕_h) { d_螢幕_h = yy; } int xx = screen.Bounds.X + screen.Bounds.Width; if (xx > d_螢幕_w) { d_螢幕_w = xx; } } d_螢幕_w -= int_螢幕起始坐標_x; d_螢幕_h -= int_螢幕起始坐標_y; if (M.ShowInTaskbar && M.WindowState != WindowState.Minimized) { //截圖前記錄視窗狀態 ws_全螢幕前的狀態 = M.WindowState; d_記錄視窗位子 = M.Top; //避免全螢幕導致視窗無法隱藏 M.fun_鎖定視窗(true); M.WindowStyle = System.Windows.WindowStyle.None;//無邊框 M.WindowState = WindowState.Normal; M.Top = -5000; } this.Closed += (object sender, EventArgs e) => { if (M.ShowInTaskbar && M.WindowState != WindowState.Minimized) { M.Top = d_記錄視窗位子; M.WindowState = ws_全螢幕前的狀態; M.WindowStyle = System.Windows.WindowStyle.SingleBorderWindow; M.fun_鎖定視窗(false); } }; bimg = CaptureScreen();//全螢幕截圖 InitializeComponent(); this.Top = -5000; this.Show(); st_按鈕群.Visibility = Visibility.Hidden; /*var tim = new System.Windows.Forms.Timer(); * tim.Interval = 100; * tim.Tick += (sender, e) => {*/ func_初始化(e_type); /*tim.Stop(); * }; * tim.Start();*/ }
/// <summary> /// /// </summary> private void func_初始化(e_截圖類型 e_type) { if (e_type == e_截圖類型.全螢幕截圖_png) { String s_儲存路徑 = M.func_取得儲存檔名("png"); //轉換成 rgb24 ,才不會有破圖現象 var bmpOut = new System.Drawing.Bitmap(bimg.Width, bimg.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); var g = System.Drawing.Graphics.FromImage(bmpOut); g.DrawImage(bimg, new System.Drawing.Rectangle(0, 0, bimg.Width, bimg.Height), new System.Drawing.Rectangle(0, 0, bimg.Width, bimg.Height), System.Drawing.GraphicsUnit.Pixel ); bmpOut.Save(s_儲存路徑); //自動存入剪貼簿 try { if (M.checkBox_自動存入剪貼簿.IsChecked.Value) { Clipboard.SetData(DataFormats.Bitmap, bimg); } } catch { } func_關閉程式(); return; } st_size與放大鏡.Visibility = Visibility.Hidden;//隱藏szie資訊 //初始化顏色 b_上.Fill = color_原始; b_下.Fill = color_原始; b_右.Fill = color_原始; b_左.Fill = color_原始; b_右上.Stroke = color_原始; b_右下.Stroke = color_原始; b_左上.Stroke = color_原始; b_左下.Stroke = color_原始; //取得解析度偏差 PresentationSource source = PresentationSource.FromVisual(M); d_解析度比例_x = source.CompositionTarget.TransformToDevice.M11; d_解析度比例_y = source.CompositionTarget.TransformToDevice.M22; this.Background = new ImageBrush(ToBitmapSource(bimg));//設定背景 this.Focus(); //程式最大化 this.Width = d_螢幕_w / d_解析度比例_x; this.Height = d_螢幕_h / d_解析度比例_y; //var Work = System.Windows.Forms.Screen.GetBounds(new System.Drawing.Point((int)this.Left, (int)this.Top)); //MessageBox.Show(d_螢幕_h + "\n" + System.Windows.Forms.Cursor.Clip.Size.Height + ""); this.Left = int_螢幕起始坐標_x; this.Top = int_螢幕起始坐標_y; //啟動計時器 t_拖曳中 = new System.Windows.Forms.Timer(); t_拖曳中.Interval = 10; t_拖曳中.Tick += T_Tick; t_拖曳中.Start(); T_Tick(null, null);//立刻執行,避免使用者開啟截圖後立即點擊 func_重新(); Rectangle[] ar = { b_中心拖曳, b_左上, b_上, b_右上, b_右, b_右下, b_下, b_左下, b_左 }; //滑鼠按下 for (int i = 0; i < ar.Length; i++) { ar[i].MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { re_目前拖曳物件 = (Rectangle)sender; st_按鈕群.Visibility = Visibility.Hidden; d_滑鼠xy = func_取得滑鼠(); t_拖曳中.Start(); }; } //滑鼠移入後改變顏色 for (int i = 0; i < ar.Length; i++) { ar[i].MouseEnter += (sender, e) => { if (sender == b_上 || sender == b_右 || sender == b_下 || sender == b_左) { if (t_拖曳中.Enabled == false) { ((Rectangle)sender).Fill = color_滑鼠移入; } } }; ar[i].MouseLeave += (sender, e) => { if (sender == b_上 || sender == b_右 || sender == b_下 || sender == b_左) { if (t_拖曳中.Enabled == false) { ((Rectangle)sender).Fill = color_原始; } } }; ar[i].MouseDown += (sender, e) => { if (sender == b_上 || sender == b_右 || sender == b_下 || sender == b_左) { ((Rectangle)sender).Fill = color_滑鼠移入; } }; ar[i].MouseEnter += (sender, e) => { if (sender == b_左上 || sender == b_左下 || sender == b_右上 || sender == b_右下) { if (t_拖曳中.Enabled == false) { ((Rectangle)sender).Stroke = color_滑鼠移入; } } }; ar[i].MouseLeave += (sender, e) => { if (sender == b_左上 || sender == b_左下 || sender == b_右上 || sender == b_右下) { if (t_拖曳中.Enabled == false) { ((Rectangle)sender).Stroke = color_原始; } } }; ar[i].MouseDown += (sender, e) => { if (sender == b_左上 || sender == b_左下 || sender == b_右上 || sender == b_右下) { ((Rectangle)sender).Stroke = color_滑鼠移入; } }; } b_中心拖曳.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { d_xywh = new double[] { b_中心拖曳.Margin.Left, b_中心拖曳.Margin.Top, b_中心拖曳.ActualWidth, b_中心拖曳.ActualHeight }; int_拖曳模式 = 拖曳模式.中心; }; b_上.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { d_xywh = new double[] { b_上.Margin.Left, b_上.Margin.Top, b_上.ActualWidth, b_上.ActualHeight }; int_拖曳模式 = 拖曳模式.; }; b_下.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { d_xywh = new double[] { b_下.Margin.Left, b_下.Margin.Top, b_下.ActualWidth, b_下.ActualHeight }; int_拖曳模式 = 拖曳模式.; }; b_右.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { d_xywh = new double[] { b_右.Margin.Left, b_右.Margin.Top, b_右.ActualWidth, b_右.ActualHeight }; int_拖曳模式 = 拖曳模式.右; }; b_左.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { d_xywh = new double[] { b_左.Margin.Left, b_左.Margin.Top, b_左.ActualWidth, b_左.ActualHeight }; int_拖曳模式 = 拖曳模式.左; }; b_左上.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { d_xywh = new double[] { b_左上.Margin.Left, b_左上.Margin.Top, b_左上.ActualWidth, b_左上.ActualHeight }; int_拖曳模式 = 拖曳模式.左上; }; b_右上.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { d_xywh = new double[] { b_右上.Margin.Left, b_右上.Margin.Top, b_右上.ActualWidth, b_右上.ActualHeight }; int_拖曳模式 = 拖曳模式.右上; }; b_右下.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { d_xywh = new double[] { b_右下.Margin.Left, b_右下.Margin.Top, b_右下.ActualWidth, b_右下.ActualHeight }; int_拖曳模式 = 拖曳模式.右下; }; b_左下.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { d_xywh = new double[] { b_左下.Margin.Left, b_左下.Margin.Top, b_左下.ActualWidth, b_左下.ActualHeight }; int_拖曳模式 = 拖曳模式.左下; }; this.MouseLeftButtonUp += (object sender, MouseButtonEventArgs e) => { if (rg_遮罩.Rect.Width == 0 || rg_遮罩.Rect.Height == 0) { func_重新(); return; } t_拖曳中.Enabled = false; rect_游標.Margin = new Thickness(-100, -100, 0, 0); fun_顯示按鈕(); }; //第一次拖曳 this.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => { if (bool_初始) { b_左上.Margin = new Thickness(func_取得滑鼠().X - 10, func_取得滑鼠().Y - 10, 0, 0); d_xywh = new double[] { b_左上.Margin.Left, b_左上.Margin.Top, b_左上.ActualWidth, b_左上.ActualHeight }; d_滑鼠xy = func_取得滑鼠(); t_拖曳中.Start(); int_拖曳模式 = 拖曳模式.右下; bool_初始 = false; } }; this.KeyDown += (sender, e) => { }; this.KeyUp += (object sender, KeyEventArgs e) => { //按空白鍵 拖曳 if (e.Key == Key.Space) { bool_空白鍵記錄 = false; Rectangle r = null; if (int_空白鍵記錄 == 拖曳模式.左上) { r = b_左上; } else if (int_空白鍵記錄 == 拖曳模式.右上) { r = b_右上; } else if (int_空白鍵記錄 == 拖曳模式.右下) { r = b_右下; } else if (int_空白鍵記錄 == 拖曳模式.左下) { r = b_左下; } else if (int_空白鍵記錄 == 拖曳模式.) { r = b_上; } else if (int_空白鍵記錄 == 拖曳模式.) { r = b_下; } else if (int_空白鍵記錄 == 拖曳模式.右) { r = b_右; } else if (int_空白鍵記錄 == 拖曳模式.左) { r = b_左; } else { return; } d_xywh = new double[] { r.Margin.Left, r.Margin.Top, r.ActualWidth, r.ActualHeight }; d_滑鼠xy = func_取得滑鼠(); int_拖曳模式 = int_空白鍵記錄; this.Title = "**"; } }; //右鍵 結束 this.MouseRightButtonUp += (object sender, MouseButtonEventArgs e) => { if (int_拖曳模式 == 拖曳模式.none) { func_關閉程式(); } else { func_重新(); } }; button_關閉.Click += (sender, e) => { func_關閉程式(); }; button_重新.Click += (sender, e) => { func_重新(); }; button_確認_png.Click += (sender, e) => { fun_確認儲存("png"); }; button_確認_jpg.Click += (sender, e) => { fun_確認儲存("jpg"); }; button_確認_copy.Click += (sender, e) => { fun_確認儲存("copy"); }; button_編輯.Click += (sender, e) => { fun_確認儲存("edit"); }; but_複製顏色_16.Click += (sender, e) => { Clipboard.SetText(lab_複製顏色_16.Content + ""); func_關閉程式(); }; but_複製顏色_rgb.Click += (sender, e) => { Clipboard.SetText(lab_複製顏色_rgb.Content + ""); func_關閉程式(); }; }