-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormMain.cs
1769 lines (1637 loc) · 71.2 KB
/
FormMain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using SpecialEnumeration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Diagnostics;
namespace UChat
{
public partial class FormMain : Form
{
/// <summary>
/// 这是一个声明的 formmain 对象,在本窗口的构造函数中已经将它与本窗口联系在一起。
/// </summary>
public static FormMain formMain;
/// <summary>
/// formmain 的构造函数。
/// </summary>
public FormMain()
{
InitializeComponent();
formMain = this;//把这两个东西联系起来。
ThreadsList = new List<Thread>(); //初始化线程列表
//启用双缓冲,减少窗口控件闪烁
this.DoubleBuffered = true;//设置本窗体
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
}
TCPFileTransfer.TaskCompletionStatus FTRResult;
/// <summary>
/// 一个集合了本程序所有线程的队列,便于程序关闭时结束所有线程。
/// </summary>
/// <returns></returns>
private List<Thread> ThreadsList;
/// <summary>
/// 一个保存局域网活动主机的表格。表头为 UID - Name - IP。
/// </summary>
public static DataTable LANtable = new DataTable();
/// <summary>
/// 初始化 LAN 表。
/// </summary>
private void InitLANTable()
{
LANtable.Columns.Add("UID", typeof(string));
LANtable.Columns.Add("Name", typeof(string));
LANtable.Columns.Add("IP", typeof(string));
DataColumn[] primaryColumns = new DataColumn[1];
primaryColumns[0] = LANtable.Columns["UID"];
LANtable.PrimaryKey = primaryColumns;
//为 LANtable 绑定事件,当发生改变时更新好友列表
LANtable.RowDeleted += new DataRowChangeEventHandler(LANtableRowChanged);
LANtable.RowChanged += new DataRowChangeEventHandler(LANtableRowChanged);
}
/// <summary>
/// 修改 LAN 表。
/// </summary>
public void UpdateLANTable(string uid, string name, string ip, string status)
{
if (status == "UP")//状态字是上线
{
if (IsUIDExist(uid) == false)//表里没有这条佬,加进去
{
DataRow dataRow = LANtable.NewRow();
dataRow["UID"] = uid;
dataRow["Name"] = name;
dataRow["IP"] = ip;
LANtable.Rows.Add(dataRow);
}
}
else//下线
{
//删掉下线那只佬
DeleteUID(uid);
LockInput(uid);
}
}
/// <summary>
/// 遍历查找指定 UID 是否存在。
/// </summary>
/// <param name="uid"></param>
/// <returns></returns>
private bool IsUIDExist(string uid)
{
foreach (DataRow dataRow in LANtable.Rows)
{
if (dataRow[0].ToString() == uid)
{
return true;
}
}
return false;
}
/// <summary>
/// 遍历查找指定 UID 的对应 IP。
/// </summary>
/// <param name="uid"></param>
/// <returns></returns>
public string UIDtoIP(string uid)
{
foreach (DataRow dataRow in LANtable.Rows)
{
if (dataRow[0].ToString() == uid)
{
return dataRow[2].ToString();//返回IP
}
}
return "";
}
/// <summary>
/// 遍历查找指定 UID 的对应名字。
/// </summary>
/// <param name="uid"></param>
/// <returns></returns>
public string UIDtoName(string uid)
{
foreach (DataRow dataRow in LANtable.Rows)
{
if (dataRow[0].ToString() == uid)
{
return dataRow[1].ToString();//返回名字
}
}
return "";
}
/// <summary>
/// 删除指定 UID 所在的表格行。
/// </summary>
/// <param name="uid"></param>
private void DeleteUID(string uid)
{
int count = LANtable.Rows.Count;
for (int i = count - 1; i >= 0; i--)//使用倒序检索并删除
{
if (LANtable.Rows[i][0].ToString() == uid)
{
LANtable.Rows.RemoveAt(i);
break;
}
}
}
/// <summary>
/// 检测下线的人是不是正在聊天的人。如果是,锁定输入。
/// </summary>
/// <param name="uid"></param>
private void LockInput(string uid)
{
if (uid == CommonFoundations.RemoteUID)
{
ForbidInput(true);
CommonFoundations.RemoteIP = "";
CommonFoundations.RemoteName = "";
CommonFoundations.RemoteUID = "";
}
}
/// <summary>
/// 当 LANTable 被更改后,需要调用此事件处理以更新局域网列表。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void LANtableRowChanged(object sender, DataRowChangeEventArgs e)
{
formMain.UpdateLANList();
}
/// <summary>
/// 根据 LANtable 的内容更新局域网列表。支持跨线程。
/// </summary>
private void UpdateLANList()
{
if (panelEmpty.InvokeRequired == false)
{
ClearFriendList();
int count = LANtable.Rows.Count;
if (count == 0)//表格数为0,就是没人上线
{
panelEmpty.Visible = true;//显示空图标
}
else//有人上线
{
panelEmpty.Visible = false;
panels = new Panel[count];
for (int i = 0; i < count; i++)
{
CreatePanels(ref panels[i], LANtable.Rows[i][0].ToString(), LANtable.Rows[i][1].ToString(), i);//重新生成列表
SetDouble(panels[i]);//设置双缓冲
}
}
}
else
{
Action dG_NoParaAndNoReturn = new Action(UpdateLANList);
panelEmpty.Invoke(dG_NoParaAndNoReturn);
}
}
/// <summary>
/// 清空局域网列表。支持跨线程。
/// </summary>
/// <param name="panel"></param>
private void ClearFriendList()
{
if (formMain.panelEmpty.InvokeRequired == false)
{
//panelFriendsBar 清空前需要解绑 panelEmpty,不然会被一起清掉
panelEmpty.Parent = this;
panelLANBar.Controls.Clear();
panelEmpty.Parent = panelLANBar;
}
else
{
Action dG_NoParaAndNoReturn = new Action(ClearFriendList);
panelEmpty.Invoke(dG_NoParaAndNoReturn);
}
}
public static Color enterColor = Color.FromArgb(100, 100, 100);//好友列表鼠标进入的颜色
public static Color downColor = Color.FromArgb(65, 65, 65);//好友列表鼠标按下的颜色
/// <summary>
/// 其中每个 panel 的名字应该以 panel + UID 作为命名。
/// </summary>
public static Panel[] panels;
/// <summary>
/// 为窗口的所有控件实现双缓冲。
/// </summary>
private void ControlsDoubleBuffered()
{
SetDouble(this);
#region
SetDouble(buttonDetail);
SetDouble(buttonMenu);
SetDouble(buttonLAN);
SetDouble(buttonSetting);
SetDouble(buttonExit2);
SetDouble(buttonMin);
SetDouble(buttonFiles);
SetDouble(buttonSendM);
SetDouble(buttonScrollToTheBottom);
SetDouble(buttonSelectFile);
SetDouble(buttonRefuse);
SetDouble(buttonAcceptFTR);
SetDouble(buttonCancelFTR);
SetDouble(buttonConfirmChange);
SetDouble(buttonCancelChange);
SetDouble(buttonChangeName);
SetDouble(buttonChangePW);
SetDouble(buttonClearData);
SetDouble(buttonCancelChangePW);
SetDouble(buttonSavePW);
SetDouble(buttonEnableQSI);
SetDouble(buttonRechooseFolder);
SetDouble(buttonOpenFolder);
SetDouble(buttonRefuse2);
SetDouble(buttonCover);
SetDouble(buttonRechooseFolder);
SetDouble(button1);
SetDouble(labelEmptyText);
SetDouble(label4);
SetDouble(label5);
SetDouble(label3);
SetDouble(label6);
SetDouble(labelAlert);
SetDouble(labelForbid);
SetDouble(labelNameIndicator);
SetDouble(label6);
SetDouble(labelWaiting);
SetDouble(labelPercent);
SetDouble(label8);
SetDouble(label9);
SetDouble(label10);
SetDouble(label12);
SetDouble(label11);
SetDouble(label13);
SetDouble(label14);
SetDouble(label15);
SetDouble(label16);
SetDouble(label17);
SetDouble(label18);
SetDouble(label19);
SetDouble(label20);
SetDouble(labelError);
SetDouble(label21);
SetDouble(label22);
SetDouble(labelSpeed);
SetDouble(labelProgress);
SetDouble(label24);
SetDouble(label23);
SetDouble(label25);
SetDouble(panel2);
SetDouble(panelLANBar);
SetDouble(panelSideBar);
SetDouble(panelTips);
SetDouble(panelConfirm);
SetDouble(panelPercent);
SetDouble(panelSetting);
SetDouble(panelChangeName);
SetDouble(panelInfo);
SetDouble(panelLANBarTitle);
SetDouble(panelChangePW);
SetDouble(panelSameFile);
SetDouble(pictureBoxTips);
SetDouble(pictureBoxEmptyIcon);
SetDouble(pictureBox1);
SetDouble(progressBar1);
SetDouble(richTextBoxChat);
SetDouble(textBoxUID);
SetDouble(textBoxIP);
SetDouble(textBoxName);
SetDouble(textBoxInput);
SetDouble(textBoxChangeName);
SetDouble(textBoxInfoUID);
SetDouble(textBoxInfoIP);
SetDouble(textBoxInfoName);
SetDouble(textBoxNewPW);
SetDouble(textBoxOldPW);
SetDouble(textBoxNewPWRepeat);
#endregion
}
/// <summary>
/// 文件传输线程
/// </summary>
public Thread TCPFileSendThread;
private void FormMain_Load(object sender, EventArgs e)
{
HostInfo hostInfo = new HostInfo();
if (hostInfo.IPv4Address.ToString() == "0.0.0.0")//没有网
{
panelNoConnection.Visible = true;
panelNoConnection.BringToFront();
}
else
{
///UDP监听线程
UDP uDP = new UDP();
Thread UDPListenThread = new Thread(uDP.UDPMessageListener);
ThreadsList.Add(UDPListenThread); //将新建的线程加入到线程队列中,以便在窗体结束时关闭所有的线程
UDPListenThread.Start();
//TCP消息监听线程
TCP tCP = new TCP();
Thread TCPMessageListenThread = new Thread(tCP.TCPMessageListener);
ThreadsList.Add(TCPMessageListenThread); //将新建的线程加入到线程队列中,以便在窗体结束时关闭所有的线程
TCPMessageListenThread.Start();
InitLANTable();//初始化局域网表格
uDP.OnlineMessageSend(IPAddress.Broadcast, ReplyStatus.NeedReply, OnlineStatus.Online);//上线广播
ControlsDoubleBuffered();
MouseDownDragMove();
buttonSendM.Enabled = false;
textBoxInfoUID.Text = CommonFoundations.HostUID;
textBoxInfoIP.Text = hostInfo.IPv4Address.ToString();
textBoxInfoName.Text = CommonFoundations.HostName;
panelLANBar.BringToFront();
panelLANBarTitle.BringToFront();
panelSideBar.BringToFront();
if (File.Exists(CommonFoundations.QuickSignIn_Path) == true)//快捷登录文件存在,把设置页面的快捷登录勾上
{
buttonEnableQSI.BackColor = CommonFoundations.MainBlue;
}
else
{
buttonEnableQSI.BackColor = CommonFoundations.DarkBlue;
}
}
}
/// <summary>
/// 暂时缓存当前聊天对象的信息,同时在个人信息框显示对面的个人信息
/// </summary>
private void SaveTemp(string uid)
{
foreach (DataRow dataRow in LANtable.Rows)
{
if (dataRow[0].ToString() == uid)
{
CommonFoundations.RemoteUID = uid;
CommonFoundations.RemoteName = dataRow[1].ToString();
CommonFoundations.RemoteIP = dataRow[2].ToString();
formMain.textBoxUID.Text = uid;
formMain.textBoxName.Text = dataRow[1].ToString();
formMain.textBoxIP.Text = dataRow[2].ToString();
break;
}
}
}
/// <summary>
/// 处理本机发送的消息。
/// </summary>
private void HandleMyMessage(string message)
{
//向聊天框中追加文本,并记录文本的起始位置
int countBefore = richTextBoxChat.TextLength;
richTextBoxChat.AppendText("我 " + DateTime.Now.ToLocalTime().ToString() + "\n");
richTextBoxChat.AppendText(message + "\n");
richTextBoxChat.AppendText("\n");
int countAfter = richTextBoxChat.TextLength;
//更改文本的颜色和右对齐
richTextBoxChat.Select(countBefore, countAfter - countBefore);
richTextBoxChat.SelectionAlignment = HorizontalAlignment.Right;
richTextBoxChat.SelectionColor = CommonFoundations.MyTextColor;
richTextBoxChat.Select(0, 0);
ScrollToTheBottom();//滚到底部
///TCP发送聊天信息格式:我方17位UID + 要发送的消息
string carrier = CommonFoundations.HostUID + message;
TCP tCP = new TCP();
tCP.TCPMessageSender(CommonFoundations.RemoteIP, carrier, 50012);//将消息通过TCP发送到对方
richTextBoxChat.SaveFile(CommonFoundations.history_Path_Slash + CommonFoundations.RemoteUID + "/" + "history.uch");//保存聊天文件
}
/// <summary>
/// 处理正在聊天的对面发送的消息。支持跨线程调用。
/// </summary>
public void HandleYouMessage(string message)
{
if (formMain.richTextBoxChat.InvokeRequired == false)//判断和窗口主线程是否是同一线程,是就正常用,否则需要代理
{
//向聊天框中追加文本,并记录文本的起始位置
int countBefore = richTextBoxChat.TextLength;
richTextBoxChat.AppendText(CommonFoundations.RemoteName + " " + DateTime.Now.ToLocalTime().ToString() + "\n");
richTextBoxChat.AppendText(message + "\n");
richTextBoxChat.AppendText("\n");
int countAfter = richTextBoxChat.TextLength;
richTextBoxChat.Select(countBefore, countAfter - countBefore);
richTextBoxChat.SelectionAlignment = HorizontalAlignment.Left;
richTextBoxChat.SelectionColor = CommonFoundations.YourTextColor;
richTextBoxChat.Select(0, 0);
richTextBoxChat.SelectionStart = richTextBoxChat.TextLength; ;
richTextBoxChat.ScrollToCaret();
richTextBoxChat.Select(0, 0);
ScrollToTheBottom();
richTextBoxChat.SaveFile(CommonFoundations.history_Path_Slash + CommonFoundations.RemoteUID + "/" + "history.uch");//保存聊天文件
}
else//跨线程调用窗口控件需要invoke
{
Action<string> dG_HandleYouMessage = new Action<string>(HandleYouMessage);
formMain.richTextBoxChat.Invoke(dG_HandleYouMessage, message);
}
}
/// <summary>
/// 向聊天框追加未读消息。
/// </summary>
/// <param name="date"></param>
/// <param name="message"></param>
private void AddUnreadMessage(string date, string message)
{
//向聊天框中追加文本,并记录追加文本的起始和终止位置
int countBefore = richTextBoxChat.TextLength;
richTextBoxChat.AppendText(CommonFoundations.RemoteName + " " + date + "\n");
richTextBoxChat.AppendText(message + "\n");
richTextBoxChat.AppendText("\n");
int countAfter = richTextBoxChat.TextLength;
//由起始位置选中追加文本,更改对齐和颜色
richTextBoxChat.Select(countBefore, countAfter - countBefore);
richTextBoxChat.SelectionAlignment = HorizontalAlignment.Left;
richTextBoxChat.SelectionColor = CommonFoundations.YourTextColor;
richTextBoxChat.Select(0, 0);
richTextBoxChat.SelectionStart = richTextBoxChat.TextLength;
richTextBoxChat.ScrollToCaret();
richTextBoxChat.Select(0, 0);
ScrollToTheBottom();
richTextBoxChat.SaveFile(CommonFoundations.history_Path_Slash + CommonFoundations.RemoteUID + "/" + "history.uch");//保存聊天文件
}
/// <summary>
/// 把聊天框滚到底部。
/// </summary>
private void ScrollToTheBottom()
{
richTextBoxChat.SelectionStart = richTextBoxChat.TextLength;
richTextBoxChat.ScrollToCaret();
}
/// <summary>
/// 加载聊天历史记录。
/// </summary>
private void LoadHistory()
{
string chatHistoryFilePath = CommonFoundations.history_Path_Slash + CommonFoundations.RemoteUID + "/" + "history.uch";
if (File.Exists(chatHistoryFilePath) == true)//检查有没有历史记录,有就直接加载
{
richTextBoxChat.Clear();//清空高级文本框
richTextBoxChat.LoadFile(chatHistoryFilePath);
ScrollToTheBottom();
}
else
{
richTextBoxChat.Clear();//清空高级文本框
Directory.CreateDirectory(CommonFoundations.history_Path_Slash + CommonFoundations.RemoteUID);
}
}
/// <summary>
/// 读取未读消息。
/// </summary>
/// <param name="message"></param>
public void ReadUnread(string uid)
{
//定义那个人的未读消息文件路径
string xmlFilePath = CommonFoundations.history_Path_Slash + uid + "/unread.xml";
if (File.Exists(xmlFilePath) == true)//文件存在,直接打开,不存在就不管
{
DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlFilePath);//读取xml为表格
int count = dataSet.Tables[0].Rows.Count;//获得未读消息总条数
for (int i = 0; i < count; i++)
{
AddUnreadMessage(dataSet.Tables[0].Rows[i][0].ToString(), dataSet.Tables[0].Rows[i][1].ToString());//逐条添加未读消息到框里
}
File.Delete(xmlFilePath);//卸磨杀驴,删掉未读消息文件
}
}
/// <summary>
/// 把未读消息写入某人的xml中。
/// </summary>
/// <param name="message"></param>
public void WriteUnread(string uid, string message)
{
Directory.CreateDirectory(CommonFoundations.history_Path_Slash + uid);
//定义那个人的未读消息文件路径
string xmlFilePath = CommonFoundations.history_Path_Slash + uid + "/unread.xml";
XmlDocument doc = new XmlDocument();
XmlElement root;
///未读消息结构:
///<history>
/// <subHistory>
/// <time></time>
/// <message></message>
/// </subHistory>
/// <subHistory>
/// <time></time>
/// <message></message>
/// </subHistory>
///</history>
if (File.Exists(xmlFilePath) == true)//文件存在,直接打开
{
doc.Load(xmlFilePath);
root = doc.DocumentElement;//获得文件的根节点
}
else//不存在,新建一个
{
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);//创建第一行
doc.AppendChild(dec);
root = doc.CreateElement("history");//创建根节点
doc.AppendChild(root);
}
XmlElement subHistory = doc.CreateElement("subHistory");//给根节点Books创建子节点
root.AppendChild(subHistory);//将book添加到根节点
//给Book1添加子节点
//时间
XmlElement time = doc.CreateElement("time");
time.InnerText = DateTime.Now.ToLocalTime().ToString();
subHistory.AppendChild(time);
//内容
XmlElement text = doc.CreateElement("message");
text.InnerText = message;
subHistory.AppendChild(text);
//todo:文件路径不存在
doc.Save(xmlFilePath); //保存
SwitchLabelUnreadVisble(uid, true);//提示未读消息
}
/// <summary>
/// 改变未读消息提示是否可见。
/// </summary>
/// <param name="uid"></param>
/// <param name="visible">是否可见?</param>
private void SwitchLabelUnreadVisble(string uid, bool visible)
{
foreach (Panel panel in panels)
{
if (panel.Name == "panel" + uid)//找到需要的panel
{
foreach (Control control in panel.Controls)//找未读消息提示条
{
if (control.Name == "labelUnread")
{
control.Visible = visible;//显示它
break;
}
}
break;
}
}
}
/// <summary>
/// 当正在聊天的对象突然下线时,调用此方法拒绝用户继续输入,以防触发 tcp 异常。
/// </summary>
/// <param name="isForbid">true 为禁用。</param>
private void ForbidInput(bool isForbid)
{
if (panelTips.InvokeRequired == false)
{
if (isForbid == true)
{
pictureBoxTips.BackgroundImage = Properties.Resources.下线;
labelNameIndicator.Text = "";
panelTips.BringToFront();
labelForbid.Visible = true;
buttonSendM.Enabled = false;
//隐藏文件传输和详细信息按钮
formMain.buttonFiles.Visible = false;
formMain.buttonDetail.Visible = false;
//关闭文件传输界面,返回局域网界面
ResetSendFileBarUI(false);
panelLANBar.BringToFront();
panelLANBarTitle.BringToFront();
panelSideBar.BringToFront();
buttonLAN.BackColor = CommonFoundations.MainBlue;
buttonFiles.BackColor = Color.Transparent;
}
else
{
panelTips.SendToBack();
buttonSendM.Enabled = true;
}
}
else
{
Action<bool> dG_ForbidInput = new Action<bool>(ForbidInput);
panelTips.Invoke(dG_ForbidInput, isForbid);
}
}
/// <summary>
/// 获取文件的大小。会根据大小自动添加合适的单位。
/// </summary>
/// <param name="filePath">文件的路径</param>
/// <returns></returns>
private string FileSize(string filePath)
{
double b = 1, kb = 1024 * b, mb = 1024 * kb, gb = 1024 * mb;//这里非常不严谨地用了小b
FileInfo fileInfo = new FileInfo(filePath);
double size = 0;
string size2 = "";
size = fileInfo.Length;
if (size > 1 && size <= 0.9 * kb)//小于900b单位为b
{
size2 = size.ToString("#0.00") + " B";
}
if (size > 0.9 * kb && size <= 0.9 * mb)//小于900kb单位为kb
{
size /= kb;
size2 = size.ToString("#0.00") + " KB";
}
if (size > 0.9 * mb && size <= 0.9 * gb)//小于900mb单位为mb
{
size /= mb;
size2 = size.ToString("#0.00") + " MB";
}
else//大于900mb均为GB
{
size /= gb;
size2 = size.ToString("#0.00") + " GB";
}
return size2;
}
/// <summary>
/// 获取文件的大小。会根据大小自动添加合适的单位。
/// </summary>
/// <param name="fileLength">文件的长度(以字节为单位)</param>
/// <returns></returns>
public string FileSize(long fileLength)
{
double b = 1, kb = 1000 * b, mb = 1000 * kb, gb = 1000 * mb;//这里非常不严谨地用了小b
double size = fileLength;
string size2 = "";
if (size > 1 && size <= 0.9 * kb)//小于900b单位为b
{
size2 = size.ToString("#0.00") + " B";
return size2;
}
if (size > 0.9 * kb && size <= 0.9 * mb)//小于900kb单位为kb
{
size /= kb;
size2 = size.ToString("#0.00") + " KB";
return size2;
}
if (size > 0.9 * mb && size <= 0.9 * gb)//小于900mb单位为mb
{
size /= mb;
size2 = size.ToString("#0.00") + " MB";
return size2;
}
else//大于900mb均为GB
{
size /= gb;
size2 = size.ToString("#0.00") + " GB";
return size2;
}
}
/// <summary>
/// 显示是否接受文件传输请求界面。
/// </summary>
public void ShowFileTransferConfirm(string uid, string fileName, string fileSize)
{
if (labelWaiting.InvokeRequired == false)
{
ClickFileButton();
timerFTTimeout.Enabled = true;
timerFTTimeout.Start();//超时计时器开始计时
string name = "";
//缓存文件信息
CommonFoundations.FileTransferTempData.FileFullName = fileName;
CommonFoundations.FileTransferTempData.FileLengthBytes = long.Parse(fileSize);
for (int i = 0; i < LANtable.Rows.Count; i++)//在局域网在线表寻找 uid 对应的 name 和 ip
{
if (LANtable.Rows[i][0].ToString() == uid)
{
name = LANtable.Rows[i][1].ToString();
CommonFoundations.FileTransferTempData.FRSourceIP = LANtable.Rows[i][2].ToString();//缓存对面IP
break;
}
}
string filesize2 = FileSize(long.Parse(fileSize));
this.BackColor = Color.FromArgb(255, 128, 0);
labelWaiting.Text = name + "想要发送\r\n\r\n" + fileName + "\r\n文件大小:" + filesize2 + "\r\n\r\n你想接受这个文件吗?";
labelWaiting.Visible = true;
panelConfirm.Visible = true;
buttonSelectFile.Visible = false;
labelTarget.Visible = false;
label6.Visible = false;
}
else
{
Action<string, string, string> dG_ShowFileTransferConfirm = new Action<string, string, string>(ShowFileTransferConfirm);
labelWaiting.Invoke(dG_ShowFileTransferConfirm, uid, fileName, fileSize);
}
}
/// <summary>
/// 刷新文件传输界面至初始状态。
/// </summary>
/// <param name="isBringToFront">若为 true ,更新时把界面置顶。</param>
private void ResetSendFileBarUI(bool isBringToFront)
{
if (label6.InvokeRequired == false)
{
progressBar1.Value = 0;
label6.Visible = true;
labelTarget.Visible = true;
buttonSelectFile.Visible = true;
labelWaiting.Visible = false;
panelConfirm.Visible = false;
buttonRefuse.Text = "拒绝(180)";
panelPercent.Visible = false;
panelSameFile.Visible = false;
if (isBringToFront == true)
{
panelFileBar.BringToFront();
panelSideBar.BringToFront();
buttonFiles.BackColor = CommonFoundations.MainBlue;
buttonLAN.BackColor = Color.Transparent;
}
else
{
}
labelTarget.Text = CommonFoundations.RemoteName;
}
else
{
Action<bool> dG_OneParaAndNoReturn = new Action<bool>(ResetSendFileBarUI);
label6.Invoke(dG_OneParaAndNoReturn);
}
}
/// <summary>
/// 提示文件传输的对方占线,导致无法传输。支持跨线程。
/// </summary>
public void BusyRemind()
{
if (labelWaiting.InvokeRequired == false)
{
NotificationSystem notificationSystem = new NotificationSystem();
notificationSystem.PushNotification("错误", "对方正处于另一个文件传输进程,请稍后重试。", NotificationSystem.PresetColors.WarningRed);
CommonFoundations.FileTransferTempData.ResetFTRTempData();
ResetSendFileBarUI(true);
this.BackColor = Color.FromArgb(0, 125, 236);
}
else
{
Action dG_NoParaAndNoReturn = new Action(BusyRemind);
labelWaiting.Invoke(dG_NoParaAndNoReturn);
}
}
/// <summary>
/// 提示文件传输的对方拒主动绝,导致无法传输。支持跨线程。
/// </summary>
public void RefuseRemind()
{
if (labelWaiting.InvokeRequired == false)
{
NotificationSystem notificationSystem = new NotificationSystem();
notificationSystem.PushNotification("注意", "对方拒绝了你的文件传输请求。", NotificationSystem.PresetColors.AttentionYellow);
CommonFoundations.FileTransferTempData.ResetFTRTempData();
ResetSendFileBarUI(false);
this.BackColor = Color.FromArgb(0, 125, 236);
}
else
{
Action dG_NoParaAndNoReturn = new Action(RefuseRemind);
labelWaiting.Invoke(dG_NoParaAndNoReturn);
}
}
private void ClickFileButton()
{
if (CommonFoundations.FileTransferTempData.FlieTransferAcceptLock == false)//文件锁没开,更新ui
{
ResetSendFileBarUI(true);
}
else
{
}
panelFileBar.BringToFront();
panelSideBar.BringToFront();
buttonFiles.BackColor = CommonFoundations.MainBlue;
buttonLAN.BackColor = Color.Transparent;
buttonSetting.BackColor = Color.Transparent;
}
/// <summary>
/// 退出程序。
/// </summary>
private void ExitProgram()
{
if (CommonFoundations.FileTransferTempData.FlieTransferAcceptLock == true)//文件传输时,不允许退出
{
NotificationSystem notificationSystem = new NotificationSystem();
notificationSystem.PushNotification("警告", "在文件传输时无法退出程序。若仍要退出,请取消文件传输任务后再尝试退出程序。", NotificationSystem.PresetColors.WarningRed);
}
else
{
UDP uDP = new UDP();
uDP.OnlineMessageSend(IPAddress.Broadcast, ReplyStatus.NoReplyRequired, OnlineStatus.Offline);
Environment.Exit(0);
}
}
/// <summary>
/// 提示用户文件传输完成,并完成收尾工作。
/// </summary>
private void FTROverProcessor()
{
if (panelLANBar.InvokeRequired == false)
{
timerPercent.Stop();
timerPercent.Enabled = false;
//做清理工作
CommonFoundations.FileTransferTempData.ResetFTRTempData();
ResetSendFileBarUI(false);
if (buttonFiles.Visible == false)
{
panelLANBar.BringToFront();
panelLANBarTitle.BringToFront();
panelSideBar.BringToFront();
buttonFiles.BackColor = CommonFoundations.MainBlue;
buttonLAN.BackColor = Color.Transparent;
}
}
else
{
Action action = new Action(FTROverProcessor);
panelLANBar.Invoke(action);
}
}
/// <summary>
/// 把秒数转换为格式化的字符串。自动加上时间单位。
/// </summary>
/// <param name="sec">总秒数</param>
/// <returns></returns>
private string SecToString(long sec)
{
if (sec <= 60)//小于1分钟
{
return sec.ToString() + " 秒";
}
else//大于1分钟,显示为分钟 + 秒。
{
long min = sec / 60;
long sec2 = sec % 60;
return min.ToString() + " 分钟 " + sec2.ToString() + " 秒";
}
}
/// <summary>
/// 拒绝文件传输。
/// </summary>
private void RefuseFTR()
{
timerFTTimeout.Stop();//超时计时器停止计时
timerFTTimeout.Enabled = false;
FileTransfer.FileTransferAnswerSender(AcceptStatus.RefuseByUser, CommonFoundations.FileTransferTempData.FRSourceIP);
CommonFoundations.FileTransferTempData.ResetFTRTempData();
ResetSendFileBarUI(false);
panelFileBar.SendToBack();
panelLANBar.BringToFront();
panelLANBarTitle.BringToFront();
panelSideBar.BringToFront();
buttonLAN.BackColor = CommonFoundations.MainBlue;
buttonFiles.BackColor = Color.Transparent;
this.BackColor = Color.FromArgb(0, 125, 236);
}
/// <summary>
/// 接受文件传输。
/// </summary>
private void AcceptFTR()
{
timerFTTimeout.Stop();//超时计时器停止计时
timerFTTimeout.Enabled = false;
FileTransfer.FileTransferAnswerSender(AcceptStatus.Accept, CommonFoundations.FileTransferTempData.FRSourceIP);//向对方确认接收文件
panelConfirm.Visible = false;
panelSameFile.Visible = false;
panelPercent.Visible = true;
panelPercent.BringToFront();
labelWaiting.Text = "正在接受文件\r\n\r\n" + CommonFoundations.FileTransferTempData.FileFullName;
timerPercent.Enabled = true;
timerPercent.Start();
backgroundWorkerFileReceiver.RunWorkerAsync();//开启后台线程,等待 TCP 接受文件
}
/// <summary>
/// 使控件订阅鼠标在控件上按下鼠标能拖动窗体的事件。
/// </summary>
#region
private void MouseDownDragMove()
{
this.MouseDown += new MouseEventHandler(Controls_MouseDown);
label9.MouseDown += new MouseEventHandler(Controls_MouseDown);
label10.MouseDown += new MouseEventHandler(Controls_MouseDown);
label11.MouseDown += new MouseEventHandler(Controls_MouseDown);
label12.MouseDown += new MouseEventHandler(Controls_MouseDown);
label13.MouseDown += new MouseEventHandler(Controls_MouseDown);
label8.MouseDown += new MouseEventHandler(Controls_MouseDown);
label14.MouseDown += new MouseEventHandler(Controls_MouseDown);
label15.MouseDown += new MouseEventHandler(Controls_MouseDown);
panelInfo.MouseDown += new MouseEventHandler(Controls_MouseDown);
panelChangeName.MouseDown += new MouseEventHandler(Controls_MouseDown);
panelLANBar.MouseDown += new MouseEventHandler(Controls_MouseDown);
panelFileBar.MouseDown += new MouseEventHandler(Controls_MouseDown);
panelSetting.MouseDown += new MouseEventHandler(Controls_MouseDown);
panelLANBarTitle.MouseDown += new MouseEventHandler(Controls_MouseDown);
pictureBoxEmptyIcon.MouseDown += new MouseEventHandler(Controls_MouseDown);