11using System ;
2+ using System . Drawing ;
23using static AwesomeWallpaper . NativeConstants ;
34using static AwesomeWallpaper . NativeMethods ;
45
@@ -13,24 +14,29 @@ static class WindowUtils
1314 /// WorkerW behind the desktop icons. If it is already there, nothing
1415 /// happens.
1516 /// </summary>
16- public static void ShowAlwaysOnDesktopBehindIcons ( IntPtr hwnd )
17+ public static void SendMessageToProgman ( )
1718 {
1819 var progmanHandle = FindWindowEx ( IntPtr . Zero , IntPtr . Zero , "Progman" , null ) ;
19- SendMessage ( progmanHandle , 0x052C , 0x0000000D , 0 ) ;
20- SendMessage ( progmanHandle , 0x052C , 0x0000000D , 1 ) ;
20+ var result = 0 ;
21+ SendMessageTimeout ( progmanHandle , 0x052C , 0 , 0 , SendMessageTimeoutFlags . SMTO_NORMAL , 1000 , out result ) ;
22+ //SendMessage(progmanHandle, 0x052C, 0, 0);
23+ //SendMessage(progmanHandle, 0x052C, 0x0000000D, 0);
24+ //SendMessage(progmanHandle, 0x052C, 0x0000000D, 1);
25+ }
2126
27+ public static IntPtr GetWorkerW ( IntPtr hwnd )
28+ {
2229 var workerWHandle = IntPtr . Zero ;
2330 EnumWindows ( new EnumWindowsProc ( ( topHandle , topParamHandle ) =>
2431 {
25- IntPtr shellHandle = FindWindowEx ( topHandle , IntPtr . Zero , "SHELLDLL_DefView" , null ) ;
32+ var shellHandle = FindWindowEx ( topHandle , IntPtr . Zero , "SHELLDLL_DefView" , null ) ;
2633 if ( shellHandle != IntPtr . Zero )
2734 {
2835 workerWHandle = FindWindowEx ( IntPtr . Zero , topHandle , "WorkerW" , null ) ;
2936 }
3037 return true ;
3138 } ) , IntPtr . Zero ) ;
32- workerWHandle = workerWHandle == IntPtr . Zero ? progmanHandle : workerWHandle ;
33- SetParent ( hwnd , workerWHandle ) ;
39+ return workerWHandle ;
3440 }
3541
3642 public static void SetStyles ( IntPtr hwnd )
@@ -54,5 +60,47 @@ public static void EnableNoActive(IntPtr hwnd, bool enable)
5460 }
5561 SetWindowLong ( hwnd , GWL_EXSTYLE , exStyle ) ;
5662 }
63+
64+ public static void RefreshDesktop ( )
65+ {
66+ SystemParametersInfo ( SPI_SETDESKWALLPAPER , 0 , null , SPIF_UPDATEINIFILE ) ;
67+ }
68+
69+ public static Image CaptureWindow ( IntPtr hwnd , int x , int y , int width , int height )
70+ {
71+ // get te hDC of the target window
72+ var hdcSrc = GetWindowDC ( hwnd ) ;
73+
74+ // get the size
75+ var windowRect = new Rect ( ) ;
76+ GetWindowRect ( hwnd , out windowRect ) ;
77+
78+ // create a device context we can copy to
79+ var hdcDest = CreateCompatibleDC ( hdcSrc ) ;
80+
81+ // create a bitmap we can copy it to,
82+ // using GetDeviceCaps to get the width/height
83+ var hBitmap = CreateCompatibleBitmap ( hdcSrc , width , height ) ;
84+
85+ // select the bitmap object
86+ var hOld = SelectObject ( hdcDest , hBitmap ) ;
87+
88+ // bitblt over
89+ BitBlt ( hdcDest , 0 , 0 , width , height , hdcSrc , x , y , SRCCOPY ) ;
90+
91+ // restore selection
92+ SelectObject ( hdcDest , hOld ) ;
93+
94+ // clean up
95+ DeleteDC ( hdcDest ) ;
96+ ReleaseDC ( hwnd , hdcSrc ) ;
97+
98+ // get a .NET image object for it
99+ var image = Image . FromHbitmap ( hBitmap ) ;
100+
101+ // free up the Bitmap object
102+ DeleteObject ( hBitmap ) ;
103+ return image ;
104+ }
57105 }
58106}
0 commit comments