一、最小化窗口

点击“X”或“Alt+F4”时,最小化窗口,
如:
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0×0112;
const int SC_CLOSE = 0xF060;
if (m.Msg == WM_SYSCOMMAND && (int) m.WParam == SC_CLOSE)
{
// User clicked close button
this.WindowState = FormWindowState.Minimized;
return;
}
base.WndProc(ref m);
}

二、如何让Foreach 循环运行的更快
foreach是一个对集合中的元素进行简单的枚举及处理的现成语句,用法如下例所示:
using System;
using System.Collections;
namespace LoopTest
{
class Class1
{
static void Main(string[] args)
{
// create an ArrayList of strings
ArrayList array = new ArrayList();
array.Add("Marty");
array.Add("Bill");
array.Add("George");
// print the value of every item
foreach (string item in array)
{
Console.WriteLine(item);
}
}
}
你可以将foreach语句用在每个实现了Ienumerable接口的集合里。如果想了解更多foreach的用法,你可以查看.NET Framework SDK文档中的C# Language Specification。

在编译的时候,C#编辑器会对每一个foreach 区域进行转换。IEnumerator enumerator = array.GetEnumerator();
try
{
string item;
while (enumerator.MoveNext())
{
item = (string) enumerator.Current;
Console.WriteLine(item);
}
}
finally
{
IDisposable d = enumerator as IDisposable;
if (d != null) d.Dispose();
}
这说明在后台,foreach的管理会给你的程序带来一些增加系统开销的额外代码。

三、将图片保存到一个XML文件
WinForm的资源文件中,将PictureBox的Image属性等非文字内容都转变成文本保存,这是通过序列化(Serialization)实现的,
例子://
using System.Runtime.Serialization.Formatters.Soap;
Stream stream = new FileStream("E:\\Image.xml",FileMode.Create,FileAccess.Write,FileShare.None);
SoapFormatter f = new SoapFormatter();
Image img = Image.FromFile("E:\\Image.bmp");
f.Serialize(stream,img);
stream.Close();

四、屏蔽CTRL-V
在WinForm中的TextBox控件没有办法屏蔽CTRL-V的剪贴板粘贴动作,如果需要一个输入框,但是不希望用户粘贴剪贴板的内容,可以改用RichTextBox控件,并且在KeyDown中屏蔽掉CTRL-V键,例子:

private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.Control && e.KeyCode==Keys.V)
e.Handled = true;
}

一、判断文件或文件夹是否存在
使用System.IO.File,要检查一个文件是否存在非常简单:
bool exist = System.IO.File.Exists(fileName);

如果需要判断目录(文件夹)是否存在,可以使用System.IO.Directory:
bool exist = System.IO.Directory.Exists(folderName);

二、使用delegate类型设计自定义事件
在C#编程中,除了Method和Property,任何Class都可以有自己的事件(Event)。定义和使用自定义事件的步骤如下:
(1)在Class之外定义一个delegate类型,用于确定事件程序的接口
(2)在Class内部,声明一个public event变量,类型为上一步骤定义的delegate类型
(3)在某个Method或者Property内部某处,触发事件
(4)Client程序中使用+=操作符指定事件处理程序

例子: // 定义Delegate类型,约束事件程序的参数
public delegate void MyEventHandler(object sender, long lineNumber) ;

public class DataImports
{
// 定义新事件NewLineRead
public event MyEventHandler NewLineRead ;

public void ImportData()
{
long i = 0 ; // 事件参数
while()
{
i++ ;
// 触发事件
if( NewLineRead != null ) NewLineRead(this, i);
//…
}
//…
}
//…
}

// 以下为Client代码

private void CallMethod()
{
// 声明Class变量,不需要WithEvents
private DataImports _da = null;
// 指定事件处理程序
_da.NewLineRead += new MyEventHandler(this.DA_EnterNewLine) ;
// 调用Class方法,途中会触发事件
_da.ImportData();
}
// 事件处理程序
private void DA_EnterNewLine(object sender, long lineNumber)
{
// …
}

三、IP与主机名解析
使用System.Net可以实现与Ping命令行类似的IP解析功能,例如将主机名解析为IP或者反过来: private string GetHostNameByIP(string ipAddress)
{
IPHostEntry hostInfo = Dns.GetHostByAddress(ipAddress);
return hostInfo.HostName;
}
private string GetIPByHostName(string hostName)
{
System.Net.IPHostEntry hostInfo = Dns.GetHostByName(hostName);
return hostInfo.AddressList[0].ToString();
}

In some scenarios, you may wish to ensure that a user can run only one instance of your application at a time. Besides ensuring that only a single instance of your application is running, you may also want to bring the instance already running to the front and restore it, if it is minimized.
First, to ensure that only one instance of your application is running at a time, the best method I've found is to create a mutex that is held by the operating system (thanks to Michael Covington). This will put a request to the operating system that a mutex be created if one does not already exist. Only one mutex can ever be created at a time, so if you request a new one and it cannot be created, you can safely assume that your application is already running.

using System.Threading
using System.Runtime.InteropServices;

public class Form1 : Form
{
[STAThread]
static void Main()
{
bool createdNew;

Mutex m = new Mutex(true, "YourAppName", out createdNew);

if (! createdNew)
{
// app is already running…
MessageBox.Show("Only one instance of this application is allowed at a time.");
return;
}

Application.Run(new Form1());

// keep the mutex reference alive until the normal termination of the program
GC.KeepAlive(m);
}
}

The above code will work for the vast majority of your needs. It will also run under scenarios where your code is executing with less than FullTrust permissions (see Code Access Security in MSDN for further information).

If your application can run with Full Trust permissions, we can take this a step further and find the window of the application instnace already running and bring it to the front for the user:

public class Form1 : Form
{
[STAThread]
static void Main()
{
bool createdNew;

System.Threading.Mutex m = new System.Threading.Mutex(true, "YourAppName", out createdNew);

if (! createdNew)
{
// see if we can find the other app and Bring it to front
IntPtr hWnd = FindWindow("WindowsForms10.Window.8.app3", "YourAppName");

if(hWnd != IntPtr.Zero)
{
Form1.WINDOWPLACEMENT placement = new Form1.WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);

GetWindowPlacement(hWnd, ref placement);

if(placement.showCmd != SW_NORMAL)
{
placement.showCmd = SW_RESTORE;

SetWindowPlacement(hWnd, ref placement);
SetForegroundWindow(hWnd);
}
}

return;
}

Application.Run(new Form1());

// keep the mutex reference alive until the normal termination of the program
GC.KeepAlive(m);
}

private const int SW_NORMAL = 1; // see WinUser.h for definitions
private const int SW_RESTORE = 9;

[DllImport("User32",EntryPoint="FindWindow")]
static extern IntPtr FindWindow(string className, string windowName);

[DllImport("User32",EntryPoint="SendMessage")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

[DllImport("User32",EntryPoint="SetForegroundWindow")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("User32",EntryPoint="SetWindowPlacement")]
private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);

[DllImport("User32",EntryPoint="GetWindowPlacement")]
private static extern bool GetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);

private struct POINTAPI
{
public int x;
public int y;
}

private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}

private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public POINTAPI ptMinPosition;
public POINTAPI ptMaxPosition;
public RECT rcNormalPosition;
}
}

As you can see, with minimal effort, you can easily add a polished touch to your application. This might even help you avoid some extra legwork in ensuring that there are no issues with running multiple instances of your app at the same time that you might have to address.

For more information about the Platform Invoke mechanisms to call Win32 API functions, I recommend that you check out .NET Framework Solutions: In Search of the Lost Win32 API by John Mueller and Charles Petzold's seminal classic Programming Windows.

Until Longhorn comes out and more of the Windows platform becomes managed, platform invokes and interop will remain a key technology to understand and use to your advantage to fill the gaps left by the Windows Forms framework.

UPDATE:
Visual Studio .NET 2005 (aka. “Whidbey”) will let you specify Single Instance behavior for Visual Basic .NET projects through the Project Properties page. Curious as to why this option is available for VB projects and not for C# projects, as this seems to instruct the runtime or some framework library not to load more than one instance.

北理代理助手是专为北京理工大学开发的代理设置软件。使用该软件可以快速的切换、验证代理,并可随时下载网协提供的最新的代理列表。

http://www.felixwoo.com/wp-content/uploads/attachments/old/20041092068694.jpg

特色:
1、专为北理校园网设计 可以随时从网协服务器下载最新代理列表,列表数据由服务器每小时自动验证更新
2、快速切换代理 双击代理列表中的代理或在悬浮窗菜单中选择即可切换,无需重启IE
3、快速启用/禁用代理 通过主界面上选择或使用热键(Alt+F1)即可方便的启用或禁用代理,无需重启IE
4、自动启用最快代理 勾选此选项后即可在下载或验证结束后自动启用最快的代理
5、本地代理列表管理方便 除了可以从服务器下载最新的代理列表,您还可以在本地手动进行验证,验证结果按照由快到慢的顺序自动排列。您也可以手动的添加或删除代理。
6、添加代理方便 直接将代理文本拖动到悬浮窗即可添加到代理列表
7、绿色软件 免安装、不更改注册表、程序小巧(不到200K)

使用说明

http://www.felixwoo.com/wp-content/uploads/attachments/old/200410920650941.jpg

下载地址:
/download/北理代理助手.exe
.NET Framework /dotnetfx.exe (运行前请先安装)

Felix
2004.10.9

从20楼跳下来∶ 阿阿阿阿阿阿阿阿阿阿阿阿阿!!!!!!!!!砰

从2楼跳下来∶ 砰!!!!!!!!!!阿阿阿阿阿阿阿阿阿阿阿阿阿!!!!….

For performance test, it is very important to measure code execution time. Without measurement, there is no way to tell if we meet performance goal.

System.Environment.TickCount is not suitable for high resolution timing. Its resolution cannot be less than 500 milliseconds.

System.Datetime.Now returns the current time of type DateTime. With start datetime and end datetime, we can get the interval as a value of TimeSpan by (end – start ) . TimeSpan.TotalMilliseconds or TimeSpan.Ticks may be used to read interval. From MSDN, the resolution of System.Datetime.Now depends on the system timer.

System Approximate Resolution

Windows NT 3.5 and later 10 milliseconds

Windows 98 55 milliseconds

So it is better but not high resolution at all.

In .NET framework v1 and v1.1, we have to use P/Invoke to get high resolution reading. The class below is commonly used in performance test measurement. It is querying hardware to get high resolution performance counter. For more information (including what happens if the hardware does not support high resolution performance counter) please check MSDN for QueryPerformanceCounter and QueryPerformanceFrequency.

public class HighResolutionTimer

{

private long start;

private long stop;

private long frequency;

public HighResolutionTimer()

{

QueryPerformanceFrequency (ref frequency);

}

public void Start ()

{

QueryPerformanceCounter (ref start);

}

public void Stop ()

{

QueryPerformanceCounter (ref stop);

}

public float ElapsedTime

{

get{

float elapsed = (((float)(stop – start)) / ((float) frequency));

return elapsed;

}

}

[System.Runtime.InteropServices.DllImport("KERNEL32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]

private static extern bool QueryPerformanceCounter( [In, Out] ref long performanceCount);

[System.Runtime.InteropServices.DllImport("KERNEL32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]

private static extern bool QueryPerformanceFrequency( [In, Out] ref long frequency);

}

To illustrate the use of this class, check the code below.

HighResolutionTimer timer = new HighResolutionTimer();

timer.Start();

//Perf Test

timer.Stop();

Console.WriteLine(timer.ElapsedTime);

(This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)

作者:孙展波

来源: http://blogs.gotdotnet.com/ZHANBOS

http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl,这是一个WSDL文件格式,可以直接在您的VS.NET环境中直接添加Web引用,把该地址输入即可。

该Web Service就只有一个方法,即string sendXml(carrier,userid,password,mobilenumber,content,msgtype)。各个参数全部为string类型,其含义基本如下(可能不正确)。

carrier:运营商名称,这里面可以随便输,不过似乎没有任何显示,不知道里面有没有其它奥秘。
userid:您在新浪无线上注册的手机ID,即http://sms.sina.com.cn。
password:您在新浪无线上注册手机时所使用的密码。
mobilenumber:对方的手机号码;
content:发送短消息的内容;
msgtype:发送短消息的类型,我估计支持彩信,不过我目前仅使用文本短信方式,似乎随便输什么都可以,我使用的是“Text”。

示例如下:
Sina.SMSWS ws = new Sina.SMSWS();
string result = ws.sendXml("Sina",textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text,"new");

资费标准请参看新浪无线网站上的相关说明,应该是一条一角钱,不过也或者是一条两角线。由于其后台可能使用了消息队列机制,在繁忙的时候,可能会有几秒钟延迟。

这里是几个主要非英文语系字符范围(google上找到的):
2E80~33FFh:中日韩符号区。收容康熙字典部首、中日韩辅助部首、注音符号、日本假名、韩文音符,中日韩的符号、标点、带圈或带括符文数字、月份,以及日本的假名组合、单位、年号、月份、日期、时间等。
3400~4DFFh:中日韩认同表意文字扩充A区,总计收容6,582个中日韩汉字。
4E00~9FFFh:中日韩认同表意文字区,总计收容20,902个中日韩汉字。
A000~A4FFh:彝族文字区,收容中国南方彝族文字和字根。
AC00~D7FFh:韩文拼音组合字区,收容以韩文音符拼成的文字。
F900~FAFFh:中日韩兼容表意文字区,总计收容302个中日韩汉字。
FB00~FFFDh:文字表现形式区,收容组合拉丁文字、希伯来文、阿拉伯文、中日韩直式标点、小符号、半角符号、全角符号等。
比如需要匹配所有中日韩非符号字符,那么正则表达式应该是^[\u3400-\u9FFF]+$
理论上没错, 可是我到msn.co.ko随便复制了个韩文下来, 发现根本不对, 诡异
再到msn.co.jp复制了个’お’, 也不得行..
然后把范围扩大到^[\u2E80-\u9FFF]+$, 这样倒是都通过了, 这个应该就是匹配中日韩文字的正则表达式了, 包括我們臺灣省還在盲目使用的繁體中文
而关于中文的正则表达式, 应该是^[\u4E00-\u9FFF]+$, 和论坛里常被人提起的^[\u4E00-\u9FA5]+$很接近
需要注意的是论坛里说的^[\u4E00-\u9FA5]+$这是专门用于匹配简体中文的正则表达式, 实际上繁体字也在里面, 我用测试器测试了下’中華人民共和國’, 也通过了, 当然, ^[\u4E00-\u9FFF]+$也是一样的结果

京ICP备05053527号
经过40次查询历时8.411秒终于生成了此页面
Powered by WordPress & Designed by Felix © 2008