// 南京千里獨行 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;
}
}
本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。