只要花上一个小时,就可以用C#语言给心爱的TA定制一个WinForm天气闹钟,加上自己录制的闹铃,美哉美哉!天气数据来源于Web Service 接口(http://ws.webxml.com.cn/WebServices/WeatherWS.asmx)。
1. 文件 --> 新建 --> 项目 -->Visual C# --> Windows 窗体应用程序
2. 在项目中的引用上点击鼠标右键,选择添加服务引用 --> 高级--> 添加 Web 引用
3. 在URL中填写天气Web Service:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx,并点击右边的“前往”小图标,自动生成 Web 引用名,点击【添加引用】即可
4. 拖拽控件并编写逻辑代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WeatherAlarmClock
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
cn.com.webxml.ws.WeatherWS w = null;
private void button1_Click(object sender, EventArgs e)
{
label_weather.Text = "";
try
{
string[] weather = w.getWeather(comboBox_city.Text.Trim(), null);
if (weather[1] == "")
{
label_weather.Text = "查询次数过多,请24小时后再试!";
}
else
{
for (int i = 7; i < 9; i++)
{
label_weather.Text += weather[i] + " ";
}
pictureBox_weather.Image = Image.FromFile(@"..\..\weather\b_" + weather[10]);
weather[4] = weather[4].Replace("气温", "");
weather[4] = weather[4].Replace("风向/风力", "");
weather[4] = weather[4].Replace(";", " ");
int index = weather[4].IndexOf(":");
weather[4] = weather[4].Replace(":", "");
weather[4] = "实时" + weather[4].Substring(index);
label_weather.Text += "\n" + weather[4];
weather[5] = weather[5].Replace("强度", "");
weather[5] = weather[5].Replace("。", " ");
label_weather.Text += "\n" + weather[5];
}
}
catch
{
Console.WriteLine("无法获取WebService!");
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
w = new cn.com.webxml.ws.WeatherWS();
label_weather.Text = "";
label_dis.Text = "";
string[] province = w.getRegionProvince();
for (int i = 0; i < province.Length; i++)
{
// 去除逗号后的省份代码,如果将省份名称和代码一起上传,城市查询结果为空
int index = province[i].LastIndexOf(",");
province[i] = province[i].Substring(0, index);
comboBox_province.Items.Add(province[i]);
}
comboBox_province.SelectedIndex = 0;
}
catch
{
Console.WriteLine("无法获取WebService!");
}
label_time.Text = DateTime.Now.ToLocalTime().ToString("HH:mm:ss");
label_alarmClock.Text = "";
numericUpDown_hour.Text = DateTime.Now.ToLocalTime().Hour.ToString();
numericUpDown_minute.Text = DateTime.Now.ToLocalTime().Minute.ToString();
}
private void comboBox_province_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox_city.Items.Clear();
try
{
string[] city = w.getSupportCityString(comboBox_province.Text.Trim());
for (int i = 0; i < city.Length; i++)
{
// 去除逗号后的城市代码,如果将城市名称和代码一起上传,WebService 返回查询结果为空
int index = city[i].LastIndexOf(",");
city[i] = city[i].Substring(0, index);
comboBox_city.Items.Add(city[i]);
}
comboBox_city.SelectedIndex = 0;
}
catch
{
Console.WriteLine("无法获取WebService!");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label_time.Text = DateTime.Now.ToLocalTime().ToString("HH:mm:ss");
System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
if (flag)
{
int disMin = Convert.ToInt32(hour) * 60 + Convert.ToInt32(minute) - (DateTime.Now.ToLocalTime().Hour * 60 + DateTime.Now.ToLocalTime().Minute);
disMin--;
disMin = disMin < 0 ? disMin + 24 * 60 : disMin;
label_dis.Text = "离闹钟还有 " + (disMin / 60 < 10 ? "0" + (disMin / 60).ToString() : (disMin / 60).ToString())
+ ":" + (disMin % 60 < 10 ? "0" + (disMin % 60).ToString() : (disMin % 60).ToString());
}
if (DateTime.Now.ToLocalTime().Hour == Convert.ToInt32(hour) && DateTime.Now.ToLocalTime().Minute == Convert.ToInt32(minute) && DateTime.Now.ToLocalTime().Second == 0)
{
flag = false;
sp.SoundLocation = @"..\..\alarm\WonderfulAbilitySong.wav";
sp.PlayLooping();
if (MessageBox.Show("时间到!", "警告", MessageBoxButtons.OK) == DialogResult.OK)
{
sp.Stop();
}
label_alarmClock.Text = "";
label_dis.Text = "";
}
}
private void numericUpDown_hour_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown_hour.Value == -1)
{
numericUpDown_hour.Value = 23;
}
else if (numericUpDown_hour.Value == 24)
{
numericUpDown_hour.Value = 0;
}
}
private void numericUpDown_minute_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown_minute.Value == -1)
{
numericUpDown_minute.Value = 59;
}
else if (numericUpDown_minute.Value == 60)
{
numericUpDown_minute.Value = 0;
}
}
string hour = DateTime.Now.ToLocalTime().Hour.ToString();
string minute = DateTime.Now.ToLocalTime().Minute.ToString();
bool flag = false;
private void button_set_Click(object sender, EventArgs e)
{
MessageBox.Show("闹钟已设定,请确保音量打开且能正常播放,切勿关闭程序!", "提示");
hour = Convert.ToInt32(numericUpDown_hour.Value) < 10 ? "0" + numericUpDown_hour.Value.ToString() : numericUpDown_hour.Value.ToString();
minute = Convert.ToInt32(numericUpDown_minute.Value) < 10 ? "0" + numericUpDown_minute.Value.ToString() : numericUpDown_minute.Value.ToString();
label_alarmClock.Text = "已设 " + hour + ":" + minute;
flag = true;
}
}
}