本文介绍了如何使用Control.DrawToBitmap将窗体呈现为不带有修饰符(标题栏,边框)的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Form,并且在其中有一个Overlay控件(透明的灰色底色,在"Drop here ..."和一个图标上带有白色文本)仅在将文件拖到Form上时才可见.通过在其背面上绘制控件,然后用透明灰色(ARGB)填充,可以使叠加层变得透明.当Overlay应该位于不是Form的控件上时,该方法效果很好,但是当我使用Control.DrawToBitmap呈现Form(而不是通常的Control)时,它也会呈现标题栏和边框.

I have a Form and in it an Overlay control (transparent gray backcolor with White text over "Drop here..." and an icon) that is visible only when a file is dragged over the Form. The Overlay is made transparent by drawing the control in its back on it and then filling over with transparent gray (ARGB). The method Works very well when the Overlay should be over a Control that is not a Form, but when I use Control.DrawToBitmap to render a Form, not an usual Control, it also renders the title bar and border.

推荐答案

Form.DrawToBitmap绘制包括非客户区域在内的整个表单.您可以使用 BitBlt . BitBlt函数执行与像素矩形相对应的颜色数据从指定的源设备上下文到目标设备上下文的位块传输.

Form.DrawToBitmap draws the whole form including non-client area. You can use BitBlt. The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.

const int SRCCOPY = 0xCC0020;
[DllImport("gdi32.dll")]
static extern int BitBlt(IntPtr hdc, int x, int y, int cx, int cy,
    IntPtr hdcSrc, int x1, int y1, int rop);

Image PrintClientRectangleToImage()
{
    var bmp = new Bitmap(ClientSize.Width, ClientSize.Height);
    using (var bmpGraphics = Graphics.FromImage(bmp))
    {
        var bmpDC = bmpGraphics.GetHdc();
        using (Graphics formGraphics = Graphics.FromHwnd(this.Handle))
        {
            var formDC = formGraphics.GetHdc();
            BitBlt(bmpDC, 0, 0, ClientSize.Width, ClientSize.Height, formDC, 0, 0, SRCCOPY);
            formGraphics.ReleaseHdc(formDC);
        }
        bmpGraphics.ReleaseHdc(bmpDC);
    }
    return bmp;
}

这篇关于如何使用Control.DrawToBitmap将窗体呈现为不带有修饰符(标题栏,边框)的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 16:53