桌面to-do-list的简单实现
主要解决两个问题,一个是如何设置用户的桌面背景,一个是如何在背景图写入to-do-list的列表。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Crawler
{
public partial class Painter : Form
{
[DllImport(”user32.dll”, CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public Painter()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Image backgroundImage = new Bitmap(@”D:\bg.jpg”);//原始背景图片地址
string toDoListImagePath = @”D:\2111.jpg”;//生成图片地址
string toDoListTxt = “1.System.Drawing.Bitmap image = new System.Drawing.Bitmap\n2.Graphics g = Graphics.FromImage(image);\n3.g.DrawImage(backgroundImage, 0, 0, imageWidth, imageHeight);”;//to-do-list列表
int imageWidth = backgroundImage.Width;
int imageHeight = backgroundImage.Height;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.Black);
g.DrawImage(backgroundImage, 0, 0, imageWidth, imageHeight);
Font tFont = new Font(”Courier New”, 9, FontStyle.Regular);//设置字体
Rectangle rect = new Rectangle(imageWidth - 400, 200, imageWidth, imageHeight);//设置字体区域
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Brush line = new SolidBrush(Color.White);
g.DrawString(toDoListTxt, tFont, line, rect);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);//将绘制的图片保存为内存流
Bitmap map = new Bitmap(ms);
PictureBox pic = new PictureBox();
pic.Image = (Image)map;
pic.Image.Save(toDoListImagePath);
pic.Dispose();
map.Dispose();
tFont.Dispose();
backgroundImage.Dispose();
g.Dispose();
image.Dispose();
if (SystemParametersInfo(20, 0, toDoListImagePath, 0×1 | 0×2)!=0) MessageBox.Show(”Success!”);// 修改桌面背景
MessageBox.Show(”Done!”);
Application.Exit();
}
static void Main()
{
Application.Run(new Painter());
}
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name=”disposing”>如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(77, 69);
this.button1.Name = “button1″;
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = “button1″;
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Painter
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(245, 176);
this.Controls.Add(this.button1);
this.Name = “Painter”;
this.Text = “Painter”;
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
)告知,即刻删除。