【问题标题】:Print an image of the content of a Panel excluding any external overlapping Window打印不包括任何外部重叠窗口的面板内容的图像
【发布时间】:2021-10-23 16:47:36
【问题描述】:

我有一些问题。
我在表单中有一个面板和一个图片框。
我想打开一个 Windows 应用程序(例如记事本)并将其作为面板的父级。
然后我想在 PictureBox 中显示面板内容的图像:

我的代码:

[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll", SetLastError = true)]
private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);

[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

IntPtr appWin1;

private void Notepad_Button_Click(object sender, EventArgs e)
{
    ProcessStartInfo ps1 = new ProcessStartInfo(@"notepad.exe");
    ps1.WindowStyle = ProcessWindowStyle.Minimized;
    Process p1 = Process.Start(ps1);
    System.Threading.Thread.Sleep(5000); // Allow the process to open it's window
    appWin1 = p1.MainWindowHandle;
    // Put it into this form
    SetParent(appWin1, this.panel1.Handle);
}


private void timer1_Tick(object sender, EventArgs e)
{
    Bitmap bm = new Bitmap(panel1.Width, panel1.Height);
    Graphics g = Graphics.FromImage(bm);
    g.CopyFromScreen(0, 0, 0, 0, bm.Size);
    pictureBox1.Image = bm;
    
}

private void Form5_Load(object sender, EventArgs e)
{

}

private void panel1_Paint(object sender, PaintEventArgs e)
{

}

private void timer2_Tick(object sender, EventArgs e)
{
    MoveWindow(appWin1, 0, 0, this.panel1.Width, this.panel1.Height, true);
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

运行此代码:

问题是这样的:我只想获取面板内的内容,但在我的面板前面移动的任何其他窗口都包含在 PictureBox 中显示的位图中:

[}3

如图所示,当外部页面放在panel1 上时,它也会出现在pictureBox1 中(图中的红框)。
我只希望记事本出现在pictureBox1 中,无论哪个窗口悬停在panel1 上。

【问题讨论】:

    标签: c# winforms bitmap panel picturebox


    【解决方案1】:

    由于您想将 Panel 的内容呈现到 Bitmap - 它托管外部应用程序 - 您不能使用 Control.DrawToBitmap():托管应用程序的内容,父调用 SetParent(),将不会被打印。 由于您还想从渲染中排除可能悬停在面板上的任何其他窗口,您可以使用PrintWindow 函数,传递面板父窗体的句柄。
    表单将收到WM_PRINTWM_PRINTCLIENT 消息,并将自己打印到指定的设备上下文:在这种情况下,从位图生成。

    这不是屏幕截图:窗口将自身及其内容绘制到 DC,因此其他窗口是否悬停/部分或完全重叠都无关紧要,结果是相同的。

    我正在使用DwmGetWindowAttribute,设置DWMWA_EXTENDED_FRAME_BOUNDS,来获取要打印的窗口的边界。它可能看起来冗余,但是,如果您的应用不是 DpiAware 并且您有一个高 Dpi 屏幕,那么在这种情况下它会显得较少冗余。最好有它,IMO。
    此函数比GetWindowRectGetClientRect可靠,它将在可变 DPI 上下文中返回正确的度量。

    你不能将子Window的句柄传递给这个函数,所以我使用父窗体的句柄来生成Bitmap,然后选择Panel所在的部分。这取决于:

    [ParentForm].RectangleToClient([Child].RectangleToScreen([Child].ClientRectangle))


    启动进程和父记事本的窗口到面板控件:

    private void Notepad_Button_Click(object sender, EventArgs e)
    {
        var psi = new ProcessStartInfo(@"notepad.exe") { WindowStyle = ProcessWindowStyle.Minimized };
        using (var p = Process.Start(psi)) {
            p.WaitForInputIdle();
            var handle = p.MainWindowHandle;
            if (handle != IntPtr.Zero) {
                SetParent(handle, panel1.Handle);
                MoveWindow(handle, 0, 0, panel1.Width, panel1.Height, true);
            }
        }
    }
    

    现在,当您想要将 Panel 所在的 Form 部分渲染到 Bitmap 时,调用 RenderWindow 并获取您感兴趣的部分:Panel 的 ClientRectangle。

    using (var image = RenderWindow(this.Handle, true, false)) {
        if (image is null) return;
        if (WindowState == FormWindowState.Minimized) return;
        var panelRect = RectangleToClient(panel1.RectangleToScreen(panel1.ClientRectangle));
        pictureBox1.Image?.Dispose();
        pictureBox1.Image = image.Clone(panelRect, PixelFormat.Format32bppArgb);
    }
    
    // [...]
    
    public static Bitmap RenderWindow(IntPtr hWnd, bool clientAreaOnly, bool tryGetFullContent = false)
    {
        var printOption = clientAreaOnly ? PrintWindowOptions.PW_CLIENTONLY : PrintWindowOptions.PW_DEFAULT;
        printOption = tryGetFullContent ? PrintWindowOptions.PW_RENDERFULLCONTENT : printOption;
        var hResult = DwmGetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out Rectangle dwmRect, Marshal.SizeOf<Rectangle>());
        if (hResult < 0) {
            Marshal.ThrowExceptionForHR(hResult);
            return null;
        }
    
        if (dwmRect.Width <= 0 || dwmRect.Height <= 0) return null;
    
        var bmp = new Bitmap(dwmRect.Width, dwmRect.Height);
        using (var g = Graphics.FromImage(bmp)) {
            var hDC = g.GetHdc();
            try {
                var success = PrintWindow(hWnd, hDC, printOption);
                if (!success) {
                    // Failed, see what happened
                    var win32Error = Marshal.GetLastWin32Error();
                    return null;
                }
                return bmp;
            }
            finally {
                g.ReleaseHdc(hDC);
            }
        }
    }
    

    Win32 声明

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
    
    public enum PrintWindowOptions : uint
    {
        PW_DEFAULT = 0,
        PW_CLIENTONLY = 1,
        PW_RENDERFULLCONTENT = 2  // Undocumented. Use, e.g., with a WebBrowser
    }
    
    [DllImport("user32.dll", SetLastError = true)]
    static extern internal bool PrintWindow(IntPtr hwnd, IntPtr hDC, PrintWindowOptions nFlags);
    
    public enum DWMWINDOWATTRIBUTE : uint
    {
        DWMWA_NCRENDERING_ENABLED = 1,      // [get] Is non-client rendering enabled/disabled
        DWMWA_NCRENDERING_POLICY,           // [set] DWMNCRENDERINGPOLICY - Non-client rendering policy - Enable or disable non-client rendering
        DWMWA_TRANSITIONS_FORCEDISABLED,    // [set] Potentially enable/forcibly disable transitions
        DWMWA_ALLOW_NCPAINT,                // [set] Allow contents rendered In the non-client area To be visible On the DWM-drawn frame.
        DWMWA_CAPTION_BUTTON_BOUNDS,        // [get] Bounds Of the caption button area In window-relative space.
        DWMWA_NONCLIENT_RTL_LAYOUT,         // [set] Is non-client content RTL mirrored
        DWMWA_FORCE_ICONIC_REPRESENTATION,  // [set] Force this window To display iconic thumbnails.
        DWMWA_FLIP3D_POLICY,                // [set] Designates how Flip3D will treat the window.
        DWMWA_EXTENDED_FRAME_BOUNDS,        // [get] Gets the extended frame bounds rectangle In screen space
        DWMWA_HAS_ICONIC_BITMAP,            // [set] Indicates an available bitmap When there Is no better thumbnail representation.
        DWMWA_DISALLOW_PEEK,                // [set] Don't invoke Peek on the window.
        DWMWA_EXCLUDED_FROM_PEEK,           // [set] LivePreview exclusion information
        DWMWA_CLOAK,                        // [set] Cloak Or uncloak the window
        DWMWA_CLOAKED,                      // [get] Gets the cloaked state Of the window. Returns a DWMCLOACKEDREASON object
        DWMWA_FREEZE_REPRESENTATION,        // [set] BOOL, Force this window To freeze the thumbnail without live update
        PlaceHolder1,
        PlaceHolder2,
        PlaceHolder3,
        DWMWA_ACCENTPOLICY = 19
    }
    
    [DllImport("dwmapi.dll", SetLastError = true)]
    static extern internal int DwmGetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, out Rectangle pvAttribute, int cbAttribute);
    

    【讨论】:

    • 这太棒了。它有效。谢谢。只有一个问题:当我最小化表单时,它会在这一行出现错误:'var bmp = new Bitmap(dwmRect.Width, dwmRect.Height);'。除了停止进程之外,您还有其他解决方案吗?
    • 你在用那个定时器来生成位图吗?如果是这样,则在窗体最小化和销毁(关闭)之前停止计时器。或者只是检查您将要生成的位图的大小——您真的需要计时器吗?
    • 我添加了一些与表单大小相关的检查,包括RenderWindow() 和调用它的代码。更新您的代码。
    • panel 和 PictureBox 必须始终如一,就像一面镜子。这就是我使用计时器的原因。有不同的解决方案吗?否则,我会在屏幕最小化时停止计时器。
    • 好的。无论如何,我所做的更改应该可以处理。你更新了你这边的代码吗?
    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    相关资源
    最近更新 更多