Wednesday, January 27, 2010

Common.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

using System.Text.RegularExpressions;
namespace PowerTalkClient
{
public class Common
{
///
/// 数据库联接
///

public static OleDbConnection OleConn
{
get {
return new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Jet OLEDB:Database Password=;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\IP.mdb"); //Access数据库连接字符串
}
}
///
/// 数据库联接
///

public static OleDbConnection OleSysConn
{
get
{
return new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Jet OLEDB:Database Password=powertalkbox;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\PowerData.mdb"); //Access数据库连接字符串
}
}
///
/// 获取真实地址
///

public static string GetAddr(string IPStr)
{

string DiZhi = "未知网络";
OleDbConnection Conn = OleConn;
try
{
string IP = IPToInt(IPStr).ToString();

Conn.Open();
object Obj = new OleDbCommand("select top 1 region from ip where IPN1<=" + IP + " and IPN2>=" + IP, Conn).ExecuteScalar();
if (Obj != null)
{
DiZhi = Obj.ToString();
}
}
catch { }
finally { Conn.Close(); }
return DiZhi;
//5-1-a-s-p-x
}

///
/// IP地址和数字之间转化的算法

///

/// 传入IP地址
///

public static uint IPToInt(string ipAddress)
{
string disjunctiveStr = ".,: ";
char[] delimiter = disjunctiveStr.ToCharArray();
string[] startIP = null;
for (int i = 1; i <= 5; i++)
{
startIP = ipAddress.Split(delimiter, i);
}
string a1 = startIP[0].ToString();
string a2 = startIP[1].ToString();
string a3 = startIP[2].ToString();
string a4 = startIP[3].ToString();
uint U1 = uint.Parse(a1);
uint U2 = uint.Parse(a2);
uint U3 = uint.Parse(a3);
uint U4 = uint.Parse(a4);
uint U = U1 << 24;
U += U2 << 16;
U += U3 << 8;
U += U4;
return U;
}
///
/// 数字和IP地址之间转化的算法
///

/// 传入IP地址
///
public static string IntToIP(uint ipAddress)
{
long ui1 = ipAddress & 0xFF000000;
ui1 = ui1 >> 24;
long ui2 = ipAddress & 0x00FF0000;
ui2 = ui2 >> 16;
long ui3 = ipAddress & 0x0000FF00;
ui3 = ui3 >> 8;
long ui4 = ipAddress & 0x000000FF;
string IPstr = "";
IPstr = System.Convert.ToString(ui1) + "." + System.Convert.ToString(ui2) + "." + System.Convert.ToString(ui3) + "." + System.Convert.ToString(ui4);
return IPstr;
}

///
/// 转换成为MSN表情
///

///
public static string MatchImg(string MatchStr)
{
string regstr = @"src\=.+?\.(gif|jpg|png|bmp)";
Regex myrg = new Regex(regstr);
Match mt = myrg.Match(MatchStr, 0);
int i = 0;
while (mt.Success)
{
string Values = mt.Value;
string Mtch = Values;
Values = Regex.Replace(Values, "src=", "", RegexOptions.IgnoreCase);
Values = Regex.Replace(Values, "‘", "", RegexOptions.IgnoreCase);
Values = Regex.Replace(Values, "\"", "", RegexOptions.IgnoreCase);
Values = Regex.Replace(Values, "'", "", RegexOptions.IgnoreCase);
Values = Regex.Replace(Values, "’", "", RegexOptions.IgnoreCase);

Values = Values.Substring(Values.LastIndexOf(@"/") + 1);

MatchStr = MatchStr.Replace("", ToMsnFace(Values));

mt = mt.NextMatch();
}
return MatchStr;
}
//5~1~a~s~p~x
///
/// 从Image转换为Msn的头像
///

/// 头像图片名称
///
private static string ToMsnFace(string faceimg)
{
string MsnFace = "";
switch (faceimg)
{

case "img1.gif":
MsnFace = ":)";
break;
case "img2.gif":
MsnFace = ":-O";
break;
case "img3.gif":
MsnFace = ":P";
break;
case "img4.gif":
MsnFace = "(H)";
break;
case "img5.gif":
MsnFace = ":@";
break;
case "img6.gif":
MsnFace = ":S";
break;
case "img7.gif":
MsnFace = ":$";
break;
case "img8.gif":
MsnFace = ":'(";
break;
case "img9.gif":
MsnFace = ":|";
break;
case "img10.gif":
MsnFace = "(A)";
break;
case "img11.gif":
MsnFace = "8o|";
break;
case "img12.gif":
MsnFace = "8-|";
break;
case "img13.gif":
MsnFace = "+o(";
break;
case "img14.gif":
MsnFace = "<:o)";
break;
case "img15.gif":
MsnFace = "|-)";
break;
case "img16.gif":
MsnFace = "*-)";
break;
case "img17.gif":
MsnFace = ":-#";
break;
case "img18.gif":
MsnFace = ":-*";
break;
case "img19.gif":
MsnFace = "^o)";
break;
case "img20.gif":
MsnFace = "8-)";
break;
case "img21.gif":
MsnFace = "(L)";
break;
case "img22.gif":
MsnFace = "(U)";
break;
case "img23.gif":
MsnFace = "(M)";
break;
case "img24.gif":
MsnFace = "(@)";
break;
case "img25.gif":
MsnFace = "(&)";
break;
case "img26.gif":
MsnFace = "(sn)";
break;
case "img27.gif":
MsnFace = "(bah)";
break;
case "img28.gif":
MsnFace = "(S)";
break;
case "img29.gif":
MsnFace = "(*)";
break;
case "img30.gif":
MsnFace = "(#)";
break;
case "img31.gif":
MsnFace = "(R)";
break;
case "img32.gif":
MsnFace = "({)";
break;
case "img33.gif":
MsnFace = "(})";
break;
case "img34.gif":
MsnFace = "(K)";
break;
case "img35.gif":
MsnFace = "(F)";
break;
case "img36.gif":
MsnFace = "(W)";
break;
case "img37.gif":
MsnFace = "(O)";
break;
case "img38.gif":
MsnFace = ";)";
break;
case "img39.gif":
MsnFace = ":D";
break;
case "img40.gif":
MsnFace = ":(";
break;
default:
MsnFace = ":)";
break;


}
return MsnFace;
}

}

//5~1-a^s^p^x
public enum Action
{
///
/// 鼓掌
///

CONGRATULATE,
///
/// 查找
///

CHECKINGSOMETHING,
///
/// 眨眼
///

DEEPIDLE1,
///
/// 缩小
///

HIDE,
///
/// 销毁
///

EMPTYTRASH,
///
/// 手势•下
///

GESTUREDOWN,
///
/// 手势•左
///

GESTURELEFT,
///
/// 手势•右
///

GESTURERIGHT,
///
/// 手势•上
///

GESTUREUP,
///
/// 靠近屏幕打招呼
///

GETATTENTION,
///
/// 感汉号
///

WAVE,
///
/// 鲜花
///

GETTECHY,
///
/// 鞠躬
///

GETWIZARDY,
///
/// 小助手下班
///

GOODBYE,
///
/// 小助手上班
///

GREETING,
///
/// 想一下
///

ALERT,
///
/// 往下看
///

LOOKDOWN,
///
/// 往左下看
///

LOOKDOWNLEFT,
///
/// 往右下看
///

LOOKDOWNRIGHT,
///
/// 往左看
///

LOOKLEFT,
///
/// 往右看
///

LOOKRIGHT,
///
/// 往上看
///

LOOKUP,
///
/// 往左上看
///

LOOKUPLEFT,
///
/// 往右上看
///

LOOKUPRIGHT,
///
/// 打印
///

PRINT,
///
/// 动作还原
///

RESTPOSE,
///
/// 保存
///

SAVE,
///
/// 后面找东西
///

SEARCHING,
///
/// 邮件
///

SENDMAIL,
///
/// 思考
///

THINKING,
///
/// 挥手再见
///

PROCESSING,
///
/// 写东西
///

WRITING,
}

public class 助手
{
Timer timer = null;
Timer speakTimer = null;

Action _Action;
public Action Action
{
get { return _Action; }
set
{
if (value.ToString() != "")
{
_Action = value;
this.Character.Stop(null);
Character.Play(value.ToString());
}
}
}

AgentObjects.IAgentCtlCharacterEx Character = null;
AxAgentObjects.AxAgent Agent = null;
string Name = string.Empty;
string Path = string.Empty;

Action[] actions = new Action[] {
Action.CONGRATULATE,
Action.DEEPIDLE1,
Action.EMPTYTRASH,
Action.GETATTENTION,
Action.WAVE,
Action.GETTECHY,
Action.GETWIZARDY,
Action.ALERT,
Action.PRINT,
Action.SAVE,
Action.SENDMAIL
};

bool visible = true;
public bool Visible
{
get { return visible; }
set
{
visible = value;
if (visible)
{
Character.Show(Action.GREETING);
timer.Enabled = true;
}
else
{
Character.Hide(Action.GOODBYE);
timer.Enabled = false;
}
}
}

public 助手(ref AxAgentObjects.AxAgent axAgent, string name, string path, bool Visibled)
{
Agent = axAgent;
Name = name;
Path = path;
visible = Visibled;

Agent.Characters.Load(Name, (object)Path);
Character = Agent.Characters[Name];
Character.AutoPopupMenu = false;
Character.Balloon.Style = 3;

Character.Left = (short)(Screen.PrimaryScreen.WorkingArea.Width - 160);
Character.Top = (short)(Screen.PrimaryScreen.WorkingArea.Height - 140);

timer = new Timer();
timer.Interval = 40000;
timer.Tick += new EventHandler(timer_Tick);
timer.Enabled = true;

speakTimer = new Timer();
speakTimer.Interval = 5000;
speakTimer.Tick += new EventHandler(speakTimer_Tick);

if (visible)
{
Character.Show(Action.GREETING);
this.Action = Action.GREETING;
}

}

void speakTimer_Tick(object sender, EventArgs e)
{
if (Character.Balloon.Visible)
{
speakTimer.Enabled = false;
Character.Balloon.Visible = false;
}
}

//随机播放动画
void timer_Tick(object sender, EventArgs e)
{
if (!visible)
return;

this.timer.Enabled = false;

this.Character.Stop(null);

Random rand = new Random();
int number = rand.Next(10000);

this.Action = actions[number % actions.Length];
this.Action = Action.RESTPOSE;
this.timer.Enabled = true;
}

public void Speak(string Text)
{
if (!visible)
return;

this.Character.StopAll(null);
this.Action = Action.RESTPOSE;
this.Character.Speak(Text, "");
timer.Enabled = true;
speakTimer.Enabled = true;
}

public void Think(string Text)
{
if (!visible)
return;

timer.Enabled = false;

this.Character.Think(Text);
timer.Enabled = true;
}

//退出
public void ExitAssistant()
{
this.timer.Enabled = false;
Agent.Characters.Unload(Name);
}
}
}

No comments:

Post a Comment