Therefore, we need to add two web references.
1. obtain your city from the IP address (if this is not available, and directly reference the webservice provided by the relevant website, your location may not be very accurate. If you use a vro ....)
2. Call the webservice to obtain weather data based on the City obtained in the previous step.
Paste some code:
Copy codeThe Code is as follows: 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; // record the transparency of the current form
// Implement 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;
// Implement borderless Movement
Public Form1 ()
{
InitializeComponent ();
}
Private void Form1_Load (object sender, EventArgs e)
{
Opacity = 0; // the specified form is completely transparent.
GetIP ();
GetCityByIP (myip );
DisplayWeather ();
}
Protected void GetIP ()
{
Try
{
String strUrl = "http://www.ip138.com/ip2city.asp"; // obtain the IP Address URL
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 (); // reads website data
Int I = all. IndexOf ("[") + 1;
String tempip = all. Substring (I, 15 );
String ip = tempip. Replace ("]", ""). Replace ("", ""); // find 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 (''); // location of the space
Int m = ss [1]. indexOf ('province '); // the actual content of ss [1] is XX Province, XX City, and the webservice that obtains the weather only needs to know that a city does not need to know the province, so we intercepted the city XX.
Int x = n-m;
Mycity = ss [1]. Substring (m + 1, X-2 );
}
Protected void DisplayWeather ()
{
Webxml. WeatherWebService w = new webxml. WeatherWebService ();
// Use webservice as a class
String [] s = new string [23]; // declare the string array to store the returned results
S = w. getWeatherbyCityName (mycity );
If (s [8] = "")
{
MessageBox. Show ("the city you are not allowed to query now ");
}
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 );
}
}
// Implement borderless Movement
Private void form=mousedown (object sender, MouseEventArgs e)
{
ReleaseCapture ();
SendMessage (this. Handle, WM_SYSCOMMAND, SC _MOVE + HTCAPTION, 0 );
}
Private void timereffectick (object sender, EventArgs e)
{
If (opacity <= 1)
{
Opacity = opacity + 0.05;
Opacity = opacity;
}
}
}
}
Source code download
Installation and use (the installation path is changed by default during installation. Please try it. If it succeeds, it will be a pleasure)
Thanks for your favorite support. Of course you can add features to beautify this tool. please be sure to tell me.