// 南京千里独行 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;
}
}