使vb.net的边框形式半透明

时间:2011-01-08 05:38:31

标签: vb.net transparent

有没有办法将vb.net表单嵌入到另一个vb.net表单中。我想要做的是使Form-A半透明,Form-B作为嵌入式主窗体。因此最终的应用程序周围有一个半透明的边框。另外,我不想使用MDI表格。

编辑:如何在不使用MDI表单的情况下使vb.net表单的边框变为半透明。

2 个答案:

答案 0 :(得分:2)

你应该检查一下。它在C#中,但您可以尝试获得翻译帮助。 Extending Form with Non-Client Area Painting。这是我的头脑,因为你是新手,可能会非常棘手。

首先,由于你没有指定,我假设你使用的是winforms而不是WPf或Asp.net,我接下来都没有经验。 我有一个方法只是弄乱了一堆winforms属性。我的winxp,盒子有点小问题,但它的工作原理:)。这是两个表单的代码,我在代码中包含了属性以简化操作。

主要表格

Public Class frmMAin
    Dim border As Form = New frmBackground()

    Private Sub frmMAin_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        RefreshBorder()
    End Sub
    Private Sub frmMAin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshBorder()
    End Sub

    Private Sub RefreshBorder()
        border.Show()


        border.Size = New Size(Me.Width + 20, Me.Height + 20)
        border.Location = New Point(Me.Location.X - 10, Me.Location.Y - 10)
    End Sub

    Private Sub frmMAin_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus
        border.Hide()
    End Sub

    Private Sub frmMAin_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        RefreshBorder()
    End Sub
End Class

背景表格

Public Class frmBackground

    Private Sub frmBackground_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Me.ShowInTaskbar = False
        Me.BackColor = Color.Blue
        Me.Opacity = 0.5
    End Sub
End Class

事实证明是这样的。 alt text

此代码仍有一些焦点问题,但大多数功能正常。另外请记住,这不是解决这个问题的最“优雅”的方法,最有可能用一些系统dll做更好的方法。你也可以试试WPF,因为我听说你对winforms有更多的控制。

答案 1 :(得分:2)

我已经简化了一些代码并将大多数事件连接到单个方法。不再是Form-A,只是Form-B。 Form-B现在可以动态创建自己的Form-A,而无需实际制作代码文件。我将边框大小移动到一个变量,因此很容易调整它。

Imports System.Runtime.InteropServices

Public Class InnerForm
    Private borderSize As Integer = 10
    Private border As Form = New Form()

    Private Sub InnerForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        border.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        border.ShowInTaskbar = False
        border.BackColor = Color.Blue
        border.Opacity = 0.5
        border.Enabled = False

        RefreshBorder()
    End Sub

Private Sub DrawRectangle()
    Dim p As New Drawing2D.GraphicsPath()
    p.StartFigure()
    p.AddArc(New Rectangle(0, 0, 40, 40), 180, 90)
    p.AddLine(40, 0, border.Width - 40, 0)
    p.AddArc(New Rectangle(border.Width - 40, 0, 40, 40), -90, 90)
    p.AddLine(border.Width, 40, border.Width, border.Height - 40)
    p.AddArc(New Rectangle(border.Width - 40, border.Height - 40, 40, 40), 0, 90)
    p.AddLine(border.Width - 40, border.Height, 40, border.Height)
    p.AddArc(New Rectangle(0, border.Height - 40, 40, 40), 90, 90)
    p.CloseFigure()
    border.Region = New Region(p)
End Sub

Private Sub RefreshBorder()
    border.Show()
    border.Size = New Size(Me.Width + borderSize * 2, Me.Height + borderSize * 2)
    border.Location = New Point(Me.Location.X - borderSize, Me.Location.Y - borderSize)
    DrawRectangle()
    SetWindowPos(border.Handle, Me.Handle, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOACTIVATE)

    Me.BringToFront()
End Sub


    Private Sub frmMAin_Refresh(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles Me.GotFocus, Me.Move, Me.Activated, Me.SizeChanged, MyBase.Shown

        'dont show when maximized or minimized, else show it'
        If Me.WindowState = FormWindowState.Maximized Or Me.WindowState = FormWindowState.Minimized Then
            border.Hide()
        Else
            RefreshBorder()
        End If
    End Sub

    <DllImport("user32.dll")> _
    Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
    End Function

    Public Const SWP_NOSIZE As Int32 = &H1
    Public Const SWP_NOMOVE As Int32 = &H2
    Public Const SWP_NOACTIVATE As Int32 = &H10
End Class

请告诉我这是否适合您。