当父窗体具有边框vb.net时,在自定义控件中使用DrawToBitmap不起作用

时间:2013-03-28 21:21:05

标签: .net custom-controls transparency drawtobitmap

好的,所以......我正在尝试创建一个自定义复选框,实际上根据字体大小调整字形大小(从不执行此操作,请转到MS)。为了正确地执行此操作,我必须完全覆盖控件中的OnPaint事件。但是,当控件设置为透明时,我遇到了问题。

当Parent控件(我现在只做一层透明)时没有边框(特别是一个设置为FormBorderStyle = None的窗体) 它工作得很好。但是,如果我将它设置为任何但是,坐标关闭并抓住我的位图的错误部分。我已经在代码下方发布了屏幕截图链接,以展示其差异。我无法弄清楚为什么它不能正确定位边框。我认为这与客户规模有关,但无法做到正确。另外,使用'drawtobitmap'函数OnPaint会自行调试ALOT,并减慢绘制控件的速度,无论如何都要阻止它?

以下是相关代码

     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    OnPaintBackground(e)
    If MyBase.Appearance = Appearance.Button Then
        MyBase.OnPaint(e)
    Else

    End If
End Sub
Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)

    If BackColor = Color.Transparent Then
        Dim ctl As Control = Parent
        Dim bmp As New Bitmap(ctl.ClientSize.Width, ctl.ClientSize.Height)
        ctl.DrawToBitmap(bmp, New Rectangle(0, 0, ctl.ClientSize.Width, ctl.ClientSize.Height))


        Dim rect As New Rectangle((ctl.Size.Width - ctl.ClientSize.Width) + Left, (ctl.Size.Height - ctl.ClientSize.Height) + Top, ClientSize.Width, ClientSize.Height)

        e.Graphics.FillRectangle(New TextureBrush(CropImage(bmp, rect)), ClientRectangle)
        ctl.Text = ((ctl.Size.Width - ctl.ClientSize.Width) + Left).ToString + " " + ((ctl.Size.Height - ctl.ClientSize.Height) + Top).ToString + " " + ClientSize.Width.ToString + " " + ClientSize.Height.ToString
        bmp.Dispose()
    Else
        e.Graphics.FillRectangle(New SolidBrush(MyBase.BackColor), ClientRectangle)
    End If

End Sub
Private Function CropImage(ByVal source As Image, ByVal crop As Rectangle) As Bitmap
    Dim bmp = New Bitmap(crop.Width, crop.Height)
    Using gr = Graphics.FromImage(bmp)
        gr.DrawImage(source, New Rectangle(0, 0, bmp.Width, bmp.Height), crop, GraphicsUnit.Pixel)
    End Using
    Return bmp
End Function

with borders

without borders

0 个答案:

没有答案