winform weather forecast gadget (with source code download)

  • 2020-05-07 20:16:12
  • OfStack

so we're going to add an web reference to two
1. Get your city by the IP address (if you don't have this, you may not be exactly where you are if you use a router... I don't know if you feel the same way.)
2. Call webservice, which retrieves the weather data, based on the city obtained in part 1
Paste part of the code:
 
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;// Records the transparency of the current form  
// Achieve borderless movement  
[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; 
// Achieve borderless movement  
public Form1() 
{ 
InitializeComponent(); 
} 
private void Form1_Load(object sender, EventArgs e) 
{ 
Opacity = 0;// Specifies that the form is completely transparent  
GetIP(); 
GetCityByIP(myip); 
DisplayWeather(); 
} 
protected void GetIP() 
{ 
try 
{ 
string strUrl = "http://www.ip138.com/ip2city.asp"; // To obtain IP The website of  
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(); // Read the data of the website  
int i = all.IndexOf("[") + 1; 
string tempip = all.Substring(i, 15); 
string ip = tempip.Replace("]", "").Replace(" ", "");// To find out 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(' ');// Space position  
int m = ss[1].IndexOf(' province ');//ss[1] The actual content is XX province  XX City, and get the weather webservice You just need to know it's a city and you don't need to know it's a province, so you intercept it XX The city  
int x = n - m; 
mycity = ss[1].Substring(m+1,x-2); 
} 
protected void DisplayWeather() 
{ 
webxml.WeatherWebService w = new webxml.WeatherWebService(); 
// the webservice As a 1 Class to operate on  
string[] s = new string[23];// The statement string The array holds the returned results  
s = w.getWeatherbyCityName(mycity); 
if (s[8] == "") 
{ 
MessageBox.Show(" The city you are querying is not currently supported "); 
} 
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(' day ')+1).ToString(); 
this.label5.Text = s[7].ToString(); 
this.tempo1.Text = s[6].Substring(s[6].IndexOf(' day ')+1); 
this.tempo2.Text = s[13].Substring(s[13].IndexOf(' day ')+1); 
this.tempo3.Text = s[18].Substring(s[18].IndexOf(' day ')+1); 
} 
} 
// Achieve borderless movement  
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; 
} 
} 
} 
} 

Download the source code
Installation and use (1 default when installation, change the installation path, please try it, successful words that everyone is happy)
Like the support of the ha, of course you can add features, beautify the gadget, please 1 must tell me ha

Related articles: