很久没发代码了,今天来发些C#代码

// 南京千里独行 2005-3-17

	///
1<summary>
2    	/// 进度信息处理委托
3    	/// </summary>

///

1<param name="CompletedStep" type="int"/>

已经完成的步骤数 ///

1<param name="TotalStep" type="int"/>

总的步骤数 public delegate void ProgressHandler( int CompletedStep , int TotalStep );

	///
1<summary>
2    	/// 通用函数集合
3    	/// </summary>

public class YYFCommon { ///

1<summary>
2    		/// 向指定URL使用POST方法发送数据的例程,本函数不进行错误处理
3    		/// </summary>

///

1<param name="strURL"/>

URL字符串 ///

1<param name="bytSend"/>

要发送的二进制数据 ///

1<param name="SendProgress"/>

发送数据时的进度处理 ///

1<param name="AcceptProgress"/>

接受数据时的进度处理 ///

1<returns>接受到的二进制数据</returns>

public static byte[] HttpPostData( string strURL , byte[] bytSend , ProgressHandler SendProgress , ProgressHandler AcceptProgress ) { // 发送数据 System.Net.HttpWebRequest myReq =(System.Net.HttpWebRequest) System.Net.WebRequest.Create( strURL ); myReq.Method = "POST" ; System.IO.Stream myStream = myReq.GetRequestStream(); int iCount = 0 ; if( SendProgress != null) SendProgress( 0 , bytSend.Length ); while( iCount < bytSend.Length ) { if( iCount + 1024 > bytSend.Length) { myStream.Write(bytSend, iCount , bytSend.Length - iCount ); iCount = bytSend.Length ; } else { myStream.Write(bytSend , iCount , 1024); iCount += 1024; } if( SendProgress != null) SendProgress( iCount , bytSend.Length ); }//while if( SendProgress != null) SendProgress( bytSend.Length , bytSend.Length ); myStream.Close();

			// 接受数据
			System.Net.HttpWebResponse myRes = null;
			myRes = myReq.GetResponse() as System.Net.HttpWebResponse ;
				
			myStream = myRes.GetResponseStream();
			System.IO.MemoryStream myBuf = new System.IO.MemoryStream(1024);
			byte[] bytBuf = new byte[1024];
			int ContentLength = (int)myRes.ContentLength ;
			int AcceptLength = 0 ;
			if( AcceptProgress != null)
				AcceptProgress(0 , ContentLength );
			while(true)
			{
				int iLen = myStream.Read(bytBuf,0,1024);
				if(iLen ==0)
					break;
				myBuf.Write(bytBuf,0,iLen);
				AcceptLength += iLen ;
				if( AcceptLength &gt; ContentLength )
					ContentLength = AcceptLength ;
				if( AcceptProgress != null)
					AcceptProgress( AcceptLength , ContentLength );
			}//while
			if( AcceptProgress != null)
				AcceptProgress( AcceptLength , ContentLength );
			myStream.Close();
			myRes.Close();
			myReq.Abort();
			byte[] bytReturn = myBuf.ToArray();
			myBuf.Close();
			return bytReturn ;
		}// public static byte[] HttpPostData()
		
		
		///
1<summary>
2    		/// 根据保存在一个列表中的数据源参数修正字符串
3    		/// </summary>

///

1<param name="strText"/>

供处理的原始字符串 ///

1<param name="strHead"/>

标记的头字符串 ///

1<param name="strEnd"/>

标记尾字符串 ///

1<param name="myKeys"/>

保存所有参数的列表 ///

1<returns>处理后的字符串</returns>

public static string fixVariableString ( string strText, string strHead, string strEnd, System.Collections.Hashtable myKeys ) { // 若原始字符串无效或者没有任何可用的参数则退出函数 if( strText == null || strHead == null || strEnd == null || strHead.Length == 0 || strEnd.Length == 0 || strText.Length == 0 || myKeys == null || myKeys.Count == 0 ) return strText ;

			int 	index = strText.IndexOf( strHead );
			// 若原始字符串没有变量标记则退出函数
			if(index &lt; 0 )
				return strText ;
			
			string 	strKey ;
			int 	index2 ;
			int 	LastIndex = 0 ;
			System.Text.StringBuilder myStr = new System.Text.StringBuilder();
			do
			{	
				// 查找有 "[内容]" 样式的子字符串
				// 若没有找到 "[" 和 "]"的字符对则退出循环
				index2 = strText.IndexOf( strEnd ,  index + 1  );
				if(index2 &gt; index)
				{
					// 若 "[" 符号后面出现 "]"符号则存在 "[]"字符对
					// 修正查找结果以保证 "[]"字符对中不出现字符 "["
					int index3 = index ;
					do
					{
						index = index3 ;
						index3 = strText.IndexOf(strHead, index3 + 1 );
					}while( index3 &gt; index &amp;&amp; index3 &lt; index2 ) ;
				
					// 获得字符对夹着的子字符串,该子字符串为参数名
					// 若该参数名有效则向输出结果输出参数值
					// 否则不进行额外的处理
					strKey = strText.Substring(index + strHead.Length ,  index2 - index - strHead.Length  );
					if( myKeys.ContainsKey( strKey ))
					{	
						if(LastIndex &lt; index)
						{
							myStr.Append( strText.Substring(LastIndex, index - LastIndex ));
						}
						myStr.Append( myKeys[strKey] as string );
						index = index2 +  strEnd.Length ;
						LastIndex = index ;
					}
					else
						index = index2 + strEnd.Length ;
				}
				else
				{
					break;
				}
			}while( index &gt;=0 &amp;&amp; index &lt; strText.Length );
			// 添加处理过后剩余的字符串
			if(LastIndex &lt; strText.Length   )
				myStr.Append( strText.Substring(LastIndex));
			return myStr.ToString();	
		}// End of function : fixVariableString
		
		
		///
1<summary>
2    		/// 计算指定矩形的拖拽控制矩形
3    		/// </summary>

///

1<param name="myRect"/>

主矩形区域 ///

1<param name="DragRectSize"/>

拖拽矩形的大小 ///

1<param name="InnerDragRect"/>

拖拽矩形是否在主矩形内部,若为false则拖拽矩形外翻 ///

 1<remarks>
 2    		/// 拖拽矩形主要用于有用户参与的图形化用户界面,在一个矩形区域的的4个顶点和边框中间点共有8个控制点
 3    		/// 用户使用鼠标拖拽操作来拖动这8个控制点可以用于改变矩形区域的位置和大小,这些控制点可以在区域区域的内部,
 4    		/// 也可在矩形区域的外部,拖拽矩形有8个,分别编号从0至7
 5    		/// <pre>
 6    		///               内拖拽矩形
 7    		/// ┌─────────────────┐
 8    		/// │■0            1■             2■│
 9    		/// │                                  │
10    		/// │                                  │
11    		/// │                                  │
12    		/// │                                  │
13    		/// │■7                            3■│
14    		/// │                                  │
15    		/// │                                  │
16    		/// │                                  │
17    		/// │                                  │
18    		/// │■6           5■              4■│
19    		/// └─────────────────┘
20    		///
21    		///              外拖拽矩形
22    		///
23    		/// ■               ■                  ■
24    		///   ┌────────────────┐
25    		///   │0            1                 2│
26    		///   │                                │
27    		///   │                                │
28    		///   │                                │
29    		///   │                                │
30    		/// ■│7                              3│■
31    		///   │                                │
32    		///   │                                │
33    		///   │                                │
34    		///   │                                │
35    		///   │6             5               4 │
36    		///   └────────────────┘
37    		/// ■                ■                 ■
38    		/// </pre>
39    		/// </remarks>

///

1<returns>拖拽矩形的数组,有8个元素</returns>

public static System.Drawing.Rectangle[] GetDragRects(System.Drawing.Rectangle myRect , int DragRectSize , bool InnerDragRect) { System.Drawing.Rectangle[] DragRects = new System.Drawing.Rectangle[8]; if( InnerDragRect) { DragRects[0] = new System.Drawing.Rectangle( myRect.X , myRect.Y , DragRectSize , DragRectSize ); DragRects[1] = new System.Drawing.Rectangle( myRect.X + (int)((myRect.Width - DragRectSize)/2) , myRect.Y , DragRectSize , DragRectSize ); DragRects[2] = new System.Drawing.Rectangle( myRect.Right - DragRectSize , myRect.Y , DragRectSize , DragRectSize ); DragRects[3] = new System.Drawing.Rectangle( myRect.Right - DragRectSize , myRect.Y + (int)(( myRect.Height - DragRectSize)/2) , DragRectSize , DragRectSize ); DragRects[4] = new System.Drawing.Rectangle( myRect.Right - DragRectSize , myRect.Bottom - DragRectSize , DragRectSize , DragRectSize ); DragRects[5] = new System.Drawing.Rectangle( myRect.X + (int)((myRect.Width - DragRectSize)/2) , myRect.Bottom - DragRectSize , DragRectSize , DragRectSize ); DragRects[6] = new System.Drawing.Rectangle( myRect.X , myRect.Bottom - DragRectSize , DragRectSize , DragRectSize ); DragRects[7] = new System.Drawing.Rectangle( myRect.X , myRect.Y + (int)(( myRect.Height - DragRectSize)/2 ) , DragRectSize , DragRectSize ); } else { DragRects[0] = new System.Drawing.Rectangle( myRect.X - DragRectSize , myRect.Y - DragRectSize , DragRectSize , DragRectSize ); DragRects[1] = new System.Drawing.Rectangle( myRect.X + (int)((myRect.Width - DragRectSize)/2) , myRect.Y - DragRectSize , DragRectSize , DragRectSize ); DragRects[2] = new System.Drawing.Rectangle( myRect.Right , myRect.Y - DragRectSize , DragRectSize , DragRectSize ); DragRects[3] = new System.Drawing.Rectangle( myRect.Right , myRect.Y + (int)(( myRect.Height - DragRectSize)/2) , DragRectSize , DragRectSize ); DragRects[4] = new System.Drawing.Rectangle( myRect.Right , myRect.Bottom , DragRectSize , DragRectSize ); DragRects[5] = new System.Drawing.Rectangle( myRect.X + (int)((myRect.Width - DragRectSize)/2) , myRect.Bottom , DragRectSize , DragRectSize ); DragRects[6] = new System.Drawing.Rectangle( myRect.X - DragRectSize , myRect.Bottom , DragRectSize , DragRectSize ); DragRects[7] = new System.Drawing.Rectangle( myRect.X - DragRectSize , myRect.Y + (int)(( myRect.Height - DragRectSize)/2 ) , DragRectSize , DragRectSize ); } return DragRects ; }

