0

我想要淡化面板控件和其中的控件/对象的效果。我发现一些具有 opacity 属性的容器只会改变背景颜色的不透明度。

所以我想出了自己的解决方案。我创建了另一个包含我需要的控件的表单,我得到了我想要的,但是我在定位创建的新表单时遇到了一些问题。我不知道如何获取其父窗体的工作区来设置初始位置。我对工作区的意思是,它不包括控制框/标题栏。不同的操作系统有不同的标题栏大小(据我所知)所以我需要正确调整它

Form2,持有不透明控件的那个

Public Class Form2

    Private Const dif As Integer = 23

    Private Const GWL_EXSTYLE As Integer = (-20)
    Private Const WS_EX_TRANSPARENT As Integer = &H20

    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer

    Public Sub resizeFrom(ByVal parent As Form)
        Me.Height = parent.Height - dif
        Me.Width = parent.Width
        Me.Top = (parent.Top + ((parent.Height - Me.Height) / 2) + (dif / 2))
        Me.Left = parent.Left + ((parent.Width - Me.Width) / 2)
    End Sub

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TopMost = True
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    Me.BackColor = Color.Red

    SetWindowLong(Me.Handle, GWL_EXSTYLE, GetWindowLong(Me.Handle, GWL_EXSTYLE) Or WS_EX_TRANSPARENT)
    End Sub
End Class

父/主窗体

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Show()
        Form2.Show()
        Form2.resizeFrom(Me)
    End Sub

    Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Form2.resizeFrom(Me)
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Form1_Move(Me, New EventArgs)
    End Sub

    Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        Form2.Opacity = HScrollBar1.Value / 100
        Me.Text = "Opacity: " & HScrollBar1.Value & "%"
    End Sub
End Class

它应该看起来像这样 http://i48.tinypic.com/25qubk0.jpg

不透明表单用于显示目的,它被设置为“Ghost Like Form”或“Click-through Form”

有什么解决方案可以让我获得表格的正确工作区吗?

请帮忙。谢谢

4

1 回答 1

0

你需要的是绝对位置ClientRectangle。这个位置是,(0, 0)但您可以神奇地将其转换为屏幕值:

Form2.Location = PointToScreen(Me.ClientRectangle.Location)
Form2.Size = Me.ClientRectangle.Size
于 2013-02-02T15:49:48.210 回答