SoFunction
Updated on 2025-03-07

Winform weather forecast gadget (with source code download)


using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
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("")]
public static extern bool ReleaseCapture();
[DllImport("")]
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;//Specify the form is completely transparent
GetIP();
GetCityByIP(myip);
DisplayWeather();
}
protected void GetIP()
{
try
{
string strUrl = "http:///"; //Get the IP URL
Uri uri = new Uri(strUrl);
wr = (uri);
s = ().GetResponseStream();
sr = new (s, );
string all = (); //Read the website data
int i = ("[") + 1;
string tempip = (i, 15);
string ip = ("]", "").Replace(" ", "");//Find out i
myip = ip;
}
catch (Exception e)
{
(());
}
}
protected void GetCityByIP(string myip)
{
city = new ();
string[] ss = (myip);
int n = ss[1].IndexOf(' ');//The space is located
int m = ss[1].IndexOf('Province');//The actual content of ss[1] is XX Province XX City, and the webservice to obtain the weather only needs to know that it is a certain city but not the province, so XX City was intercepted
int x = n - m;
mycity = ss[1].Substring(m+1,x-2);
}
protected void DisplayWeather()
{
w = new ();
//Administrate webservice as a class
string[] s = new string[23];//Declare string array to store and return result
s = (mycity);
if (s[8] == "")
{
("The city you are querying is not supported for the time being");
}
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 = ;
= (path+"\\images\\"+png+".png");
= (path + "\\images\\" + png + ".png");
= (path + "\\images\\" + png2 + ".png");
= (path + "\\images\\" + png3 + ".png");
this. = s[5].ToString();
this. = s[12].ToString();
this. = s[17].ToString();
= s[4].ToString();
= s[1].ToString();
= s[5].ToString();
this. = s[6].Substring(s[6].IndexOf('Day')+1).ToString();
this. = s[7].ToString();
this. = s[6].Substring(s[6].IndexOf('Day')+1);
this. = s[13].Substring(s[13].IndexOf('Day')+1);
this. = s[18].Substring(s[18].IndexOf('Day')+1);
}
}
//Implement borderless movement
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (opacity <= 1)
{
opacity = opacity + 0.05;
Opacity = opacity;
}
}
}
}