很久沒發代碼了,今天來發些C#代碼

來源:互聯網
上載者:User
// 南京千裡獨行 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 );}//whileif( 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 );}//whileif( 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 ;}elseindex = 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則返回Null 參考</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>獲得的文本,若操作失敗則返回Null 物件</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;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.