📄 formmain.cs
字号:
CheckOnlineState();//检测自己的在线状态
foreach(LanMsg.ClassUserInfo userinfo in this.MyUsers)//向对方打洞
if(userinfo.State!=0)
this.sendMsgToOneUser(new Controls.ClassMsg(8,this.selfInfo.ID,null),userinfo.IP,userinfo.Port);//向对方打洞
}
private int OnlineState=1;
private void CheckOnlineState()//检测自己的在线状态
{
if(selfInfo.State ==0)//如果没有登录,则登录
{
Login();
}
else//如果已经登录在线,则将自己设为脱机状态,然后向服务器发送消息告之自已在线状态
{
OnlineState=selfInfo.State;
selfInfo.State=0;
LanMsg.Controls.ClassMsg msg=new LanMsg.Controls.ClassMsg(2,selfInfo.ID,System.Text.Encoding.Unicode.GetBytes(OnlineState.ToString()));
sendMsgToServer(msg);
}
}
public void sendMsgToServer(Controls.ClassMsg msg)//发送消息到服务器
{
this.sockUDP1.Send(this.ServerIP,this.ServerPort,new ClassSerializers().SerializeBinary(msg).ToArray());
}
public void sendMsgToOneUser(Controls.ClassMsg msg,System.Net.IPAddress Ip, int Port)//发送消息到用户的一个联系人
{
msg.ID=selfInfo.ID;//标识本人的ID号
this.sockUDP1.Send(Ip,Port,new ClassSerializers().SerializeBinary(msg).ToArray());
}
private void TvUsers_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
foreach(System.Windows.Forms.TreeNode nd in e.Node.Nodes)
nd.Checked=e.Node.Checked;
}
private void butMsgSendGourp_Click(object sender, System.EventArgs e)
{
foreach(System.Windows.Forms.Form form in forms)
if( form is LanMsg.FormNotice )
{
form.Activate();
return;
}
FormSendMsgGroup fSendMsgGroup=new FormSendMsgGroup();
fSendMsgGroup.Tag="Notice";
forms.add(fSendMsgGroup);
fSendMsgGroup.Show ();
this.TvUsers.CheckBoxes=true;
}
public void sendMsgToOneUser(Controls.ClassMsg msg,string userID)//发送消息到用户的一个联系人
{
msg.ID=selfInfo.ID;//标识本人的ID号
ClassUserInfo userinfo=this.findUser(userID);
if(userinfo!=null)
this.sockUDP1.Send(userinfo.IP ,userinfo.Port ,new ClassSerializers().SerializeBinary(msg).ToArray());
}
private void sendMsgToAllUser(Controls.ClassMsg msg)//发送通知消息(群发)
{
msg.ID=selfInfo.ID;//标识本人的ID号
foreach(ClassUserInfo userinfo in this.MyUsers )
{
this.sockUDP1.Send(userinfo.IP ,userinfo.Port ,new ClassSerializers().SerializeBinary(msg).ToArray());
}
}
public void sendNotice(Controls.ClassMsg msg)//发送通知消息(群发)
{
msg.ID=selfInfo.ID;//标识本人的ID号
foreach(ClassUserInfo userinfo in this.MyUsers )
{
if(userinfo.Node.Checked)
this.sockUDP1.Send(userinfo.IP ,userinfo.Port ,new ClassSerializers().SerializeBinary(msg).ToArray());
}
}
private System.Drawing.Point point=new Point(0,0);
private void TvUsers_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if(e.Node.Parent==null)
{
foreach(System.Windows.Forms.TreeNode nd in this.TvUsers.Nodes)
nd.Collapse();
e.Node.Expand();
}
else
{
this.butSendMsg.Visible=true;
this.butSendFile.Visible=true;
this.butOpenShared.Visible=true;
this.butOpenShared.Tag=e.Node.Tag;
this.butSendFile.Tag=e.Node.Tag;
butMenuMain.PopupMenu(this.Left + this.point.X+10 ,this.Top + this.point.Y+50 );
}
}
private void LabselfName_TextChanged(object sender, System.EventArgs e)
{
this.NotifyIcon.Text=this.LabselfName.Text;
}
private void butMsgMis_Click(object sender, System.EventArgs e)
{
OpenMsgMis("");//打开消息管理器
}
public void OpenMsgMis(string OpenID)//打开消息管理器
{
foreach(System.Windows.Forms.Form form in forms)
if (form is LanMsg.FormMsgMis)
{
LanMsg.FormMsgMis f=(form as LanMsg.FormMsgMis);
f.Activate ();
f.selectID(OpenID);
return;
}
FormMsgMis fr=new FormMsgMis();
this.forms.add(fr);
fr.Show();
fr.Activate();
fr.selectID(OpenID);
}
#region 添加记录到数据库
public void MsgAddToDB(string msgContent,string sendID,string ReceiveID,string AssemblyVersion,string msgDateTime,string ImageInfo,bool IsNotNotice)//添加用户对话消息到数据库
{
try
{
string DBtable="Notice";
if(IsNotNotice)
{
DBtable="MsgRecord";
}
System.Data.OleDb.OleDbConnection connection=new System.Data.OleDb.OleDbConnection(new ClassFormMain().ConStr);
System.Data.OleDb.OleDbCommand command=new System.Data.OleDb.OleDbCommand("INSERT INTO ["+DBtable+"](msgContent,sendID,ReceiveID,AssemblyVersion,msgDateTime,ImageInfo) VALUES( @msgContent,@sendID,@ReceiveID ,@AssemblyVersion,@msgDateTime,@ImageInfo)", connection );
System.Data.OleDb.OleDbParameter paramMsgContent =new System.Data.OleDb.OleDbParameter("@msgContent",System.Data.OleDb.OleDbType.LongVarChar);
paramMsgContent.Value = msgContent;
command.Parameters.Add(paramMsgContent);
System.Data.OleDb.OleDbParameter paramSendID=new System.Data.OleDb.OleDbParameter("@sendID",System.Data.OleDb.OleDbType.Char);
paramSendID.Value = sendID;
command.Parameters.Add(paramSendID);
System.Data.OleDb.OleDbParameter paramReceiveID=new System.Data.OleDb.OleDbParameter("@ReceiveID",System.Data.OleDb.OleDbType.Char);
paramReceiveID.Value = ReceiveID;
command.Parameters.Add(paramReceiveID);
System.Data.OleDb.OleDbParameter paramAssemblyVersion=new System.Data.OleDb.OleDbParameter("@AssemblyVersion",System.Data.OleDb.OleDbType.Char);
paramAssemblyVersion.Value = AssemblyVersion;
command.Parameters.Add(paramAssemblyVersion);
System.Data.OleDb.OleDbParameter paramMsgDateTime=new System.Data.OleDb.OleDbParameter("@msgDateTime",System.Data.OleDb.OleDbType.DBTimeStamp);
paramMsgDateTime.Value =msgDateTime;
command.Parameters.Add(paramMsgDateTime);
System.Data.OleDb.OleDbParameter paramImageInfo=new System.Data.OleDb.OleDbParameter("@ImageInfo",System.Data.OleDb.OleDbType.LongVarChar);
paramImageInfo.Value =ImageInfo;
command.Parameters.Add(paramImageInfo);
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
}
catch//如果有错误发生
{
}
}
#endregion
[System.Runtime.InteropServices.DllImport("user32")]
private static extern long AnimateWindow(long hwnd, long dwTime, long dwFlags);
[System.Runtime.InteropServices.DllImport("winmm.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private static extern int PlaySound(string lpszSoundName, int hModule, int dwFlags);
const int SND_FILENAME = 131072;
const int SND_ALIAS = 65536;
const int SND_SYNC = 0;
private void PlaySound(string fileStr)
{
PlaySound(fileStr, 0, SND_FILENAME);
}
private void buttonItemState1_Click(object sender, System.EventArgs e)
{
CheckButFalse(sender);
this.OnlineState=1;
updateSelfState();
}
private void buttonItemState2_Click(object sender, System.EventArgs e)
{
CheckButFalse(sender);
this.OnlineState=2;
updateSelfState();
}
private void buttonItemState3_Click(object sender, System.EventArgs e)
{
CheckButFalse(sender);
this.OnlineState=3;
updateSelfState();
}
private void buttonItemState4_Click(object sender, System.EventArgs e)
{
CheckButFalse(sender);
this.OnlineState=4;
updateSelfState();
}
private void buttonItemState5_Click(object sender, System.EventArgs e)
{
CheckButFalse(sender);
this.OnlineState=5;
updateSelfState();
}
private void CheckButFalse(object sender)
{
buttonItemState1.Checked=false;
buttonItemState2.Checked=false;
buttonItemState3.Checked=false;
buttonItemState4.Checked=false;
buttonItemState5.Checked=false;
(sender as DevComponents.DotNetBar.ButtonItem).Checked=true;
}
private void NotifyIcon_Click(object sender, System.EventArgs e)
{
this.Opacity=100;
this.ShowInTaskbar=true;
this.WindowState=System.Windows.Forms.FormWindowState.Normal;
this.Show();
this.Activate();
this.Refresh();
}
private void FormMain_SizeChanged(object sender, System.EventArgs e)
{
}
private void ShowNotifyIcon(int ShowClass,string title,string content)
{
if(title=="")title="新消息";
LanMsg.CustomUIControls.TaskbarNotifier taskbarNotifier=new TaskbarNotifier();
switch(ShowClass)
{
case 1:
taskbarNotifier.SetBackgroundBitmap(new Bitmap(GetType(),"Resources.skin.bmp"),Color.FromArgb(255,0,255));
taskbarNotifier.SetCloseBitmap(new Bitmap(GetType(),"Resources.close.bmp"),Color.FromArgb(255,0,255),new Point(127,8));
taskbarNotifier.TitleRectangle=new Rectangle(40,9,70,25);
taskbarNotifier.ContentRectangle=new Rectangle(8,41,133,68);
break;
case 2:
taskbarNotifier.SetBackgroundBitmap(new Bitmap(GetType(),"Resources.skin2.bmp"),Color.FromArgb(255,0,255));
taskbarNotifier.SetCloseBitmap(new Bitmap(GetType(),"Resources.close2.bmp"),Color.FromArgb(255,0,255),new Point(300,74));
taskbarNotifier.TitleRectangle=new Rectangle(123,80,176,16);
taskbarNotifier.ContentRectangle=new Rectangle(116,97,197,22);
break;
case 3:
taskbarNotifier.SetBackgroundBitmap(new Bitmap(GetType(),"Resources.skin3.bmp"),Color.FromArgb(255,0,255));
taskbarNotifier.SetCloseBitmap(new Bitmap(GetType(),"Resources.close.bmp"),Color.FromArgb(255,0,255),new Point(280,57));
taskbarNotifier.TitleRectangle=new Rectangle(150, 57, 125, 28);
taskbarNotifier.ContentRectangle=new Rectangle(75, 92, 215, 55);
break;
}
taskbarNotifier.Show(title ,content,500,3000,500);
}
private void TvUsers_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.point.X =e.X ;
this.point.Y =e.Y;
}
private void butOpenShared_Click(object sender, System.EventArgs e)
{
try
{
string sharedComputerName="\\\\"+ Convert.ToString(this.butOpenShared.Tag);
System.Diagnostics.Process.Start(sharedComputerName);
}
catch
{
MessageBox.Show("无法打开对方("+ Convert.ToString(this.butOpenShared.Tag) +")的共享文件夹(原因可能是对方没有开机或没有设置共享以及其它网络故障造成的)。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void butMenuMain_PopupClose(object sender, System.EventArgs e)
{
this.butOpenShared.Visible=false;
this.butSendFile.Visible=false;
this.butSendMsg.Visible=false;
}
private void butSendMsg_Click(object sender, System.EventArgs e)
{
ActivateOrCreateFormSend();
}
private void butSendFile_Click(object sender, System.EventArgs e)//发送文件
{
System.Windows.Forms.OpenFileDialog fd=new OpenFileDialog();
fd.Title="选择要发送的文件";
fd.Filter="所有文件(*.*)|*.*";
if (fd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
ClassUserInfo userinfo =this.findUser(butSendFile.Tag.ToString());
if (userinfo!=null)
{
foreach(System.Windows.Forms.Form form in forms)
if (form.Tag.ToString() == userinfo.ID )
{
FormSendMsg f=(form as FormSendMsg );
f.sendFileRequest(fd.FileName );
f.Activate ();
return;
}
FormSendMsg newf =new FormSendMsg();
newf.Tag=userinfo.ID;
newf.Text="与 "+ userinfo.UserName+"("+userinfo.ID+") 对话";
newf.sendFileRequest(fd.FileName );
forms.add(newf);
newf.Show();
}
}
}
private void ButAbout_Click(object sender, System.EventArgs e)
{
MessageBox.Show("LanMsg局域网即时通讯\n作者:租李叶(25348855)");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -