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

类别:.NET开发 点击:0 评论:0 推荐:

// 南京千里独行 2005-3-17 /// <summary> /// 进度信息处理委托 /// </summary> /// <param name="CompletedStep" type="int">已经完成的步骤数</param> /// <param name="TotalStep" type="int">总的步骤数</param> public delegate void ProgressHandler( int CompletedStep , int TotalStep ); /// <summary> /// 通用函数集合 /// </summary> public class YYFCommon { /// <summary> /// 向指定URL使用POST方法发送数据的例程,本函数不进行错误处理 /// </summary> /// <param name="strURL">URL字符串</param> /// <param name="bytSend">要发送的二进制数据</param> /// <param name="SendProgress">发送数据时的进度处理</param> /// <param name="AcceptProgress">接受数据时的进度处理</param> /// <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 > 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() /// <summary> /// 根据保存在一个列表中的数据源参数修正字符串 /// </summary> /// <param name="strText">供处理的原始字符串</param> /// <param name="strHead">标记的头字符串</param> /// <param name="strEnd">标记尾字符串</param> /// <param name="myKeys">保存所有参数的列表</param> /// <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 < 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 > index) { // 若 "[" 符号后面出现 "]"符号则存在 "[]"字符对 // 修正查找结果以保证 "[]"字符对中不出现字符 "[" int index3 = index ; do { index = index3 ; index3 = strText.IndexOf(strHead, index3 + 1 ); }while( index3 > index && index3 < index2 ) ; // 获得字符对夹着的子字符串,该子字符串为参数名 // 若该参数名有效则向输出结果输出参数值 // 否则不进行额外的处理 strKey = strText.Substring(index + strHead.Length , index2 - index - strHead.Length ); if( myKeys.ContainsKey( strKey )) { if(LastIndex < 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 >=0 && index < strText.Length ); // 添加处理过后剩余的字符串 if(LastIndex < strText.Length ) myStr.Append( strText.Substring(LastIndex)); return myStr.ToString(); }// End of function : fixVariableString /// <summary> /// 计算指定矩形的拖拽控制矩形 /// </summary> /// <param name="myRect">主矩形区域</param> /// <param name="DragRectSize">拖拽矩形的大小</param> /// <param name="InnerDragRect">拖拽矩形是否在主矩形内部,若为false则拖拽矩形外翻</param> /// <remarks> /// 拖拽矩形主要用于有用户参与的图形化用户界面,在一个矩形区域的的4个顶点和边框中间点共有8个控制点 /// 用户使用鼠标拖拽操作来拖动这8个控制点可以用于改变矩形区域的位置和大小,这些控制点可以在区域区域的内部, /// 也可在矩形区域的外部,拖拽矩形有8个,分别编号从0至7 /// <pre> /// 内拖拽矩形 /// ┌─────────────────┐ /// │■0 1■ 2■│ /// │ │ /// │ │ /// │ │ /// │ │ /// │■7 3■│ /// │ │ /// │ │ /// │ │ /// │ │ /// │■6 5■ 4■│ /// └─────────────────┘ /// /// 外拖拽矩形 /// /// ■ ■ ■ /// ┌────────────────┐ /// │0 1 2│ /// │ │ /// │ │ /// │ │ /// │ │ /// ■│7 3│■ /// │ │ /// │ │ /// │ │ /// │ │ /// │6 5 4 │ /// └────────────────┘ /// ■ ■ ■ /// </pre> /// </remarks> /// <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 ; } /// <summary> /// 计算拖拉矩形上的鼠标光标位置 /// </summary> /// <remarks> /// 鼠标设置如下 /// 西北-东南 南北 东北-西南 /// ■ ■ ■ /// ┌────────────────┐ /// │0 1 2│ /// │ │ /// │ │ /// │ │ /// │ │ /// ■│7 西-南 3│■ 西-南 /// │ │ /// │ │ /// │ │ /// │ │ /// │6 5 4 │ /// └────────────────┘ /// ■ ■ ■ /// 东北-西南 南北 西北-东南 /// </remarks> /// <param name="index">拖拽矩形的序号,从0至7</param> /// <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; } } /// <summary> /// 操作系统剪切板处理模块,提供的方法为静态函数 /// </summary> /// <example> /// C#语言中使用该类的例子,从操作系统剪切板获得纯文本数据 /// // 判断操作系统剪切板是否保存了纯文本数据 /// if( ClipboardHandler.CanGetText()) /// { /// // 返回获得的纯文本数据 /// return ClipboardHandler.GetTextFromClipboard(); /// } /// /// 向操作系统剪切板设置纯文本数据 /// string strText = "要设置的纯文本数据"; /// ClipboardHandler.SetTextToClipboard( strText ); /// </example> public class ClipboardHandler { /// <summary> /// 是否可以从操作系统剪切板获得文本 /// </summary> /// <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; } } // // /// <summary> // /// 是否可以向操作系统剪切板设置文本 // /// </summary> // /// <returns></returns> // public static bool CanSetText() // { // return true; // } /// <summary> /// 向操作系统剪切板设置文本数据 /// </summary> /// <param name="strText">文本数据</param> /// <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; } /// <summary> /// 从操作系统剪切板获得文本 /// </summary> /// <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; } }

本文地址:http://com.8s8s.com/it/it41531.htm