Пример #1
0
        // 设定自定义截图坐标
        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 DrawSelectionBox(Graphics g)
        {
            Font font = new Font("微软雅黑", 12f);

            g.DrawImage(this.screenImage, this._SelectedArea, this._SelectedArea, GraphicsUnit.Pixel);
            g.DrawRectangle(Pens.DodgerBlue, this._SelectedArea.Left, this._SelectedArea.Top, this._SelectedArea.Width - 1, this._SelectedArea.Height - 1);
            string showText;

            if (WindowHandle != IntPtr.Zero)
            {
                Api.POINT p = new Api.POINT(this._SelectedArea.Left, this._SelectedArea.Top);
                // 屏幕坐标转为客户端窗口坐标
                Api.ScreenToClient(WindowHandle, ref p);
                showText = string.Format($"按住鼠标左键选取要截取的区域\n按鼠标右键或ESC取消\nX相对坐标:{p.X} Y相对坐标:{p.Y}  宽:{this._SelectedArea.Width} 高:{this._SelectedArea.Height}");
            }
            else
            {
                showText = string.Format($"按住鼠标左键选取要截取的区域\n按鼠标右键或ESC取消\nX坐标:{this._SelectedArea.Left} Y坐标:{this._SelectedArea.Top}  宽:{this._SelectedArea.Width} 高:{this._SelectedArea.Height}");
            }
            // 测量显示字体需要的尺寸
            Size sizeRequired = g.MeasureString(showText, font).ToSize();
            // 显示字体的坐标
            Point displayPos = new Point(this._SelectedArea.Left, this._SelectedArea.Top - sizeRequired.Height - 5);

            if (displayPos.Y < 0)
            {
                displayPos.Y = this._SelectedArea.Top + 5;
            }

            if (displayPos.X + sizeRequired.Width > this.Width)
            {
                displayPos.X = this.Width - sizeRequired.Width;
            }

            if (displayPos.X == 0)
            {
                displayPos.X = 5;
            }

            // 在指定坐标绘制字体
            using (SolidBrush Brush = new SolidBrush(Color.FromArgb(125, 0, 0, 0)))
            {
                // 在矩形内部填充半透明橙色
                g.FillRectangle(Brush, new Rectangle(displayPos, sizeRequired));
                // 绘制字体
                g.DrawString(showText, font, Brushes.Orange, displayPos);
            }
        }
Пример #3
0
        // 设定自定义截图坐标
        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();
                }
            }
        }