// 设定自定义截图坐标 private void button_SetPosition_Click(object sender, EventArgs e) { bool change = HideWindow();// 隐藏窗口 Thread.Sleep(500); IntPtr hwnd = IntPtr.Zero; Rectangle rect; try { SaveWindowNameAndWindowClass(); using (var shot = new FrmScreenShot()) { if (!FixedScreen.WindowNameAndClassIsNull) { // 通过窗口类名或窗口标题获的窗口句柄 hwnd = Api.FindWindowHandle(FixedScreen.WindowName, FixedScreen.WindowClass); // 激活目标窗口,让目标窗口显示在最前方 Api.SetForegroundWindow(hwnd); Thread.Sleep(300);// 延时,等待窗口显示到最前方 } // 开始截图 if (shot.Start(hwnd) == DialogResult.Cancel) { return; } rect = shot.SelectedArea; } if (!FixedScreen.WindowNameAndClassIsNull) { Api.POINT p = new Api.POINT(rect.X, rect.Y); Api.ScreenToClient(hwnd, ref p); // 屏幕坐标转为客户端窗口坐标 screenRect = new Rectangle(p.X, p.Y, rect.Width, rect.Height); // 保存截图坐标高宽 } else { screenRect = rect; // 保存截图坐标高宽 } if (rect.Width > 0 && rect.Height > 0) { MessageBox.Show("设置成功!\n请点击“保存”按钮。", "截图翻译", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { throw new Exception("设置失败,请重试!"); } } catch (Exception ex) { MessageBox.Show("设置固定翻译坐标失败!\n原因:" + ex.Message, "截图翻译", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (change) { this.WindowState = FormWindowState.Normal; } } }
// 设定自定义截图坐标 private void button_SetPosition_Click(object sender, EventArgs e) { bool change = HideWindow();// 隐藏窗口 FrmScreenShot shot = null; IntPtr hwnd = IntPtr.Zero; try { SaveWindowNameAndWindowClass(); if (!FixedScreen.WindowNameAndClassIsNull) { // 通过窗口类名或窗口标题获的窗口句柄 hwnd = Api.FindWindowHandle(FixedScreen.WindowName, FixedScreen.WindowClass); // 激活目标窗口,让目标窗口显示在最前方 Api.SetForegroundWindow(hwnd); Thread.Sleep(300);// 延时,等待窗口显示到最前方 shot = new FrmScreenShot(); shot.WindowHandle = hwnd; } else { shot = new FrmScreenShot(); } // 显示截图窗口 if (shot.ShowDialog() == DialogResult.Cancel) { return; } if (!FixedScreen.WindowNameAndClassIsNull) { Api.POINT p = new Api.POINT(); p.X = shot.StartPos.X; p.Y = shot.StartPos.Y; // 屏幕坐标转为客户端窗口坐标 Api.ScreenToClient(hwnd, ref p); // 保存截图坐标高宽 screenRect = new Rectangle(p.X, p.Y, shot.SelectedArea.Width, shot.SelectedArea.Height); } else { // 保存截图坐标高宽 screenRect = new Rectangle(shot.StartPos.X, shot.StartPos.Y, shot.SelectedArea.Width, shot.SelectedArea.Height); } if (shot.SelectedArea.Width > 0 && shot.SelectedArea.Height > 0) { MessageBox.Show("设置成功!\n请点击“保存”按钮。", "截图翻译", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { throw new Exception("设置失败,请重试!"); } } catch (Exception ex) { MessageBox.Show("设置固定翻译坐标失败!\n原因:" + ex.Message, "截图翻译", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (change) { this.WindowState = FormWindowState.Normal; } if (shot != null && !shot.IsMyDisposed) { shot.MyDispose(); } } }