欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

winform天气预报小工具(附源码下载)

程序员文章站 2023-12-01 21:26:40
所以我们要添加web引用共两个 1.根据ip地址获取你所在城市(假如没有这个,而直接引用相关网站提供的webservice,你所在的地点可能不是很准确,假如,你用了路由器....
所以我们要添加web引用共两个
1.根据ip地址获取你所在城市(假如没有这个,而直接引用相关网站提供的webservice,你所在的地点可能不是很准确,假如,你用了路由器....等,不知道大家是有同感)
2.根据上一部获取的城市,调用获取天气数据的webservice
贴取部分代码:
复制代码 代码如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.servicemodel;
using system.runtime.interopservices;
namespace myweather
{
public partial class form1 : form
{
string myip,mycity;
private double opacity = 0;//记录当前窗体的透明度
//实现无边框移动
[dllimport("user32.dll")]
public static extern bool releasecapture();
[dllimport("user32.dll")]
public static extern bool sendmessage(intptr hwnd, int wmsg, int wparam, int lparam);
public const int wm_syscommand = 0x0112;
public const int sc_move = 0xf010;
public const int htcaption = 0x0002;
//实现无边框移动
public form1()
{
initializecomponent();
}
private void form1_load(object sender, eventargs e)
{
opacity = 0;//指定窗体完全透明
getip();
getcitybyip(myip);
displayweather();
}
protected void getip()
{
try
{
string strurl = "http://www.ip138.com/ip2city.asp"; //获得ip的网址
uri uri = new uri(strurl);
system.net.webrequest wr = system.net.webrequest.create(uri);
system.io.stream s = wr.getresponse().getresponsestream();
system.io.streamreader sr = new system.io.streamreader(s, encoding.default);
string all = sr.readtoend(); //读取网站的数据
int i = all.indexof("[") + 1;
string tempip = all.substring(i, 15);
string ip = tempip.replace("]", "").replace(" ", "");//找出i
myip = ip;
}
catch (exception e)
{
console.writeline(e.tostring());
}
}
protected void getcitybyip(string myip)
{
ipcity.ipaddresssearchwebservice city = new ipcity.ipaddresssearchwebservice();
string[] ss = city.getcountrycitybyip(myip);
int n = ss[1].indexof(' ');//空格所在位置
int m = ss[1].indexof('省');//ss[1]的实际内容是xx省 xx市,而获取天气的webservice只需要知道是某个市不需要知道省,所以截取了xx市
int x = n - m;
mycity = ss[1].substring(m+1,x-2);
}
protected void displayweather()
{
webxml.weatherwebservice w = new webxml.weatherwebservice();
//把webservice当做一个类来操作
string[] s = new string[23];//声明string数组存放返回结果
s = w.getweatherbycityname(mycity);
if (s[8] == "")
{
messagebox.show("暂时不支持您查询的城市");
}
else
{
string png = s[8].substring(0, s[8].length - 4);
string png2 = s[15].substring(0, s[15].length - 4);
string png3 = s[20].substring(0, s[20].length - 4);
string path = application.startuppath;
pictoday.image = image.fromfile(path+"\\images\\"+png+".png");
pic1.image = image.fromfile(path + "\\images\\" + png + ".png");
pic2.image = image.fromfile(path + "\\images\\" + png2 + ".png");
pic3.image = image.fromfile(path + "\\images\\" + png3 + ".png");
this.lbl1.text = s[5].tostring();
this.lbl2.text = s[12].tostring();
this.lbl3.text = s[17].tostring();
this.time.text = s[4].tostring();
this.address.text = s[1].tostring();
this.temperature.text = s[5].tostring();
this.label4.text = s[6].substring(s[6].indexof('日')+1).tostring();
this.label5.text = s[7].tostring();
this.tempo1.text = s[6].substring(s[6].indexof('日')+1);
this.tempo2.text = s[13].substring(s[13].indexof('日')+1);
this.tempo3.text = s[18].substring(s[18].indexof('日')+1);
}
}
//实现无边框移动
private void form1_mousedown(object sender, mouseeventargs e)
{
releasecapture();
sendmessage(this.handle, wm_syscommand, sc_move + htcaption, 0);
}
private void timer1_tick(object sender, eventargs e)
{
if (opacity <= 1)
{
opacity = opacity + 0.05;
opacity = opacity;
}
}
}
}

源码下载
安装使用(安装的时候一路默认,改变安装路径,请您试试把,成功的话那皆大欢喜)
喜欢的支持下哈,当然你可以增加功能,美化该小工具,请一定要告诉我哈