界面设计学习
1、将项目加入到ComboBox的最上面
if(this.comboBoxWeb.FindStringExact(this.comboBoxWeb.Text) < 0)
{
this.comboBoxWeb.Items.Insert(0, this.comboBoxWeb.Text);
}
{
this.comboBoxWeb.Items.Insert(0, this.comboBoxWeb.Text);
}
From http://www.codeproject.com/KB/IP/Crawler.aspx
2、显示CPU和内存使用率
代码
System.Diagnostics.PerformanceCounter cpuCounter =
new System.Diagnostics.PerformanceCounte();
System.Diagnostics.PerformanceCounter ramCounter =
new System.Diagnostics.PerformanceCounte("Memory", "Available MBytes");
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
FreeMemory = ramCounter.NextValue();
CPUUsage = (int)cpuCounter.NextValue();
new System.Diagnostics.PerformanceCounte();
System.Diagnostics.PerformanceCounter ramCounter =
new System.Diagnostics.PerformanceCounte("Memory", "Available MBytes");
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
FreeMemory = ramCounter.NextValue();
CPUUsage = (int)cpuCounter.NextValue();
其中FreeMemory和CPUUsage属性定义如下:
代码
private float nFreeMemory;
private float FreeMemory
{
get { return nFreeMemory; }
set
{
nFreeMemory = value;
statusBarPanelMem.Text = nFreeMemory + "Mb Available";
}
}
private int nCPUUsage;
private int CPUUsage
{
get { return nCPUUsage; }
set
{
nCPUUsage = value;
statusBarPanelCPU.Text = "CPU usage" + nCPUUsage + "%";
Icon icon = Icon.FromHandle(((Bitmap)imageListPercentage.Image[value/10]).GetHicon());
statusBarPanelCPU.Icon = icon;
}
}
private float FreeMemory
{
get { return nFreeMemory; }
set
{
nFreeMemory = value;
statusBarPanelMem.Text = nFreeMemory + "Mb Available";
}
}
private int nCPUUsage;
private int CPUUsage
{
get { return nCPUUsage; }
set
{
nCPUUsage = value;
statusBarPanelCPU.Text = "CPU usage" + nCPUUsage + "%";
Icon icon = Icon.FromHandle(((Bitmap)imageListPercentage.Image[value/10]).GetHicon());
statusBarPanelCPU.Icon = icon;
}
}
而在imageListPercentage中则是十个标识不同进度的图标icon。
From http://www.codeproject.com/KB/IP/Crawler.aspx
3、监测网络连接状态
代码
[DllImport("wininet")]
public static extern int InternetGetConnectedState(ref int lpdwFlags, int dwReserved);
[DllImport("wininet")]
public static extern int InternetAutodial(int dwFlags, int hwndParent);
public static extern int InternetGetConnectedState(ref int lpdwFlags, int dwReserved);
[DllImport("wininet")]
public static extern int InternetAutodial(int dwFlags, int hwndParent);
使用下列检测代码:
代码
try
{
int nState = 0;
if(InternetGetConnectedState(ref nState, 0) == 0)
{
if(nFirstTimeCheckConnection++ == 0)
// ask for dial up or DSL connection
if(InternetAutodial(1, 0) != 0)
// check internet connection state again
InternetGetConnectedState(ref nState, 0);
}
if((nState & 2) == 2 || (nState & 4) == 4)
// reset to reask for connection agina
nFirstTimeCheckConnection = 0;
}
catch
{
}
{
int nState = 0;
if(InternetGetConnectedState(ref nState, 0) == 0)
{
if(nFirstTimeCheckConnection++ == 0)
// ask for dial up or DSL connection
if(InternetAutodial(1, 0) != 0)
// check internet connection state again
InternetGetConnectedState(ref nState, 0);
}
if((nState & 2) == 2 || (nState & 4) == 4)
// reset to reask for connection agina
nFirstTimeCheckConnection = 0;
}
catch
{
}
对其中的状态再进行解析即可:
代码
string InternetGetConnectedStateString()
{
string strState = "";
try
{
int nState = 0;
// check internet connection state
if(InternetGetConnectedState(ref nState, 0) == 0)
return "You are currently not connected to the internet";
if((nState & 1) == 1)
strState = "Modem connection";
else if((nState & 2) == 2)
strState = "LAN connection";
else if((nState & 4) == 4)
strState = "Proxy connection";
else if((nState & 8) == 8)
strState = "Modem is busy with a non-Internet connection";
else if((nState & 0x10) == 0x10)
strState = "Remote Access Server is installed";
else if((nState & 0x20) == 0x20)
return "Offline";
else if((nState & 0x40) == 0x40)
return "Internet connection is currently configured";
// get current machine IP
IPHostEntry he = Dns.Resolve(Dns.GetHostName());
strState += ", Machine IP: "+he.AddressList[0].ToString();
}
catch
{
}
return strState;
}
{
string strState = "";
try
{
int nState = 0;
// check internet connection state
if(InternetGetConnectedState(ref nState, 0) == 0)
return "You are currently not connected to the internet";
if((nState & 1) == 1)
strState = "Modem connection";
else if((nState & 2) == 2)
strState = "LAN connection";
else if((nState & 4) == 4)
strState = "Proxy connection";
else if((nState & 8) == 8)
strState = "Modem is busy with a non-Internet connection";
else if((nState & 0x10) == 0x10)
strState = "Remote Access Server is installed";
else if((nState & 0x20) == 0x20)
return "Offline";
else if((nState & 0x40) == 0x40)
return "Internet connection is currently configured";
// get current machine IP
IPHostEntry he = Dns.Resolve(Dns.GetHostName());
strState += ", Machine IP: "+he.AddressList[0].ToString();
}
catch
{
}
return strState;
}
注意:字符串中已经包含了当前计算机的IP地址。