代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
winform天气预报小工具
作者:
Dezai大蜗牛
/ 发布于
2012/3/6
/
617
主要原理就是利用网上免费的webservice获取天气数据 所以我们要添加web引用共两个 1.根据IP地址获取你所在城市(假如没有这个,而直接引用相关网站提供的webservice,你所在的地点可能不是很准确,假如,你用了路由器....等,不知道大家是有同感) 2.根据上一部获取的城市,调用获取天气数据的webservice 贴取部分代码:
<div>using System;</div> <div>using System.Collections.Generic;</div> <div>using System.ComponentModel;</div> <div>using System.Data;</div> <div>using System.Drawing;</div> <div>using System.Text;</div> <div>using System.Windows.Forms;</div> <div>using System.ServiceModel;</div> <div>using System.Runtime.InteropServices;</div> <div></div> <div>namespace MyWeather</div> <div>{</div> <div> </div> <div> </div> <div></div> <div> public partial class Form1 : Form</div> <div> {</div> <div> </div> <div> string myip,mycity;</div> <div> private double opacity = 0;//记录当前窗体的透明度 </div> <div> //实现无边框移动</div> <div> [DllImport("user32.dll")]</div> <div> public static extern bool ReleaseCapture();</div> <div> [DllImport("user32.dll")]</div> <div> public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);</div> <div> public const int WM_SYSCOMMAND = 0x0112;</div> <div> public const int SC_MOVE = 0xF010;</div> <div> public const int HTCAPTION = 0x0002;</div> <div> //实现无边框移动</div> <div></div> <div></div> <div> public Form1()</div> <div> {</div> <div> InitializeComponent();</div> <div> </div> <div> } <div></div> <div> </div> <div></div> <div> private void Form1_Load(object sender, EventArgs e)</div> <div> {</div> <div> Opacity = 0;//指定窗体完全透明 </div> <div> GetIP();</div> <div> GetCityByIP(myip);</div> <div> DisplayWeather();</div> <div> } <div></div> <div> protected void GetIP()</div> <div> {</div> <div> try</div> <div> {</div> <div> string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址</div> <div> Uri uri = new Uri(strUrl);</div> <div> System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);</div> <div> System.IO.Stream s = wr.GetResponse().GetResponseStream();</div> <div> System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.Default);</div> <div> string all = sr.ReadToEnd(); //读取网站的数据</div> <div> int i = all.IndexOf("[") + 1;</div> <div> string tempip = all.Substring(i, 15);</div> <div> string ip = tempip.Replace("]", "").Replace(" ", "");//找出i</div> <div> myip = ip;</div> <div> } <div> catch (Exception e)</div> <div> {</div> <div> Console.WriteLine(e.ToString());</div> <div> } <div> } <div></div> <div> protected void GetCityByIP(string myip)</div> <div> {</div> <div> IPCity.IpAddressSearchWebService city = new IPCity.IpAddressSearchWebService();</div> <div></div> <div> string[] ss = city.getCountryCityByIp(myip);</div> <div> </div> <div> int n = ss[1].IndexOf(' ');//空格所在位置</div> <div> int m = ss[1].IndexOf('省');//ss[1]的实际内容是XX省 XX市,而获取天气的webservice只需要知道是某个市不需要知道省,所以截取了XX市</div> <div> int x = n - m;</div> <div> mycity = ss[1].Substring(m+1,x-2);</div> <div> </div> <div> </div> <div></div> <div> } <div> protected void DisplayWeather()</div> <div> {</div> <div></div> <div> webxml.WeatherWebService w = new webxml.WeatherWebService();</div> <div> //把webservice当做一个类来操作 </div> <div></div> <div> string[] s = new string[23];//声明string数组存放返回结果 </div> <div> </div> <div> s = w.getWeatherbyCityName(mycity);</div> <div> </div> <div> </div> <div> if (s[8] == "")</div> <div> {</div> <div> MessageBox.Show("暂时不支持您查询的城市");</div> <div> } <div> else</div> <div> {</div> <div></div> <div> string png = s[8].Substring(0, s[8].Length - 4);</div> <div> string png2 = s[15].Substring(0, s[15].Length - 4);</div> <div> string png3 = s[20].Substring(0, s[20].Length - 4);</div> <div> string path = Application.StartupPath;</div> <div> pictoday.Image = Image.FromFile(path+"\\images\\"+png+".png");</div> <div> pic1.Image = Image.FromFile(path + "\\images\\" + png + ".png");</div> <div> pic2.Image = Image.FromFile(path + "\\images\\" + png2 + ".png");</div> <div> pic3.Image = Image.FromFile(path + "\\images\\" + png3 + ".png");</div> <div> this.lbl1.Text = s[5].ToString();</div> <div> this.lbl2.Text = s[12].ToString();</div> <div> this.lbl3.Text = s[17].ToString();</div> <div> this.time.Text = s[4].ToString();</div> <div> this.address.Text = s[1].ToString();</div> <div> this.temperature.Text = s[5].ToString();</div> <div> this.label4.Text = s[6].Substring(s[6].IndexOf('日')+1).ToString();</div> <div> this.label5.Text = s[7].ToString();</div> <div> this.tempo1.Text = s[6].Substring(s[6].IndexOf('日')+1);</div> <div> this.tempo2.Text = s[13].Substring(s[13].IndexOf('日')+1);</div> <div> this.tempo3.Text = s[18].Substring(s[18].IndexOf('日')+1);</div> <div> </div> <div></div> <div> } <div> } <div> //实现无边框移动</div> <div> private void Form1_MouseDown(object sender, MouseEventArgs e)</div> <div> {</div> <div> ReleaseCapture();</div> <div> SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);</div> <div> } <div></div> <div> private void timer1_Tick(object sender, EventArgs e)</div> <div> {</div> <div> if (opacity <= 1) </div> <div></div> <div> { </div> <div></div> <div> opacity = opacity + 0.05; </div> <div></div> <div> Opacity = opacity; </div> <div> } </div> <div> } <div> } <div>}
试试其它关键字
天气预报小工具
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai大蜗牛
贡献的其它代码
(
1
)
.
winform天气预报小工具
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3