void CaptureListItem_Loaded(object sender, RoutedEventArgs e) { entry = (CaptureEntry)this.DataContext; if (entry.SelectedIndex == 0) { border1.BorderThickness = new Thickness(2.0); border2.BorderThickness = new Thickness(0.0); } else { border1.BorderThickness = new Thickness(0.0); border2.BorderThickness = new Thickness(2.0); } entry.LoadImage(); image2.Source = entry.ClipImage; image1.Source = entry.WindowImage; txtName2.Text = string.IsNullOrEmpty(entry.ClickedName) ? entry.ClickedControlType : entry.ClickedName; if (entry.WindowHandle != entry.ClickedWindowHandle) { txtName1.Text = entry.WindowName; // string.IsNullOrEmpty(entry.WindowName) ? entry.WindowControlType : entry.WindowName; } //同じなら表示しない txtName1.Visibility = (txtName2.Text == txtName1.Text) ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible; }
void CaptureListItem_Loaded(object sender, RoutedEventArgs e) { entry = (CaptureEntry)this.DataContext; if (entry.SelectedIndex == 0) { border1.BorderThickness = new Thickness(2.0); border2.BorderThickness = new Thickness(0.0); } else { border1.BorderThickness = new Thickness(0.0); border2.BorderThickness = new Thickness(2.0); } entry.LoadImage(); image2.Source = entry.ClipImage; image1.Source = entry.WindowImage; txtName2.Text = string.IsNullOrEmpty(entry.ClickedName) ? entry.ClickedControlType : entry.ClickedName; if (entry.WindowHandle != entry.ClickedWindowHandle) txtName1.Text = entry.WindowName; // string.IsNullOrEmpty(entry.WindowName) ? entry.WindowControlType : entry.WindowName; //同じなら表示しない txtName1.Visibility = (txtName2.Text == txtName1.Text) ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible; }
/// <summary> /// 画像選択 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e) { CaptureEntry entry = (CaptureEntry)listView1.SelectedItem; if (entry == null) { return; } if (entry.SelectedIndex == 1) { image1.Source = entry.ClipImage; } else { image1.Source = entry.WindowImage; } }
void DoCapture(object param) { MouseMessage mouse = (MouseMessage)param; if (_captureList.Count > 100) return; //ファイル数制限をつけておく System.Windows.Point point = new Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y); System.Windows.Automation.AutomationElement el = null; try { el = System.Windows.Automation.AutomationElement.FromPoint(point); if (el == null) return; if (el.Current.ProcessId == System.Diagnostics.Process.GetCurrentProcess().Id) return; //自分自身のWindow上のクリックは無視 string path_w = System.IO.Path.Combine(setting.SaveDir, DateTime.Now.ToString("HHmmssfff") + "-w.png"); string path_c = System.IO.Path.Combine(setting.SaveDir, DateTime.Now.ToString("HHmmssfff") + "-c.png"); System.Windows.Automation.AutomationElement el_top = TopElement(el); CaptureEntry entry = new CaptureEntry(); entry.MouseMessage = mouse; entry.ClickedControlType = el.Current.LocalizedControlType; entry.ClickedName = el.Current.Name; entry.ClickedRect = el.Current.BoundingRectangle; entry.ClickedWindowHandle = el.Current.NativeWindowHandle; entry.Cursor = point; entry.PathToWindowImage = path_w; entry.PathToClipImage = path_c; if (el_top != null) { entry.WindowControlType = el_top.Current.LocalizedControlType; entry.WindowName = el_top.Current.Name; entry.WindowRect = el_top.Current.BoundingRectangle; entry.WindowHandle = el_top.Current.NativeWindowHandle; } //キャプチャ //Cpt(el_top, path_w); Cpt(point, path_c, path_w, entry); this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { //リストに追加 entry.No = _captureList.Count + 1; entry.SelectedIndex = setting.InitialImage; _captureList.Add(entry); listView1.SelectedIndex = (_captureList.Count - 1); //スクロール位置と画像表示 this.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { listView1.ScrollIntoView(entry); if (entry.SelectedIndex == 1) image1.Source = entry.ClipImage; else image1.Source = entry.WindowImage; })); })); } catch (System.Windows.Automation.ElementNotAvailableException) { //無視 } catch (Exception ex) { this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { MessageBox.Show(ex.Message + "\n" + ex.StackTrace); })); } }
/// <summary> /// キャプチャ(切り抜き) /// </summary> /// <param name="p"></param> /// <param name="path"></param> void Cpt(Point p, string path_c, string path_w, CaptureEntry entry) { int top = setting.ClipSizeTop; int left = setting.ClipSizeLeft; int right = setting.ClipSizeRight; int bottom = setting.ClipSizeBottom; System.Drawing.Bitmap bmp = CaptureHelper.CaptureScreen(); int top_offset = 0; int left_offset = 0; if ((p.Y + top) < 0) top_offset = -((int)(p.Y + top)); else if (bmp.Height < (p.Y + bottom)) top_offset = bmp.Height - (int)(p.Y + bottom); if ((p.X + left) < 0) left_offset = -((int)(p.X + left)); else if (bmp.Width < (p.X + right)) left_offset = bmp.Width - (int)(p.X + right); int adj_top = top + top_offset; int adj_left = left + left_offset; int adj_right = right + left_offset; int adj_bottom = bottom + top_offset; //マウス範囲を切り抜く System.Drawing.Rectangle rect = new System.Drawing.Rectangle((int)(p.X + adj_left), (int)(p.Y + adj_top), -adj_left + adj_right, -adj_top + adj_bottom); entry.ClipImageRect = new Rect((int)(p.X + adj_left), (int)(p.Y + adj_top), -adj_left + adj_right, -adj_top + adj_bottom); System.Drawing.Bitmap bmpNew = bmp.Clone(rect, bmp.PixelFormat); bmpNew.Save(path_c, System.Drawing.Imaging.ImageFormat.Png); /* this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { MessageBox.Show(entry.WindowRect.X.ToString() + " " + entry.WindowRect.Y.ToString() + " " + entry.WindowRect.Width.ToString() + " " + entry.WindowRect.Height.ToString() + " "); })); */ //ウィンドウを範囲を切り抜く if (!entry.WindowRect.IsEmpty) { //領域座標を微調整する int w_x = (int)entry.WindowRect.X; int w_y = (int)entry.WindowRect.Y; int w_width = (int)entry.WindowRect.Width; int w_height = (int)entry.WindowRect.Height; if (w_x < 0) w_x = 0; if (w_y < 0) w_y = 0; if (bmp.Width < (w_x + w_width)) w_width = bmp.Width - w_x; if (bmp.Height < (w_y + w_height)) w_height = bmp.Height - w_y; System.Drawing.Rectangle rect2 = new System.Drawing.Rectangle(w_x, w_y, w_width, w_height); System.Drawing.Bitmap bmpNew2 = bmp.Clone(rect2, bmp.PixelFormat); bmpNew2.Save(path_w, System.Drawing.Imaging.ImageFormat.Png); } }
/// <summary> /// キャプチャ(切り抜き) /// </summary> /// <param name="p"></param> /// <param name="path"></param> void Cpt(Point p, string path_c, string path_w, CaptureEntry entry) { int top = setting.ClipSizeTop; int left = setting.ClipSizeLeft; int right = setting.ClipSizeRight; int bottom = setting.ClipSizeBottom; System.Drawing.Bitmap bmp = CaptureHelper.CaptureScreen(); int top_offset = 0; int left_offset = 0; if ((p.Y + top) < 0) { top_offset = -((int)(p.Y + top)); } else if (bmp.Height < (p.Y + bottom)) { top_offset = bmp.Height - (int)(p.Y + bottom); } if ((p.X + left) < 0) { left_offset = -((int)(p.X + left)); } else if (bmp.Width < (p.X + right)) { left_offset = bmp.Width - (int)(p.X + right); } int adj_top = top + top_offset; int adj_left = left + left_offset; int adj_right = right + left_offset; int adj_bottom = bottom + top_offset; //マウス範囲を切り抜く System.Drawing.Rectangle rect = new System.Drawing.Rectangle((int)(p.X + adj_left), (int)(p.Y + adj_top), -adj_left + adj_right, -adj_top + adj_bottom); entry.ClipImageRect = new Rect((int)(p.X + adj_left), (int)(p.Y + adj_top), -adj_left + adj_right, -adj_top + adj_bottom); System.Drawing.Bitmap bmpNew = bmp.Clone(rect, bmp.PixelFormat); bmpNew.Save(path_c, System.Drawing.Imaging.ImageFormat.Png); /* * this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate * { * MessageBox.Show(entry.WindowRect.X.ToString() + " " + * entry.WindowRect.Y.ToString() + " " + * entry.WindowRect.Width.ToString() + " " + * entry.WindowRect.Height.ToString() + " "); * })); */ //ウィンドウを範囲を切り抜く if (!entry.WindowRect.IsEmpty) { //領域座標を微調整する int w_x = (int)entry.WindowRect.X; int w_y = (int)entry.WindowRect.Y; int w_width = (int)entry.WindowRect.Width; int w_height = (int)entry.WindowRect.Height; if (w_x < 0) { w_x = 0; } if (w_y < 0) { w_y = 0; } if (bmp.Width < (w_x + w_width)) { w_width = bmp.Width - w_x; } if (bmp.Height < (w_y + w_height)) { w_height = bmp.Height - w_y; } System.Drawing.Rectangle rect2 = new System.Drawing.Rectangle(w_x, w_y, w_width, w_height); System.Drawing.Bitmap bmpNew2 = bmp.Clone(rect2, bmp.PixelFormat); bmpNew2.Save(path_w, System.Drawing.Imaging.ImageFormat.Png); } }
void DoCapture(object param) { MouseMessage mouse = (MouseMessage)param; if (_captureList.Count > 100) { return; //ファイル数制限をつけておく } System.Windows.Point point = new Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y); System.Windows.Automation.AutomationElement el = null; try { el = System.Windows.Automation.AutomationElement.FromPoint(point); if (el == null) { return; } if (el.Current.ProcessId == System.Diagnostics.Process.GetCurrentProcess().Id) { return; //自分自身のWindow上のクリックは無視 } string path_w = System.IO.Path.Combine(setting.SaveDir, DateTime.Now.ToString("HHmmssfff") + "-w.png"); string path_c = System.IO.Path.Combine(setting.SaveDir, DateTime.Now.ToString("HHmmssfff") + "-c.png"); System.Windows.Automation.AutomationElement el_top = TopElement(el); CaptureEntry entry = new CaptureEntry(); entry.MouseMessage = mouse; entry.ClickedControlType = el.Current.LocalizedControlType; entry.ClickedName = el.Current.Name; entry.ClickedRect = el.Current.BoundingRectangle; entry.ClickedWindowHandle = el.Current.NativeWindowHandle; entry.Cursor = point; entry.PathToWindowImage = path_w; entry.PathToClipImage = path_c; if (el_top != null) { entry.WindowControlType = el_top.Current.LocalizedControlType; entry.WindowName = el_top.Current.Name; entry.WindowRect = el_top.Current.BoundingRectangle; entry.WindowHandle = el_top.Current.NativeWindowHandle; } //キャプチャ //Cpt(el_top, path_w); Cpt(point, path_c, path_w, entry); this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { //リストに追加 entry.No = _captureList.Count + 1; entry.SelectedIndex = setting.InitialImage; _captureList.Add(entry); listView1.SelectedIndex = (_captureList.Count - 1); //スクロール位置と画像表示 this.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { listView1.ScrollIntoView(entry); if (entry.SelectedIndex == 1) { image1.Source = entry.ClipImage; } else { image1.Source = entry.WindowImage; } })); })); } catch (System.Windows.Automation.ElementNotAvailableException) { //無視 } catch (Exception ex) { this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { MessageBox.Show(ex.Message + "\n" + ex.StackTrace); })); } }