【小5聊】C#一键设置桌面壁纸同时叠加今天工作或备注内容到壁纸上(windows窗体篇)

【实现效果】

【简单描述】

这里主要是通过windows窗体来实现,当然你也可以通过web方式来实现,后台代码都是一样的,只是传输的数据方式不一样

为了方便,一打开电脑就知道今天的工作内容或一些备忘录信息。那么就可以这样来设置提醒自己。

【关键代码】

    public partial class SetWallpaper : Form
    {
        public SetWallpaper()
        {
            InitializeComponent();
        }
        
        private void btnSet_Click(object sender, EventArgs e)
        {
            try
            {
                string filePath = @"C:\bg.jpg"; //本地图片完成物理路径
                string newFilePath = "";

                //加载图片
                Image backgroundImage = new Bitmap(filePath);

                //文字写在图片上
                if (true)
                {
                    int i = 0;
                    string textContent = txtText.Text;//文本
                    foreach (string item in textContent.Split(new string[] { "\r\n" }, StringSplitOptions.None))
                    {
                        if (!string.IsNullOrEmpty(item))
                        {
                            int fontSize = 20; //字体大小
                            string fontFamily = "黑体"; //字体类型
                            FontStyle fontStyle = FontStyle.Regular; //字体样式
                            Color textColor = Color.White; //白色字体

                            using (Graphics g = Graphics.FromImage(backgroundImage))
                            {
                                g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
                                using (Font f = new Font(fontFamily, fontSize + 10, fontStyle))
                                {
                                    using (Brush b = new SolidBrush(textColor))
                                    {
                                        //计算字体所占的大小
                                        SizeF size = g.MeasureString(item, new Font(fontFamily, fontSize + 10, fontStyle));
                                        float x = 1180; //backgroundImage.Width - size.Width - 100; //字体在图片的x轴位置
                                        float y = 50 + size.Height * i; //字体在图片的y轴位置
                                        g.DrawString(item, f, b, x, y);
                                        i++;
                                    }
                                }
                            }
                        }
                    }

                    newFilePath = string.Format(@"D:\{0}.jpg", Guid.NewGuid().ToString());
                    backgroundImage.Save(newFilePath);
                }

                //设置壁纸
                SystemParametersInfo(20, 0, newFilePath, 2);

                MessageBox.Show("壁纸设置完成,回到桌面查看效果!");
            }
            catch(Exception ex)
            {
                MessageBox.Show("壁纸设置出错:" + ex.Message);
            }
        }

        /// <summary>
        /// Windows系统函数 - DllImport:using System.Runtime.InteropServices;
        /// </summary>
        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern int SystemParametersInfo(
            int uAction,
            int uParam,
            string lpvParam,
            int fuWinIni
        );
    }

猜你喜欢

转载自blog.csdn.net/lmy_520/article/details/107510082