WinForm Weather forecast gadget (source download) _c# Tutorial

Source: Internet
Author: User
so we're going to add a Web reference for a total of two
1. According to the IP address to get your city (if there is no this, and directly refer to the relevant site to provide webservice, your location may not be very accurate, if you use the router .... And so on, I do not know that everyone is feeling the same)
2. According to the last acquired city, call the webservice to get the weather data.
To paste part of the code:
Copy Code code 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 transparency of the current form
Achieve no Border 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 no Border 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"; Access to IP URLs
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 (); Reading data from a Web site
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 (");//Space location
int m = ss[1]. IndexOf (' Province ');//ss[1] The actual content is xx xx province, and get the weather webservice only need to know is a city do not need to know province, so intercepted xx city
int x = n-m;
mycity = ss[1]. Substring (m+1,x-2);
}
protected void Displayweather ()
{
Webxml. Weatherwebservice w = new webxml. Weatherwebservice ();
To manipulate webservice as a class
String[] s = new string[23];//declares a string array holds the return result
s = w.getweatherbycityname (mycity);
if (s[8] = = "")
{
MessageBox.Show ("The city for which you are 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 no Border 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;
}
}
}
}

SOURCE download
Install use (when installing the default, change the installation path, please try it, success of the words that happy)
Like the support under Ha, of course you can add features to beautify the gadget, please be sure to tell me ha

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.