Browse Source

添加修改系统壁纸功能,添加Bing壁纸Web接口

yuzhengyang 6 years ago
parent
commit
2e5c613e4d

+ 3 - 1
Azylee.Utils/Azylee.Core/Azylee.Core.csproj

@@ -157,8 +157,10 @@
     <Compile Include="WindowsUtils\APIUtils\ExplorerAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\ExplorerAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\PermissionAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\PermissionAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\SystemSleepAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\SystemSleepAPI.cs" />
+    <Compile Include="WindowsUtils\APIUtils\WallpaperUtils\WallpaperTool.cs" />
     <Compile Include="WindowsUtils\APIUtils\WindowsAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\WindowsAPI.cs" />
-    <Compile Include="WindowsUtils\APIUtils\WindowsDrawerAPI.cs" />
+    <Compile Include="WindowsUtils\APIUtils\WinDrawUtils\WinDrawAPI.cs" />
+    <Compile Include="WindowsUtils\APIUtils\WinDrawUtils\WinDrawTool.cs" />
     <Compile Include="WindowsUtils\APIUtils\WindowsHotKeyAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\WindowsHotKeyAPI.cs" />
     <Compile Include="WindowsUtils\BrowserUtils\BrowserSelector.cs" />
     <Compile Include="WindowsUtils\BrowserUtils\BrowserSelector.cs" />
     <Compile Include="WindowsUtils\ClipboardUtils\ClipboardTool.cs" />
     <Compile Include="WindowsUtils\ClipboardUtils\ClipboardTool.cs" />

+ 56 - 0
Azylee.Utils/Azylee.Core/WindowsUtils/APIUtils/WallpaperUtils/WallpaperTool.cs

@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+
+namespace Azylee.Core.WindowsUtils.APIUtils.WallpaperUtils
+{
+    /// <summary>
+    /// 系统桌面壁纸工具类
+    /// </summary>
+    public static class WallpaperTool
+    {
+        #region 获取windows桌面背景        
+        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+        public static extern int SystemParametersInfo(int uAction, int uParam, StringBuilder lpvParam, int fuWinIni);
+        private const int SPI_GETDESKWALLPAPER = 0x0073;
+        #endregion
+        #region 设置windows桌面背景
+        [DllImport("user32.dll")]
+        private static extern bool SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
+        #endregion
+
+        /// <summary>
+        /// 获取当前桌面壁纸路径
+        /// </summary>
+        /// <returns></returns>
+        public static string Get()
+        {
+            try
+            {
+                //定义存储缓冲区大小
+                StringBuilder s = new StringBuilder(300);
+                //获取Window 桌面背景图片地址,使用缓冲区
+                SystemParametersInfo(SPI_GETDESKWALLPAPER, 300, s, 0);
+                //缓冲区中字符进行转换
+                return s.ToString(); //系统桌面背景图片路径
+            }
+            catch { return null; }
+        }
+        /// <summary>
+        /// 设置当前桌面背景
+        /// </summary>
+        /// <param name="path"></param>
+        /// <returns></returns>
+        public static bool Set(string path)
+        {
+            try
+            {
+                SystemParametersInfo(20, 0, path, 0x01 | 0x02);
+                return true;
+            }
+            catch { return false; }
+        }
+    }
+}

+ 2 - 4
Azylee.Utils/Azylee.Core/WindowsUtils/APIUtils/WindowsDrawerAPI.cs

@@ -1,14 +1,12 @@
 //DrawBehindDesktopIcons
 //DrawBehindDesktopIcons
 
 
 using System;
 using System;
