Resolved VS2008 SetParent and Opacity

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I've recently gotten over the hurdle of a Borderless form that doesn't show in the Taskbar to not be hidden when the user clicks the Show Desktop button in windows by using the SetParent API to make the windows all children of the desktop, but now I can't change the Opacity of the form, it throws an exception:
VB.NET:
System.ComponentModel.Win32Exception was unhandled
  ErrorCode=-2147467259
  Message="The parameter is incorrect"
  NativeErrorCode=87
  Source="System.Windows.Forms"
  StackTrace:
       at System.Windows.Forms.Form.UpdateLayered()
       at System.Windows.Forms.Form.set_Opacity(Double value)
       at ReminderNotes.NoteBase.set_Opacity(Double value) in C:\Users\IBM_ADMIN\Desktop\Program Stuffs\ReminderNotes\ReminderNotes\NoteBase.vb:line 255
       at ReminderNotes.NoteForm.TransparencyToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\IBM_ADMIN\Desktop\Program Stuffs\ReminderNotes\ReminderNotes\NoteForm.vb:line 254
       at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
       at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
       at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ToolStrip.WndProc(Message& m)
       at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at ReminderNotes.MainModule.Main() in C:\Users\IBM_ADMIN\Desktop\Program Stuffs\ReminderNotes\ReminderNotes\MainApp.vb:line 62
  InnerException:
Here's the code I have:
VB.NET:
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll")> _
    Public Shared Function SetParent(ByVal child As IntPtr, ByVal parent As IntPtr) As Integer
    End Function

    Public Sub New()
        MyBase.New()
        MyBase.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        MyBase.ShowInTaskbar = False
        MyBase.Size = New Size(180I, 165I)
        MyBase.MinimumSize = New Size(180I, 120I)
        MyBase.AutoScroll = True
        SetParent(Me.Handle, FindWindow("progman", Microsoft.VisualBasic.vbNullString))
    End Sub

    Private Sub TransparencyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Opacity values are stored as "1", "0.9", etc in the tag property
        Me.Opacity = CDbl(CType(sender, ToolStripMenuItem).Tag.ToString)
    End Sub
Any ideas on how to keep the windows children of the desktop but still allow the opacity to be something other than 1.0?
 
Opacity requires a layered window, which a child window cannot be.
 
Don't suppose you'd know of a work around to avoid the Show Desktop minimizing & still allow the opacity to be changed would ya?
 
No. I did see the WM_WINDOWPOSCHANGING message with SWP_HIDEWINDOW flag, but it didn't appear to arrive until window was restored.
During research I found it discussed using a global hook and listen for EVENT_SYSTEM_FOREGROUND and check which window gets foreground.
 
I ended up providing a menu item that loops through the Note's collection and calls the BringToFront() method, perhaps someday I'll get around to having it automatically do that whenever the Show Desktop operation has completed or something.
 
Back
Top