-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormFriend.cs
397 lines (334 loc) · 13 KB
/
FormFriend.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
/*
* 子线程的问题 没有完
*
* 多线程改变控件时,会发生掉死的现象,待解决。
*
* 增加根据某人ID查找他的好友列表
*
*/
namespace KaiXinAssist
{
public delegate void LoadFriendsDelegate(string text);
public delegate void LoadFriendsOnlineDelegate(string text);
public delegate void ClearFriendDelegate();
public delegate void GetJiPiao(); //获取机票信息
public partial class FormFriend : Form
{
private Thread LoadFriendsThread = null;
private static bool isAllSelect = false; //全选标记,默认全不选
private static bool isAllList = true; //显示全部好友
private static string[] arrReceiveMessage;
KaixinHelper kh;
public FormFriend()
{
InitializeComponent();
LoadFriendsThread = new Thread(new ThreadStart(ByFriendsThreadHandle)); //此线程中指定执行的函数
LoadFriendsThread.IsBackground = true;
}
private void FormFriend_Load(object sender, EventArgs e)
{
Program.formLogin.Hide(); //隐藏登录窗体
kh = new KaixinHelper();
LoadFriendsThread.Start();
//if ((LoadFriendsThread.ThreadState & ThreadState.Running) == ThreadState.Running)
// LoadFriendsThread.Abort();
//LoadFriendsThread = null;
}
public void ByFriendsThreadHandle() //线程调用的函数
{
try
{
string[] arrFriend = kh.GetFriends(KaixinHelper.userid);//获取指定用户好友列表
//string[] arrFriend = kh.GetFriends(1044978);
BeginInvoke(new ClearFriendDelegate(ClearFriends));
for (int i = 0; i < arrFriend.Length; i++)
{
string tempFriendId = arrFriend[i].Substring(0, arrFriend[i].IndexOf('_'));
string tempFriendName = arrFriend[i].Substring(arrFriend[i].IndexOf('_') + 1, arrFriend[i].Length - (arrFriend[i].IndexOf('_') + 1));
if (tempFriendName.Length < 3 && tempFriendName.Length > 1)
{
tempFriendName = tempFriendName.Substring(0, 1) + " " + tempFriendName.Substring(1, 1);
}
// chklstFriend.Items.Add(tempFriendName + " (" + tempFriendId + ")");
BeginInvoke(new LoadFriendsDelegate(LoadFriendsListCotrol), new object[] { tempFriendName + " (" + tempFriendId + ")" });
//Thread.Sleep(50);
}
}
catch
{
}
}
public void ByFriendsOnlineThreadHandle() //线程调用的函数
{
try
{
string[] arrFriend = kh.GetOnlineFriends();
BeginInvoke(new ClearFriendDelegate(ClearFriends));
for (int i = 0; i < arrFriend.Length; i++)
{
string tempFriendId = arrFriend[i].Substring(0, arrFriend[i].IndexOf('_'));
string tempFriendName = arrFriend[i].Substring(arrFriend[i].IndexOf('_') + 1, arrFriend[i].Length - (arrFriend[i].IndexOf('_') + 1));
if (tempFriendName.Length < 3 && tempFriendName.Length > 1)
{
tempFriendName = tempFriendName.Substring(0, 1) + " " + tempFriendName.Substring(1, 1);
}
BeginInvoke(new LoadFriendsDelegate(LoadFriendsListCotrol), new object[] { tempFriendName + " (" + tempFriendId + ")" });
}
}
catch
{
}
}
#region method for Delegate
private void ClearFriends() //被委托调用的函数
{
chklstFriend.Items.Clear();
}
private void LoadFriendsListCotrol(string text) //被委托调用的函数
{
chklstFriend.Items.Add(text);
}
#endregion
private void ClearSelect()
{
txtBoxReceive.Text = "";
lblReceive.Text = "接收者";
btnAllSelect.Text = "全 选";
isAllSelect = false;
for (int i = 0; i < chklstFriend.Items.Count; i++)
{
chklstFriend.SetItemChecked(i, false);
}
}
private void SelectEvent()
{
lblReceive.Text = "接收者 共" + chklstFriend.CheckedItems.Count.ToString() + "人";
txtBoxReceive.Text = "";
txtReceiverID.Text = "";
for (int i = 0; i < chklstFriend.CheckedItems.Count; i++)
{
string tempFriendName = kh.GetMatch(@"(?<text1>[\s\S]*?) \(", chklstFriend.CheckedItems[i].ToString());
tempFriendName = tempFriendName.Replace(" ", "");
txtBoxReceive.Text += tempFriendName + ";";
txtReceiverID.Text += kh.GetMatch(@"\((?<text1>[\d]*?)\)", chklstFriend.CheckedItems[i].ToString())+",";
}
if(txtReceiverID.Text.Length>0)
{
txtReceiverID.Text = txtReceiverID.Text.Substring(0, txtReceiverID.Text.Length - 1);
}
}
/// <summary>
/// 选中好友
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chklstFriend_SelectedIndexChanged(object sender, EventArgs e)
{
SelectEvent();
}
/// <summary>
/// 全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAllSelect_Click(object sender, EventArgs e)
{
isAllSelect = !isAllSelect;
if (isAllSelect)
{
btnAllSelect.Text = "清 除";
}
else
{
btnAllSelect.Text = "全 选";
}
for (int i = 0; i < chklstFriend.Items.Count; i++)
{
chklstFriend.SetItemChecked(i, isAllSelect);
}
SelectEvent();
}
/// <summary>
/// 退出应用程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormFriend_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtContent.Text = "";
}
#region 备用
/*
private void btnSend_Click(object sender,EventArgs e)
{
bool msgReturn = false;
string[] arrUserId = txtReceiverID.Text.Split(',');
if (chkComment.Checked) //如果留言复选框被选中,则仅当作留言发送。
{
for (int i = 0; i < arrUserId.Length; i++)
{
string tempFriendId = arrUserId[i];
MessageBox.Show(tempFriendId + "\n" + txtContent.Text);
msgReturn = kh.PostComment(tempFriendId, txtContent.Text);
if (msgReturn == false)
{
MessageBox.Show(tempFriendId.ToString()+": send failed ");
break;
}
}
}
else
{
msgReturn = kh.PostMessage(txtReceiverID.Text, txtContent.Text);
if (!msgReturn)
{
MessageBox.Show("发送失败,请重新发送!");
}
}
if (msgReturn)
{
MessageBox.Show("OK,发送完成!");
}
else
{
MessageBox.Show("OK,发送失败!");
}
}
*/
#endregion
private void btnSend_Click(object sender, EventArgs e)
{
bool msgReturn = false;
if (chkComment.Checked) //如果留言复选框被选中,则仅当作留言发送。
{
for (int i = 0; i < chklstFriend.CheckedItems.Count; i++)
{
string tempFriendId = kh.GetMatch(@"\((?<text1>[\d]*?)\)", chklstFriend.CheckedItems[i].ToString());
//MessageBox.Show(tempFriendId + "\n" + txtContent.Text);
msgReturn = kh.PostComment(tempFriendId, txtContent.Text);
if (msgReturn == false)
{
break;
}
}
}
else
{
// if (chkGroupSend.Checked) //是否群发
{
string strReceiveIds = string.Empty;//群发时接收的ID组
for (int i = 0; i < chklstFriend.CheckedItems.Count; i++)
{
string tempFriendId = kh.GetMatch(@"\((?<text1>[\d]*?)\)", chklstFriend.CheckedItems[i].ToString());
if (i < chklstFriend.CheckedItems.Count - 1)
{
strReceiveIds += tempFriendId + ",";
}
else
{
strReceiveIds += tempFriendId;
}
}
msgReturn = kh.PostMessage(strReceiveIds, txtContent.Text);
if (!msgReturn)
{
MessageBox.Show("发送给失败,请重新发送!");
}
}
//else
//{
// for (int i = 0; i < chklstFriend.CheckedItems.Count; i++)
// {
// string tempFriendName = kh.GetMatch(@"(?<text1>[\s\S]*?) \(", chklstFriend.CheckedItems[i].ToString());
// tempFriendName = tempFriendName.Replace(" ", "");
// string tempFriendId = kh.GetMatch(@"\((?<text1>[\d]*?)\)", chklstFriend.CheckedItems[i].ToString());
// //txtContent.Text += tempFriendId + ";";
// msgReturn = kh.PostMessage(tempFriendId.ToString(), txtContent.Text);
// if (!msgReturn)
// {
// MessageBox.Show("发送给" + tempFriendName + "时失败,请重新发送!");
// break;
// }
// }
//}
}
if (msgReturn)
{
MessageBox.Show("OK,发送完成!");
}
else
{
MessageBox.Show("OK,发送失败!");
}
}
private void btnFriendList_Click(object sender, EventArgs e)
{
}
private void chkDispOnline_CheckedChanged(object sender, EventArgs e)
{
ClearSelect();
if (isAllList)
{
LoadFriendsThread = new Thread(new ThreadStart(ByFriendsOnlineThreadHandle)); //此线程中指定执行的函数
LoadFriendsThread.IsBackground = true;
LoadFriendsThread.Start();
chkDispOnline.Checked = true;
isAllList = !isAllList;
}
else
{
LoadFriendsThread = new Thread(new ThreadStart(ByFriendsThreadHandle)); //此线程中指定执行的函数
LoadFriendsThread.IsBackground = true;
LoadFriendsThread.Start();
chkDispOnline.Checked = false;
isAllList = !isAllList;
}
}
private void btnRefresh_Click(object sender, EventArgs e)
{
try
{
arrReceiveMessage = kh.GetMessage();
chklstReceiveList.Items.Clear();
for (int i = 0; i < arrReceiveMessage.Length; i++)
{
string[] tempArrOneMessage = arrReceiveMessage[i].Split('_');
string m_Id = tempArrOneMessage[0];
string m_Name = tempArrOneMessage[1];
string m_Content = tempArrOneMessage[2];
chklstReceiveList.Items.Add(m_Name + " (" + m_Id + ")");
}
}
catch
{
}
}
private void chklstReceiveList_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string[] tempArrOneMessage = arrReceiveMessage[chklstReceiveList.SelectedIndex].Split('_');
string m_Content = tempArrOneMessage[2];
txtReceiveContent.Text = m_Content;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}