-using System.Collections.Generic;
-using System.Linq;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Text;
 
 
-namespace Azylee.Core.WindowsUtils.APIUtils
+namespace Azylee.Core.WindowsUtils.APIUtils.WinDrawUtils
 {
 {
-    public class WindowsDrawerAPI
+    public class WindowsDrawAPI
     {
     {
         public const int SM_CXSCREEN = 0;
         public const int SM_CXSCREEN = 0;
 
 

+ 41 - 0
Azylee.Utils/Azylee.Core/WindowsUtils/APIUtils/WinDrawUtils/WinDrawTool.cs

@@ -0,0 +1,41 @@
+using System;
+using System.Drawing;
+
+namespace Azylee.Core.WindowsUtils.APIUtils.WinDrawUtils
+{
+    /// <summary>
+    /// 桌面绘制工具
+    /// </summary>
+    public static class WindowsDrawTool
+    {
+        /// <summary>
+        /// 将图片绘制到桌面上
+        /// </summary>
+        /// <param name="image"></param>
+        /// <param name="x"></param>
+        /// <param name="y"></param>
+        /// <param name="width"></param>
+        /// <param name="height"></param>
+        public static void Paint(Image image, int x, int y, int width, int height)
+        {
+            IntPtr workerw = IntPtr.Zero;
+            WindowsDrawAPI.EnumWindows(new WindowsDrawAPI.EnumWindowsProc((tophandle, topparamhandle) =>
+            {
+                IntPtr p = WindowsDrawAPI.FindWindowEx(tophandle, IntPtr.Zero, "SHELLDLL_DefView", IntPtr.Zero);
+                if (p != IntPtr.Zero) workerw = WindowsDrawAPI.FindWindowEx(IntPtr.Zero, tophandle, "WorkerW", IntPtr.Zero);
+
+                return true;
+            }), IntPtr.Zero);
+
+            IntPtr dc = WindowsDrawAPI.GetDCEx(workerw, IntPtr.Zero, (WindowsDrawAPI.DeviceContextValues)0x403);
+            if (dc != IntPtr.Zero)
+            {
+                using (Graphics g = Graphics.FromHdc(dc))
+                {
+                    g.DrawImage(image, x, y, width, height);
+                }
+                WindowsDrawAPI.ReleaseDC(workerw, dc);
+            }
+        }
+    }
+}

+ 4 - 1
Azylee.Utils/Azylee.YeahWeb/Azylee.YeahWeb.csproj

@@ -50,6 +50,8 @@
     <Compile Include="BaiDuWebAPI\IPLocationAPI\IPLocationWebModel.cs" />
     <Compile Include="BaiDuWebAPI\IPLocationAPI\IPLocationWebModel.cs" />
     <Compile Include="BaiDuWebAPI\IPLocationAPI\IPLocationTool.cs" />
     <Compile Include="BaiDuWebAPI\IPLocationAPI\IPLocationTool.cs" />
     <Compile Include="EmailUtils\EmailTool.cs" />
     <Compile Include="EmailUtils\EmailTool.cs" />
+    <Compile Include="ExtWebAPI\BingWebAPI\WallpaperUtils\WallpaperModel.cs" />
+    <Compile Include="ExtWebAPI\BingWebAPI\WallpaperUtils\WallpaperTool.cs" />
     <Compile Include="FTPUtils\FTPTool.cs" />
     <Compile Include="FTPUtils\FTPTool.cs" />
     <Compile Include="HttpUtils\MethodUtils\ExtendUtils\HeaderTool.cs" />
     <Compile Include="HttpUtils\MethodUtils\ExtendUtils\HeaderTool.cs" />
     <Compile Include="HttpUtils\MethodUtils\GetUtils\GetToolPlus.cs" />
     <Compile Include="HttpUtils\MethodUtils\GetUtils\GetToolPlus.cs" />
@@ -68,7 +70,7 @@
     <Compile Include="SocketUtils\TcpUtils\TcppServer.cs" />
     <Compile Include="SocketUtils\TcpUtils\TcppServer.cs" />
     <Compile Include="SocketUtils\TcpUtils\TcpDelegate.cs" />
     <Compile Include="SocketUtils\TcpUtils\TcpDelegate.cs" />
     <Compile Include="SocketUtils\TcpUtils\TcpStreamHelper.cs" />
     <Compile Include="SocketUtils\TcpUtils\TcpStreamHelper.cs" />
-    <Compile Include="TencentWebAPI\PictureAI\PictureScener.cs" />
+    <Compile Include="ExtWebAPI\TencentWebAPI\PictureAI\PictureScener.cs" />
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\Azylee.Core\Azylee.Core.csproj">
     <ProjectReference Include="..\Azylee.Core\Azylee.Core.csproj">
@@ -83,5 +85,6 @@
   <ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />
     <None Include="packages.config" />
   </ItemGroup>
   </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>
 </Project>

+ 106 - 0
Azylee.Utils/Azylee.YeahWeb/ExtWebAPI/BingWebAPI/WallpaperUtils/WallpaperModel.cs

@@ -0,0 +1,106 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.YeahWeb.ExtWebAPI.BingWebAPI.WallpaperUtils
+{
+    public class WallpaperModel
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public List<ImagesItem> images { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public Tooltips tooltips { get; set; }
+    }
+    public class ImagesItem
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public string startdate { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string fullstartdate { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string enddate { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string url { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string urlbase { get; set; }
+        /// <summary>
+        /// 黄昏时佩吉海湾的灯塔,加拿大新斯科舍省 (© Darwin Wiggett/Offset)
+        /// </summary>
+        public string copyright { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string copyrightlink { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string title { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string quiz { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string wp { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string hsh { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public int drk { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public int top { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public int bot { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public List<string> hs { get; set; }
+    }
+
+    public class Tooltips
+    {
+        /// <summary>
+        /// 正在加载...
+        /// </summary>
+        public string loading { get; set; }
+        /// <summary>
+        /// 上一个图像
+        /// </summary>
+        public string previous { get; set; }
+        /// <summary>
+        /// 下一个图像
+        /// </summary>
+        public string next { get; set; }
+        /// <summary>
+        /// 此图片不能下载用作壁纸。
+        /// </summary>
+        public string walle { get; set; }
+        /// <summary>
+        /// 下载今日美图。仅限用作桌面壁纸。
+        /// </summary>
+        public string walls { get; set; }
+    }
+}

+ 55 - 0
Azylee.Utils/Azylee.YeahWeb/ExtWebAPI/BingWebAPI/WallpaperUtils/WallpaperTool.cs

@@ -0,0 +1,55 @@
+using Azylee.Jsons;
+using Azylee.YeahWeb.HttpUtils;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Text;
+
+namespace Azylee.YeahWeb.ExtWebAPI.BingWebAPI.WallpaperUtils
+{
+    /// <summary>
+    /// Bing壁纸工具类
+    /// </summary>
+    public static class WallpaperTool
+    {
+        // 图片尺寸信息
+        // 1920x1200   1920x1080    1366x768   1280x768    
+        // 1024x768    800x600       800x480   768x1280  
+        // 720x1280    640x480       480x800   400x240     
+        // 320x240     240x320
+        const string URL = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx={0}&n=8";
+        /// <summary>
+        /// 获取今天的壁纸
+        /// </summary>
+        /// <returns></returns>
+        public static WallpaperModel GetToday()
+        {
+            return GetSomeday(0);
+        }
+        /// <summary>
+        /// 获取昨天的壁纸
+        /// </summary>
+        /// <returns></returns>
+        public static WallpaperModel GetYesterday()
+        {
+            return GetSomeday(1);
+        }
+        /// <summary>
+        /// 获取某一天的壁纸(每天8张)
+        /// </summary>
+        /// <param name="day"></param>
+        /// <returns></returns>
+        public static WallpaperModel GetSomeday(int day)
+        {
+            WallpaperModel model = null;
+            try
+            {
+                string rs = HttpTool.Get(string.Format(URL, day));
+                model = Json.String2Object<WallpaperModel>(rs);
+                return model;
+            }
+            catch (Exception e) { return null; }
+        }
+    }
+}

+ 1 - 1
Azylee.Utils/Azylee.YeahWeb/TencentWebAPI/PictureAI/PictureScener.cs

@@ -1,7 +1,7 @@
 using Azylee.YeahWeb.HttpUtils;
 using Azylee.YeahWeb.HttpUtils;
 using System.Text;
 using System.Text;
 
 
-namespace Azylee.YeahWeb.TencentWebAPI.PictureAI
+namespace Azylee.YeahWeb.ExtWebAPI.TencentWebAPI.PictureAI
 {
 {
     public class PictureScener
     public class PictureScener
     {
     {

+ 11 - 3
Azylee.Utils/Tests/Test.ImageToolTest/Form1.cs

@@ -1,4 +1,5 @@
 using Azylee.Core.DrawingUtils.ImageUtils;
 using Azylee.Core.DrawingUtils.ImageUtils;
+using Azylee.Core.WindowsUtils.APIUtils.WallpaperUtils;
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.ComponentModel;
@@ -20,9 +21,16 @@ namespace Test.ImageToolTest
 
 
         private void Form1_Load(object sender, EventArgs e)
         private void Form1_Load(object sender, EventArgs e)
         {
         {
-            Bitmap b1 = new Bitmap(@"F:\图片压缩测试\未标题-1.jpg");
-            byte[] b1_byte = IMG.Compression(b1, 30);
-            File.WriteAllBytes(@"F:\图片压缩测试\未标题-1(Compression).jpg", b1_byte);
+            var md = Azylee.YeahWeb.ExtWebAPI.BingWebAPI.WallpaperUtils.WallpaperTool.GetToday();
+            var md2 = Azylee.YeahWeb.ExtWebAPI.BingWebAPI.WallpaperUtils.WallpaperTool.GetYesterday();
+
+            //string a = WallpaperTool.Get();
+
+            //bool b = WallpaperTool.Set(@"C:\Users\yuzhengyang\Pictures\\cc.jpg");
+
+            //Bitmap b1 = new Bitmap(@"F:\图片压缩测试\未标题-1.jpg");
+            //byte[] b1_byte = IMG.Compression(b1, 30);
+            //File.WriteAllBytes(@"F:\图片压缩测试\未标题-1(Compression).jpg", b1_byte);
         }
         }
     }
     }
 }
 }

+ 4 - 0
Azylee.Utils/Tests/Test.ImageToolTest/Test.ImageToolTest.csproj

@@ -78,6 +78,10 @@
       <Project>{88dc61fa-95f0-41b7-9d7d-ab0f3cbd169c}</Project>
       <Project>{88dc61fa-95f0-41b7-9d7d-ab0f3cbd169c}</Project>
       <Name>Azylee.Core</Name>
       <Name>Azylee.Core</Name>
     </ProjectReference>
     </ProjectReference>
+    <ProjectReference Include="..\..\Azylee.YeahWeb\Azylee.YeahWeb.csproj">
+      <Project>{ccf7a654-b442-4db1-bb3b-0f8014c3237f}</Project>
+      <Name>Azylee.YeahWeb</Name>
+    </ProjectReference>
   </ItemGroup>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>
 </Project>