private static IntPtr SetHook(Win32Wrap.LowLevelKeyboardProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return(Win32Wrap.SetWindowsHookEx(Win32Wrap.WH_KEYBOARD_LL, proc, Win32Wrap.GetModuleHandle(curModule.ModuleName), 0)); } }
/// <summary> /// プライマリスクリーンの画像を取得する /// </summary> /// <returns>プライマリスクリーンの画像</returns> private void CaptureScreen(int p1x, int p1y, int p2x, int p2y) { //プライマリモニタのデバイスコンテキストを取得 IntPtr disDC = Win32Wrap.GetDC(IntPtr.Zero); //Bitmapの作成 Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); //Graphicsの作成 Graphics g = Graphics.FromImage(bmp); //Graphicsのデバイスコンテキストを取得 IntPtr hDC = g.GetHdc(); //Bitmapに画像をコピーする Win32Wrap.BitBlt(hDC, 0, 0, bmp.Width, bmp.Height, disDC, 0, 0, Win32Wrap.SRCCOPY); //解放 g.ReleaseHdc(hDC); g.Dispose(); Win32Wrap.ReleaseDC(IntPtr.Zero, disDC); // 画像を切り抜く var ptx = p1x; var pty = p1y; var width = p2x - p1x; var height = p2y - p1y; Rectangle rect = new Rectangle(ptx, pty, width, height); // (左上x, 左上y, 幅, 高さ) Bitmap bmpNew = bmp.Clone(rect, bmp.PixelFormat); // 画像を保存 DateTime dt = DateTime.Now; var capName = $@".\({p1x.ToString()}_{p1y.ToString()})_({p2x.ToString()}_{p2y.ToString()})_{dt.ToString(Win32Wrap.FORMAT_TIME)}.png"; bmpNew.Save(capName); //(おまけ)画像ファイルのフルパスをクリップボードに // var absCapName = System.IO.Path.GetFullPath(capName).ToString(); // Clipboard.SetText(absCapName); // スレッドがSTAでないとだめ。 //(おまけ)画像をクリップボードに Clipboard.SetImage(bmpNew); // 画像リソースを解放 bmp.Dispose(); bmpNew.Dispose(); // キャプチャ通知を飛ばす this.notifyIcon3.BalloonTipTitle = "キャプチャしました"; this.notifyIcon3.BalloonTipText = capName; this.notifyIcon3.ShowBalloonTip(1000); return; }
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && wParam == (IntPtr)Win32Wrap.WM_KEYDOWN) { int vkCode = Marshal.ReadInt32(lParam); Keys k = (Keys)vkCode; if (k == Keys.F2) { short s = Win32Wrap.GetKeyState(Keys.ControlKey); bool ctrl = ((s & 0x8000) != 0); // Console.WriteLine("Ctrl:{0}, {1}, {2}, {3}", ctrl ? "1" : "0", Convert.ToString((int)lParam, 2), Convert.ToString((int)wParam, 2), Convert.ToString(s, 2).PadLeft(16, '0')); if (ctrl) { DoCapture(); } } } return(Win32Wrap.CallNextHookEx(_hookID, nCode, wParam, lParam)); }
// #tabPage2 private void timer2_Tick(object sender, EventArgs e) { sp = Cursor.Position; // スクリーン座標の取得 cp = this.PointToClient(sp); // クライアント座標に変換 text21[0].Text = $"{sp.X.ToString()},{sp.Y.ToString()}"; StringBuilder sb = new StringBuilder(65535); //65535に特に意味はない Win32Wrap.GetWindowText(Win32Wrap.GetForegroundWindow(), sb, 65535); text21[3].Text = $"{sb}"; toolTip2.SetToolTip(text21[3], $"{sb}"); // ツールチップ更新 IntPtr hWnd = Win32Wrap.GetForegroundWindow(); // 1. フォアグランドのウィンドウハンドルを取得 int id = 0; try { Win32Wrap.GetWindowThreadProcessId(hWnd, ref id); // 2.ウィンドウハンドル→プロセスIDを取得 text21[2].Text = Process.GetProcessById(id).ProcessName; // プロセスID → プロセス名 Win32Wrap.RECT windowRect = new Win32Wrap.RECT(); // 3.ハンドル→ウインドウの見た目通りのRectを取得する // (GetWindowRectだと見た目とのズレがあるため、DwmGetWindowAttribute()を使用。) Win32Wrap.DwmGetWindowAttribute(hWnd, Win32Wrap.DWMWA_EXTENDED_FRAME_BOUNDS, ref windowRect, 4 * 4); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; text21[1].Text = $"{((sp.X-windowRect.left).ToString())},{((sp.Y-windowRect.top).ToString())}"; text21[4].Text = $"{width.ToString()}x{height.ToString()}"; text21[5].Text = $"{windowRect.left.ToString()},{windowRect.top.ToString()}"; text21[6].Text = $"{windowRect.right.ToString()},{windowRect.bottom.ToString()}"; if (m_curId != id) { text31[4].Text = Process.GetProcessById(id).ProcessName; text31[5].Text = windowRect.left.ToString(); text31[6].Text = windowRect.top.ToString(); text31[7].Text = windowRect.right.ToString(); text31[8].Text = windowRect.bottom.ToString(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
public static string DoCapture(IntPtr hwnd, string savingDirectory) { ScreenCapture sc = new ScreenCapture(); // ファイル名決定 string fname = ""; string fpath = ""; bool ok = false; for (int i = 0; i <= 99; i++) { fname = "capt_" + DateTime.Now.ToString("yyyyMMdd_HHmmss_") + i.ToString().PadLeft(2, '0') + ".png"; fpath = Path.Combine(savingDirectory, fname); if (!File.Exists(fpath)) { ok = true; break; } } if (ok) { // capture this window, and save it sc.CaptureWindowToFile(hwnd, fpath, ImageFormat.Png); // title取得 StringBuilder title = new StringBuilder(1048); Win32Wrap.GetWindowText(hwnd, title, 1024); // ファイル名表示 Debug.WriteLine("{0} ({1})", fname, title.ToString()); // ファイル名を返す return(fpath); } else { throw new Exception("Failed to decide filename"); } }
static void Main() { Gd.InitGd(); var doWork = Task.Run(() => { try { Thread.Sleep(Timeout.Infinite); } catch (ThreadInterruptedException) { } Application.Exit(); // Quick exit for demonstration only. }); _hookID = SetHook(_proc); Application.Run(); Win32Wrap.UnhookWindowsHookEx(_hookID); }
static void DoCapture(IntPtr hwnd) { ScreenCapture sc = new ScreenCapture(); // ファイル名決定 string fname = ""; string fpath = ""; bool ok = false; for (int i = 0; i <= 99; i++) { fname = "capt_" + DateTime.Now.ToString("yyyyMMdd_HHmmss_") + i.ToString().PadLeft(2, '0') + ".png"; fpath = "C:\\_tmp\\" + fname; if (!File.Exists(fpath)) { ok = true; break; } } if (ok) { // capture this window, and save it sc.CaptureWindowToFile(hwnd, fpath, ImageFormat.Png); // title取得 StringBuilder title = new StringBuilder(1048); Win32Wrap.GetWindowText(hwnd, title, 1024); // ファイル名表示 Console.WriteLine("{0} ({1})", fname, title.ToString()); } else { Console.WriteLine("failed to decide filename"); } }
public static string CaptureActiveWindow(string savingDirectory) { IntPtr hwnd = Win32Wrap.GetForegroundWindow(); return(DoCapture(hwnd, savingDirectory)); }
static void DoCapture() { IntPtr hwnd = Win32Wrap.GetForegroundWindow(); DoCapture(hwnd); }
public static void EndHook() { Win32Wrap.UnhookWindowsHookEx(_hookID); }