Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/kakuretter/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 58 - (show annotations) (download)
Sun Aug 4 08:09:58 2019 UTC (4 years, 8 months ago) by momen2009
File size: 16546 byte(s)
[kakuretter]投稿できないようになってしまったため、修正。
1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Windows.Forms;
5 using Twitter;
6 using System.Text.RegularExpressions;
7 using Newtonsoft.Json;
8 using System.Collections;
9
10 namespace Kakuretter
11 {
12
13 public partial class Form1 : Form
14 {
15
16 public HotKey hotKey;
17 TwitterUser selectedTwitterUser { set; get; }
18
19 void hotKey_HotKeyPush(object sender, EventArgs e)
20 {
21
22 if (Visible)
23 {
24 Hide();
25 Close();
26
27 // トレイリストのアイコンを表示する
28 notifyIcon1.Icon = new Icon(Icon, 40, 40);
29 notifyIcon1.Visible = true;
30 隠れるHToolStripMenuItem.Text = "表示する(&H)";
31 }
32 else
33 {
34 // フォームを表示する
35 Visible = true;
36 WindowState = FormWindowState.Normal;
37 Activate();
38 隠れるHToolStripMenuItem.Text = "隠れる(&H)";
39 }
40
41
42 }
43
44 public Form1()
45 {
46 InitializeComponent();
47
48 var settings = Properties.Settings.Default;
49
50 if ((bool)settings["HotKeyHidden"] == true)
51 {
52 HotKeyHiddenActivate();
53 }
54
55 accountListUpdate();
56
57 隠れるHToolStripMenuItem.Text = "表示する(&H)";
58 Location = settings.MyLocation;
59 Opacity = settings.Opacity;
60 Form1.Form1Instance = this;
61 }
62
63 public void accountListUpdate()
64 {
65 var settings = Properties.Settings.Default;
66 ArrayList data;
67 if (string.IsNullOrEmpty(settings.TwitterUserList))
68 {
69 data = new ArrayList();
70 }
71 else {
72 data = JsonConvert.DeserializeObject<ArrayList>(settings.TwitterUserList);
73 }
74 アカウント変更CToolStripMenuItem.DropDownItems.Clear();
75
76 if (0 < data.Count)
77 {
78 foreach (var i in data)
79 {
80 TwitterUser tu = JsonConvert.DeserializeObject<TwitterUser>(i.ToString());
81
82 ToolStripMenuItem item = new ToolStripMenuItem("Menu");
83 item.Text = tu.ScreenName;
84 item.Click += new EventHandler(accountChange);
85 if (tu.ScreenName == settings.SelectedTwitterUser)
86 {
87 item.Checked = true;
88 selectedTwitterUser = tu;
89 }
90
91 アカウント変更CToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { item });
92 }
93 アカウント変更CToolStripMenuItem.Enabled = true;
94 認証するAToolStripMenuItem.Enabled = true;
95 }
96 else
97 {
98 アカウント変更CToolStripMenuItem.DropDownItems.Clear();
99 アカウント変更CToolStripMenuItem.Enabled = false;
100 認証するAToolStripMenuItem.Enabled = false;
101 }
102 }
103
104 private void accountChange(object sender, EventArgs e)
105 {
106 var settings = Properties.Settings.Default;
107 ArrayList data = JsonConvert.DeserializeObject<ArrayList>(settings.TwitterUserList);
108
109 foreach (ToolStripMenuItem item2 in アカウント変更CToolStripMenuItem.DropDownItems)
110 {
111 item2.Checked = false;
112 }
113
114 ToolStripMenuItem item = (ToolStripMenuItem)sender;
115 foreach (var i in data)
116 {
117 TwitterUser tu = JsonConvert.DeserializeObject<TwitterUser>(i.ToString());
118 if (tu.ScreenName == item.Text)
119 {
120 selectedTwitterUser = tu;
121 item.Checked = true;
122 settings.SelectedTwitterUser = tu.ScreenName;
123 settings.Save();
124 }
125 }
126 }
127
128 public void HotKeyHiddenActivate()
129 {
130 hotKey = new HotKey(MOD_KEY.ALT, Keys.H);
131 hotKey.HotKeyPush += new EventHandler(hotKey_HotKeyPush);
132 }
133
134 //Form1オブジェクトを保持するためのフィールド
135 private static Form1 _form1Instance;
136
137 //Form1オブジェクトを取得、設定するためのプロパティ
138 public static Form1 Form1Instance
139 {
140 get
141 {
142 return _form1Instance;
143 }
144 set
145 {
146 _form1Instance = value;
147 }
148 }
149
150 private void textBox3_KeyDown_1(object sender, KeyEventArgs e)
151 {
152
153 if (e.KeyCode == System.Windows.Forms.Keys.Enter)
154 {
155
156 // 設定ファイルから読み込む
157 var settings = Properties.Settings.Default;
158 if (string.IsNullOrEmpty(settings.TwitterUserList) || settings.TwitterUserList == "[]")
159 {
160 Form2 form2 = new Form2();
161 form2.Show();
162 return;
163 }
164 tweet();
165
166 }
167 }
168
169 private void button1_Click(object sender, EventArgs e)
170 {
171
172 // 設定ファイルから読み込む
173 var settings = Properties.Settings.Default;
174 if (settings.UserId == "")
175 {
176 Form2 form2 = new Form2();
177 form2.Show();
178 return;
179 }
180 tweet();
181
182 }
183
184 private void tweet()
185 {
186 if (selectedTwitterUser == null)
187 {
188 MessageBox.Show("アカウントを選択してください");
189 return;
190 }
191
192 if (int.Parse(label2.Text) < 0) return;
193 String text = textBox3.Text;
194 if (text == "") return;
195
196 Dictionary<string, string> parameters = new Dictionary<string, string>();
197
198 if (textBox1.Text != "")
199 {
200 if (label1.Text == "+")
201 {
202 text += " " + textBox1.Text;
203 }
204 else
205 {
206 text += " #" + textBox1.Text;
207 }
208 }
209 // 設定ファイルから読み込む
210 var settings = Properties.Settings.Default;
211 Auth auth = new Auth(settings.ConsumerKey, settings.ConsumerSecret,
212 selectedTwitterUser.AccessToken, selectedTwitterUser.AccessTokenSecret,
213 selectedTwitterUser.UserId, selectedTwitterUser.ScreenName);
214 parameters.Add("status", auth.UrlEncode(text));
215
216 try
217 {
218 auth.Post("https://api.twitter.com/1.1/statuses/update.json", parameters);
219 textBox3.Text = "";
220 }
221 catch (Exception)
222 {
223 notifyIcon1.BalloonTipTitle = "エラー";
224 notifyIcon1.BalloonTipText = "ツイートに失敗しました。";
225 notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
226 notifyIcon1.ShowBalloonTip(10000);
227 }
228
229 }
230
231 private void 認証ToolStripMenuItem_Click(object sender, EventArgs e)
232 {
233
234 }
235
236 private void textBox3_TextChanged(object sender, EventArgs e)
237 {
238 tweetLengthChange();
239 }
240
241 private void 隠れるToolStripMenuItem_Click(object sender, EventArgs e)
242 {
243
244 }
245
246 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
247 {
248 if (e.CloseReason != CloseReason.ApplicationExitCall)
249 {
250 e.Cancel = true;
251 Hide();
252 //Close();
253
254 // トレイリストのアイコンを表示する
255 //notifyIcon1.Icon = new Icon(Icon, 40, 40);
256 notifyIcon1.Visible = true;
257 隠れるHToolStripMenuItem.Text = "表示する(&H)";
258 }
259 }
260
261 private void notifyIcon1_DoubleClick(object sender, EventArgs e)
262 {
263
264 }
265
266 private void 終了ToolStripMenuItem1_Click(object sender, EventArgs e)
267 {
268 notifyIcon1.Visible = false;
269
270 var settings = Properties.Settings.Default;
271
272 if (settings.HotKeyHidden == true)
273 {
274 hotKey.Dispose();
275 }
276 Application.Exit();
277 }
278
279 private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
280 {
281 if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
282 {
283 if (Visible)
284 {
285 Hide();
286 Close();
287
288 // トレイリストのアイコンを表示する
289 notifyIcon1.Icon = new Icon(Icon, 40, 40);
290 notifyIcon1.Visible = true;
291 隠れるHToolStripMenuItem.Text = "表示する(&H)";
292 }
293 else
294 {
295 // フォームを表示する
296 Visible = true;
297 WindowState = FormWindowState.Normal;
298 Activate();
299 隠れるHToolStripMenuItem.Text = "隠れる(&H)";
300 }
301 }
302 }
303
304 private void 設定ToolStripMenuItem_Click(object sender, EventArgs e)
305 {
306
307 }
308
309 private void Form1_Load(object sender, EventArgs e)
310 {
311
312 }
313
314 private void label1_Click(object sender, EventArgs e)
315 {
316 if (label1.Text == "#")
317 {
318 label1.Text = "+";
319 }
320 else
321 {
322 label1.Text = "#";
323 }
324 tweetLengthChange();
325 }
326
327 private void 設定OToolStripMenuItem_Click(object sender, EventArgs e)
328 {
329 Form3 form3 = new Form3();
330 form3.Show();
331 }
332
333 private void textBox1_TextChanged(object sender, EventArgs e)
334 {
335 tweetLengthChange();
336 }
337
338 private void tweetLengthChange()
339 {
340 int tl = 140;
341 Regex regex = new Regex(@"(?:^|[\s ]+)((?:https?|ftp):\/\/[^\s ]+)");
342 Match m;
343 if (0 < textBox1.TextLength)
344 {
345 if (label1.Text == "url")
346 {
347 tl -= textBox1.TextLength + 1;
348 }
349 else
350 {
351 tl -= textBox1.TextLength + 2;
352 }
353 m = regex.Match(textBox1.Text);
354 if (m.Success)
355 {
356 while (m.Success)
357 {
358 if (22 < m.Length)
359 {
360 tl += m.Length;
361 tl -= 22;
362 }
363 m = m.NextMatch();
364 }
365 }
366 }
367
368 m = regex.Match(textBox3.Text);
369
370 tl -= textBox3.TextLength;
371
372 if (m.Success)
373 {
374 while (m.Success)
375 {
376 if (22 < m.Length)
377 {
378 tl += m.Length;
379 tl -= 22;
380 }
381 m = m.NextMatch();
382 }
383 }
384
385 if (tl < 0)
386 {
387 label2.ForeColor = System.Drawing.Color.Red;
388 }
389 else
390 {
391 label2.ForeColor = SystemColors.ControlText;
392 }
393
394 label2.Text = tl.ToString();
395 }
396
397 private void Form1_KeyDown(object sender, KeyEventArgs e)
398 {
399
400 if (e.KeyCode == Keys.H && e.Alt == true)
401 {
402 e.SuppressKeyPress = true;
403 if (Visible)
404 {
405 Hide();
406 Close();
407
408 // トレイリストのアイコンを表示する
409 notifyIcon1.Icon = new Icon(Icon, 40, 40);
410 notifyIcon1.Visible = true;
411 隠れるHToolStripMenuItem.Text = "表示する(&H)";
412 }
413 else
414 {
415 // フォームを表示する
416 Visible = true;
417 WindowState = FormWindowState.Normal;
418 Activate();
419 隠れるHToolStripMenuItem.Text = "隠れる(&H)";
420 }
421 }
422
423 if (e.KeyCode == Keys.A && e.Alt == true)
424 {
425 e.SuppressKeyPress = true;
426 Form2 form2 = new Form2();
427 form2.Show();
428 }
429
430 if (e.KeyCode == Keys.O && e.Alt == true)
431 {
432 e.SuppressKeyPress = true;
433 Form3 form3 = new Form3();
434 form3.Show();
435 }
436 }
437
438 private void 認証するAToolStripMenuItem_Click(object sender, EventArgs e)
439 {
440 var settings = Properties.Settings.Default;
441 ArrayList data = JsonConvert.DeserializeObject<ArrayList>(settings.TwitterUserList);
442 ArrayList newdata = new ArrayList();
443 if (selectedTwitterUser == null)
444 {
445 MessageBox.Show("アカウントを選択してください");
446 return;
447 }
448 foreach (var i in data)
449 {
450 TwitterUser tu = JsonConvert.DeserializeObject<TwitterUser>(i.ToString());
451 if (tu.ScreenName != selectedTwitterUser.ScreenName)
452 {
453 newdata.Add(tu);
454 }
455 }
456 settings.TwitterUserList = JsonConvert.SerializeObject(newdata);
457 selectedTwitterUser = null;
458 settings.SelectedTwitterUser = null;
459 settings.Save();
460
461 accountListUpdate();
462 }
463
464 private void 隠れるHToolStripMenuItem_Click(object sender, EventArgs e)
465 {
466 if (Visible)
467 {
468 Hide();
469 Close();
470
471 // トレイリストのアイコンを表示する
472 notifyIcon1.Icon = new Icon(Icon, 40, 40);
473 notifyIcon1.Visible = true;
474 隠れるHToolStripMenuItem.Text = "表示する(&H)";
475 }
476 else
477 {
478 // フォームを表示する
479 Visible = true;
480 WindowState = FormWindowState.Normal;
481 Activate();
482 隠れるHToolStripMenuItem.Text = "隠れる(&H)";
483 }
484 }
485
486 private void Form1_KeyPress(object sender, KeyPressEventArgs e)
487 {
488
489 }
490
491 private void Form1_LocationChanged(object sender, EventArgs e)
492 {
493 var settings = Properties.Settings.Default;
494 settings.MyLocation = Location;
495 settings.Save();
496 }
497
498 private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
499 {
500 //EnterやEscapeキーでビープ音が鳴らないようにする
501 if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Escape)
502 {
503 e.Handled = true;
504 }
505 }
506
507 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
508 {
509 //EnterやEscapeキーでビープ音が鳴らないようにする
510 if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Escape)
511 {
512 e.Handled = true;
513 }
514 }
515
516 private void アカウント追加AToolStripMenuItem_Click(object sender, EventArgs e)
517 {
518 Form2 form2 = new Form2();
519 form2.Show();
520 }
521 }
522 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26