		///
1<summary>
2    		/// 计算拖拉矩形上的鼠标光标位置
3    		/// </summary>

///

 1<remarks>
 2    		/// 鼠标设置如下
 3    		/// 西北-东南          南北                东北-西南
 4    		///	   ■               ■                  ■
 5    		///     ┌────────────────┐
 6    		///     │0            1                 2│
 7    		///     │                                │
 8    		///     │                                │
 9    		///     │                                │
10    		///     │                                │
11    		///   ■│7 西-南                        3│■ 西-南
12    		///     │                                │
13    		///     │                                │
14    		///     │                                │
15    		///     │                                │
16    		///     │6             5               4 │
17    		///     └────────────────┘
18    		///   ■                ■                 ■
19    		/// 东北-西南          南北                   西北-东南
20    		/// </remarks>

///

1<param name="index"/>

拖拽矩形的序号,从0至7 ///

1<returns>鼠标光标对象,若序号小于0或大于7则返回空引用</returns>

public static System.Windows.Forms.Cursor GetDragRectCursor( int index ) { switch(index) { case 0: return System.Windows.Forms.Cursors.SizeNWSE ; case 1: return System.Windows.Forms.Cursors.SizeNS ; case 2: return System.Windows.Forms.Cursors.SizeNESW ; case 3: return System.Windows.Forms.Cursors.SizeWE ; case 4: return System.Windows.Forms.Cursors.SizeNWSE ; case 5: return System.Windows.Forms.Cursors.SizeNS ; case 6: return System.Windows.Forms.Cursors.SizeNESW ; case 7: return System.Windows.Forms.Cursors.SizeWE ; } return null; } }

	///
1<summary>
2    	/// 操作系统剪切板处理模块,提供的方法为静态函数
3    	/// </summary>

///

 1<example>
 2    	/// C#语言中使用该类的例子,从操作系统剪切板获得纯文本数据
 3    	/// // 判断操作系统剪切板是否保存了纯文本数据
 4    	/// if( ClipboardHandler.CanGetText())
 5    	/// {
 6    	///		// 返回获得的纯文本数据
 7    	///		return ClipboardHandler.GetTextFromClipboard();
 8    	/// }
 9    	///
10    	/// 向操作系统剪切板设置纯文本数据
11    	/// string strText = "要设置的纯文本数据";
12    	/// ClipboardHandler.SetTextToClipboard( strText );
13    	/// </example>

public class ClipboardHandler {

		///
1<summary>
2    		/// 是否可以从操作系统剪切板获得文本
3    		/// </summary>

///

1<returns>true 可以从操作系统剪切板获得文本,false 不可以</returns>

public static bool CanGetText() { // Clipboard.GetDataObject may throw an exception... try { System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject(); return data != null && data.GetDataPresent(System.Windows.Forms.DataFormats.Text); } catch (Exception e) { return false; } } // // ///

1<summary>
2    //		/// 是否可以向操作系统剪切板设置文本
3    //		/// </summary>

// ///

1<returns></returns>

// public static bool CanSetText() // { // return true; // }

		///
1<summary>
2    		/// 向操作系统剪切板设置文本数据
3    		/// </summary>

///

1<param name="strText"/>

文本数据 ///

1<returns>操作是否成功</returns>

public static bool SetTextToClipboard(string strText) { if ( strText != null && strText.Length > 0 ) { try { System.Windows.Forms.DataObject dataObject = new System.Windows.Forms.DataObject(); dataObject.SetData(System.Windows.Forms.DataFormats.UnicodeText , true, strText ); System.Windows.Forms.Clipboard.SetDataObject(dataObject, true); return true; } catch {

				}
			}
			return false;
		}

		///
1<summary>
2    		/// 从操作系统剪切板获得文本
3    		/// </summary>

///

1<returns>获得的文本,若操作失败则返回空对象</returns>

public static string GetTextFromClipboard() { try { System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject(); if( data.GetDataPresent(System.Windows.Forms.DataFormats.UnicodeText)) { string strText = ( string) data.GetData( System.Windows.Forms.DataFormats.UnicodeText); return strText; } } catch {} return null; } }

Published At
Categories with Web编程
Tagged with
comments powered by Disqus