Blurred Background for focus

  • Thread starter Thread starter NachoShaw
  • Start date Start date
N

NachoShaw

Guest
Hey

I have been playing with this piece of code to get a reasonable background blur behind my main form to give a bit of focus while its in use but im having some issues. I was wondering if anyone had something similar that they could share?

The problems i an experiencing:

  1. the blur form appears on the opposite screen to my form
  2. its not maximized, its off center between the 2 screens about 1/2in into my other screen with a 1/4in gap at the top
  3. its clickable. i can click through it to any underlying window
  4. periodically crashes ob build

here is the code i have been using

Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Runtime.CompilerServices

Namespace WFWindows
Partial Public Class BackgroundBlur
Inherits Form

Public Sub New()
Me.EnableBlur()
SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
BackColor = Color.LimeGreen
TransparencyKey = Color.LimeGreen
' InitializeComponent()
FormBorderStyle = FormBorderStyle.None
WindowState = FormWindowState.Maximized
ControlBox = False
TopMost = False
StartPosition = FormStartPosition.CenterScreen
Screen.FromControl(FrmModelLoader)

End Sub
End Class

Module WindowExtension
<DllImport("user32.dll")>
Friend Function SetWindowCompositionAttribute(ByVal hwnd As IntPtr, ByRef data As WindowCompositionAttributeData) As Integer

End Function

<Extension()>
Sub EnableBlur(ByVal this As Form)
Dim accent = New AccentPolicy()
accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND
Dim accentStructSize = Marshal.SizeOf(accent)
Dim accentPtr = Marshal.AllocHGlobal(accentStructSize)
Marshal.StructureToPtr(accent, accentPtr, False)
Dim Data = New WindowCompositionAttributeData()
Data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY
Data.SizeOfData = accentStructSize
Data.Data = accentPtr
SetWindowCompositionAttribute(this.Handle, Data)
Marshal.FreeHGlobal(accentPtr)
End Sub
End Module

Enum AccentState
ACCENT_DISABLED = 0
ACCENT_ENABLE_GRADIENT = 1
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2
ACCENT_ENABLE_BLURBEHIND = 3
ACCENT_INVALID_STATE = 4
End Enum

Structure AccentPolicy
Public AccentState As AccentState
Public AccentFlags As Integer
Public GradientColor As Integer
Public AnimationId As Integer
End Structure

Structure WindowCompositionAttributeData
Public Attribute As WindowCompositionAttribute
Public Data As IntPtr
Public SizeOfData As Integer
End Structure

Enum WindowCompositionAttribute
WCA_ACCENT_POLICY = 19
End Enum
End Namespace



This is how i have called in in my main form

WFWindows.BackgroundBlur.Show()

i also tried this to load on the same screen as my project form

Screen.FromControl(frmMain)

Any help / guidance greatly appreciated


1579658.jpg


Thanks



Im a self taught VB.Net guy who writes code for Autodesk Inventor. I may not know the terminology but i try so please be patient. Im not a kid so please dont treat me like one :)

Continue reading...
 
Back
Top