1313using Windows . Win32 . Graphics . Direct3D11 ;
1414using Windows . Win32 . Graphics . DirectComposition ;
1515using Windows . Win32 . Graphics . Dwm ;
16+ using Windows . Win32 . Graphics . Dxgi ;
1617using Windows . Win32 . UI . Shell ;
1718using Windows . Win32 . UI . WindowsAndMessaging ;
1819using WinRT ;
@@ -71,34 +72,17 @@ public void SizeChanged(RECT size)
7172 if ( hwnd != HWND . Null )
7273 PInvoke . SetWindowPos ( hwnd , ( HWND ) 0 , size . left , size . top , size . Width , size . Height , SET_WINDOW_POS_FLAGS . SWP_NOACTIVATE ) ;
7374
74- if ( currentHandler != null )
75- currentHandler . ResetBounds ( new ( 0 , 0 , size . Width , size . Height ) ) ;
75+ currentHandler ? . ResetBounds ( new ( 0 , 0 , size . Width , size . Height ) ) ;
7676
7777 if ( outputLink is not null )
7878 outputLink . PlacementVisual . Size = new ( size . Width , size . Height ) ;
7979 }
8080
8181 private unsafe LRESULT WndProc ( HWND hwnd , uint msg , WPARAM wParam , LPARAM lParam )
8282 {
83- //if (msg == 0x0081 /*WM_NCCREATE*/)
84- //{
85- // try
86- // {
87- // var cp = Marshal.PtrToStructure<CREATESTRUCTW>(lParam).lpCreateParams;
88- // var pCreateParams = new nint(cp);
89- // if (pCreateParams != nint.Zero && GCHandle.FromIntPtr(pCreateParams).Target is IWindowInit wnd)
90- // return wnd.InitWndProcOnNCCreate(
91- // hwnd,
92- // msg,
93- // Marshal.GetFunctionPointerForDelegate(wndProc ?? throw new NullReferenceException()),
94- // lParam);
95- // }
96- // catch { }
97- //}
98- //else
9983 if ( msg == 0x0001 /*WM_CREATE*/ )
10084 {
101- var clsid = FindPreviewHandlerFor ( Item . FileExtension , hwnd . DangerousGetHandle ( ) ) ;
85+ var clsid = FindPreviewHandlerFor ( Item . FileExtension , hwnd ) ;
10286
10387 isOfficePreview = new Guid ? [ ]
10488 {
@@ -139,10 +123,13 @@ public unsafe void LoadPreview(UIElement presenter)
139123
140124 fixed ( char * pszClassName = szClassName )
141125 {
126+ var pWindProc = Marshal . GetFunctionPointerForDelegate ( WndProc ) ;
127+ var pfnWndProc = ( delegate * unmanaged[ Stdcall] < HWND , uint , WPARAM , LPARAM , LRESULT > ) pWindProc ;
128+
142129 wCls = new WNDCLASSEXW
143130 {
144131 cbSize = ( uint ) Marshal . SizeOf ( typeof ( WNDCLASSEXW ) ) ,
145- lpfnWndProc = new ( WndProc ) ,
132+ lpfnWndProc = pfnWndProc ,
146133 hInstance = hInst ,
147134 lpszClassName = pszClassName ,
148135 style = 0 ,
@@ -159,7 +146,7 @@ public unsafe void LoadPreview(UIElement presenter)
159146 {
160147 hwnd = PInvoke . CreateWindowEx (
161148 WINDOW_EX_STYLE . WS_EX_LAYERED | WINDOW_EX_STYLE . WS_EX_COMPOSITED ,
162- wCls . Value . lpszClassName ,
149+ pszClassName ,
163150 pszWindowName ,
164151 WINDOW_STYLE . WS_CHILD | WINDOW_STYLE . WS_CLIPSIBLINGS | WINDOW_STYLE . WS_VISIBLE ,
165152 0 , 0 , 0 , 0 ,
@@ -213,7 +200,7 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
213200 IUnknown * pControlSurface = default ;
214201
215202 pDCompositionDevice ->CreateVisual ( & pChildVisual ) ;
216- pDCompositionDevice ->CreateSurfaceFromHwnd ( new ( hwnd . DangerousGetHandle ( ) ) , & pControlSurface ) ;
203+ pDCompositionDevice ->CreateSurfaceFromHwnd ( hwnd , & pControlSurface ) ;
217204 pChildVisual ->SetContent ( pControlSurface ) ;
218205 if ( pChildVisual is null || pControlSurface is null )
219206 return false ;
@@ -251,8 +238,8 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
251238
252239 public void UnloadPreview ( )
253240 {
254- if ( hwnd != HWND . NULL )
255- DestroyWindow ( hwnd ) ;
241+ if ( hwnd != HWND . Null )
242+ PInvoke . DestroyWindow ( hwnd ) ;
256243
257244 //outputLink?.Dispose();
258245 outputLink = null ;
0 commit comments