Search code examples
vb.netwinformsopacitybounds

VB.NET | How to get Windows Form WorkingArea, not Desktop WorkingArea


I wanted an effect of fading a panel control and the controls / object inside it. I found some containers with opacity property it only changes the background color's opacity.

So I came up my own solution. I created another form containing the controls I need and I got what I want but I got some problems with positioning the new form created. I don't know how to get the working area of its parent form to set the initial position. What I mean for working area is that, it won't include the control box / title bar. Different OS has different title bar sizes (as far as I know) so I need to adjust it correctly

Form2, the one holding the Opaque Controls

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

The Parent/Main Form

    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

It should look like this http://i48.tinypic.com/25qubk0.jpg

Opaque Form is for display purposes, it was set to "Ghost Like Form" or "Click-through Form"

Is there any solution so I can get the proper WorkingArea of the Form?

Please help. Thanks


Solution

  • What you need is the absolute location of the ClientRectangle. The Location of this is (0, 0) but you can magically convert it to screen values:

    Form2.Location = PointToScreen(Me.ClientRectangle.Location)
    Form2.Size = Me.ClientRectangle.Size