-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormMain.cs
823 lines (758 loc) · 30.3 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Configuration;
using System.Security.Policy;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Web;
using CefSharp;
using CefSharp.WinForms;
using System.Runtime.InteropServices;
using System.IO;
namespace RZZReader
{
public partial class FormMain : Form
{
private static string salt = "pleaseinputyourownsaltstringhere";
private static string cfgFN = ".rzz.conf";
private static string cfgDataPath = string.Empty;
private static string mainTitle = string.Empty;
private static ExeConfigurationFileMap cfgFile = new ExeConfigurationFileMap();
private FormLoading formLoading = null;
public FormMain()
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.CenterScreen;
InitializeComponent();
mainTitle = this.Text;
cfgFile.ExeConfigFilename = String.Format("{0}\\{1}",
System.Windows.Forms.Application.StartupPath, cfgFN);
cfgDataPath = cfgFile.ExeConfigFilename + "ig";
CefSettings settings = new CefSettings();
settings.Locale = "zh-CN";
settings.AcceptLanguageList = "zh-CN";
Cef.Initialize(settings);
csWebBrowser.LifeSpanHandler = new LifeSpanHandler();
}
protected SyndicationFeed loadRSS(string rssURI)
{
try
{
SyndicationFeed sf = SyndicationFeed.Load(XmlReader.Create(rssURI));
sf.BaseUri = new Uri(rssURI);
return sf;
} catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return null;
}
}
protected async Task<SyndicationFeed> asyncLoadRSS(string rssURI)
{
SyndicationFeed sf = await Task.Run(() => loadRSS(rssURI));
return sf;
}
protected void listRSS(SyndicationFeed sf, string folder = "default",
bool showTitles = true, TreeNode selectedNode = null, String originalUrl = null)
{
//if (sf == null) return;
/*
this.ShowInTaskbar = true;
this.notifyIcon.Visible = false;
*/
if (folder == null) folder = "default";
TreeNode folderNode;
if (treeViewRzz.Nodes.Count > 0)
{
int i = 0;
for (i = 0; i < treeViewRzz.Nodes.Count; i++)
{
if (treeViewRzz.Nodes[i].Text == folder)
{
break;
}
}
if (i == treeViewRzz.Nodes.Count)
{
folderNode = treeViewRzz.Nodes.Add(folder);
} else
{
folderNode = treeViewRzz.Nodes[i];
}
} else
{
folderNode = treeViewRzz.Nodes.Add(folder);
}
if (selectedNode != null)
{
selectedNode.Text = (sf == null ? originalUrl : sf.Title.Text);
selectedNode.Tag = sf;
} else
{
/*
* insert rssURL and its name&content&everything into the treeview
*/
TreeNode newNode = new TreeNode(sf == null ? originalUrl : sf.Title.Text);
newNode.Tag = sf;
folderNode.Nodes.Add(newNode);
}
if (showTitles) listTitles(sf);
//else listViewRzz.Items.Clear();
/*
this.ShowInTaskbar = false;
this.notifyIcon.Visible = true;
*/
}
protected void listTitles(SyndicationFeed sf, String originalUrl = null)
{
/*
* insert contents including "title, link, date, summary and content" into listview
*/
listViewRzz.Items.Clear();
listViewRzz.Columns[0].Text = (sf == null ? originalUrl : sf.Title.Text);
//listViewRzz.Sorting = SortOrder.Ascending;
if (sf != null)
{
foreach (SyndicationItem it in sf.Items)
{
ListViewItem item = new ListViewItem(it.Title.Text);
item.Tag = it;
listViewRzz.Items.Add(item);
//Application.DoEvents();
}
}
}
private void hideToNotifyIcon()
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.notifyIcon.Visible = true;
this.Hide();
}
private void try2ShowRZZ()
{
notifyIcon.Visible = false;
string cryptedPwd = getConfigValue("pwd");
if (string.IsNullOrEmpty(cryptedPwd))
{
MessageBox.Show("It's your very 1st PIN setting, please remember it, FOR SURE.", "PIN");
}
FormInput formPwd = new FormInput();
formPwd.setPwdMode();
if (formPwd.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(cryptedPwd))
{
setConfigValue("pwd", MD5Hash(formPwd.GetPwd() + salt));
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(0, "Info", "PIN set, please keep going.", ToolTipIcon.Info);
}
else
{
if (cryptedPwd == MD5Hash(formPwd.GetPwd() + salt))
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Show();
}
else
{
notifyIcon.ShowBalloonTip(0, "Error", "Wrong PIN, please try again.", ToolTipIcon.Error);
}
}
}
else
{
notifyIcon.Visible = true;
}
}
string syndicationItemToString(SyndicationItem it)
{
string text = "";
text += "<h1>" + it.Title.Text + "</h1>";
text += it.PublishDate.ToString("yyyy-MM-dd HH:mm:ss") + "<br><hr>";
if (it.Links.Count > 0)
text += "<a href=\"" + it.Links[0].Uri.ToString() + "\">" + it.Links[0].Uri.ToString() + "</a><hr>";
if (it.Summary != null)
text += it.Summary.Text + "<hr>";
if (it.Content != null)
text += ((TextSyndicationContent)it.Content).Text;
text = "<!DOCTYPE html><html lang=\"zh-CN\"><head><meta charset=\"UTF-8\"></head><body>"
+ text
+ "</body></html>";
return text;
}
private string MD5Hash(string text)
{
MD5 md5 = new MD5CryptoServiceProvider();
//compute hash from the bytes of text
md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));
//get hash result after compute it
byte[] result = md5.Hash;
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
//change it into 2 hexadecimal digits
//for each byte
strBuilder.Append(result[i].ToString("x2"));
}
return strBuilder.ToString();
}
List<string> getImageLinks(string htmlContent)
{
List<string> imageLinks = new List<string>();
string pattern = @"<img.*?src=""(.*?)"".*?>";
MatchCollection matches = Regex.Matches(htmlContent, pattern);
foreach (Match match in matches)
{
string imageUrl = match.Groups[1].Value;
if (!string.IsNullOrEmpty(imageUrl))
{
imageLinks.Add(imageUrl);
}
}
return imageLinks;
}
public bool setConfigValue(String key, String value)
{
try
{
if (File.Exists(cfgDataPath))
{
FileEncryption.DecryptFile(cfgDataPath, cfgFile.ExeConfigFilename);
}
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
cfgFile, ConfigurationUserLevel.None);
if (config != null)
{
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
if (appSettings.Settings.AllKeys.Contains(key))
{
appSettings.Settings[key].Value = value;
}
else
{
appSettings.Settings.Add(key, value);
}
config.Save();
FileEncryption.EncryptFile(cfgFile.ExeConfigFilename, cfgDataPath);
File.Delete(cfgFile.ExeConfigFilename);
return true;
}
return false;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
public String getConfigValue(String key)
{
try
{
if (File.Exists(cfgDataPath))
{
FileEncryption.DecryptFile(cfgDataPath, cfgFile.ExeConfigFilename);
}
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
cfgFile, ConfigurationUserLevel.None);
File.Delete(cfgFile.ExeConfigFilename);
if (config != null)
{
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
if (appSettings.Settings.AllKeys.Contains(key))
{
return appSettings.Settings[key].Value;
}
else return null;
}
else return null;
} catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return null;
}
}
private async void toolStripButtonAdd_Click(object sender, EventArgs e)
{
FormInput formAdd = new FormInput();
formAdd.StartPosition = FormStartPosition.CenterParent;
if (formAdd.ShowDialog() == DialogResult.OK)
{
string url = formAdd.GetUrl();
if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
this.Text = mainTitle + " (...Try to add source '" + url + "'...)";
SyndicationFeed sf = await asyncLoadRSS(url);
this.Text = mainTitle;
notifyIcon.Visible = true;
if (sf != null)
{
listRSS(sf);
} else
{
notifyIcon.ShowBalloonTip(0, "Error",
"Something went wrong, maybe the source just entered is not available.",
ToolTipIcon.Error);
notifyIcon.Visible = true;
return;
}
/*
* save rssURI into config file
*/
string urls = getConfigValue("urls");
if (!String.IsNullOrEmpty(urls))
{
var arrUrls = urls.Split(',');
if (arrUrls.Contains(url))
{
notifyIcon.ShowBalloonTip(0, "Warning",
"Duplicated resource, please try others.",
ToolTipIcon.Warning);
notifyIcon.Visible = false;
return;
}
setConfigValue("urls", urls + "," + url);
}
else
{
setConfigValue("urls", url);
}
notifyIcon.Visible = false;
}
else
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(0, "Error", "Not a valid URL.", ToolTipIcon.Error);
notifyIcon.Visible = false;
}
}
formAdd.Close();
}
private void listViewRzz_Click(object sender, EventArgs e)
{
if (listViewRzz.SelectedItems.Count > 0)
{
ListViewItem item = listViewRzz.SelectedItems[0];
SyndicationItem it = item.Tag as SyndicationItem;
try
{
csWebBrowser.LoadHtml(syndicationItemToString(it), true);
} catch (Exception ex)
{
Console.WriteLine(ex.ToString());
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(0, "Error", ex.ToString(), ToolTipIcon.Error);
notifyIcon.Visible = false;
}
}
else return;
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
hideToNotifyIcon();
e.Cancel = true;
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
private void toolStripMenuItemExit_Click(object sender, EventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
Cef.Shutdown();
this.Dispose();
this.Close();
}
private void toolStripButtonExit_Click(object sender, EventArgs e)
{
toolStripMenuItemExit_Click(sender, e);
}
private void treeViewRzz_Click(object sender, EventArgs e)
{
MouseEventArgs me = (MouseEventArgs)e;
if (me.Button == MouseButtons.Left)
{
// just do nothing
} else if (me.Button == MouseButtons.Right)
{
/*
* pop up the menu let the item could be deleted or remaned or sth. like that.
*/
contextMenuStripTree.Show();
}
}
private void treeViewRzz_AfterSelect(object sender, TreeViewEventArgs e)
{
SyndicationFeed sf = (SyndicationFeed)e.Node.Tag;
if (sf != null)
{
listTitles(sf);
}
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
SyndicationFeed sf = (SyndicationFeed)treeViewRzz.SelectedNode.Tag;
DialogResult dr =
MessageBox.Show(
"Are you sure to delete the selected one below?\r\n************\r\n"
+ sf.Title.Text + "\r\n"
+ sf.BaseUri.OriginalString + "\r\n"
+ "************",
"Delete",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (dr == DialogResult.OK)
{
/*
* delete the selected RSS from tree view,
* and renew and sync the "settings"
* */
treeViewRzz.Nodes.Remove(treeViewRzz.SelectedNode);
string urls = getConfigValue("urls");
if (!String.IsNullOrEmpty(urls))
{
var arrUrl = urls.Split(',');
if (arrUrl.Contains(sf.BaseUri.OriginalString))
{
arrUrl = arrUrl.Except(new string[] { sf.BaseUri.OriginalString }).ToArray();
setConfigValue("urls", string.Join(",", arrUrl));
}
else
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(0, "Error", "No can do!", ToolTipIcon.Error);
notifyIcon.Visible = false;
}
} else
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(0, "Error", "Failed to process the config file.", ToolTipIcon.Error);
notifyIcon.Visible = false;
}
}
}
private async void editSourceToolStripMenuItem_Click(object sender, EventArgs e)
{
TreeNode selectedNode = treeViewRzz.SelectedNode;
SyndicationFeed sf = (SyndicationFeed)selectedNode.Tag;
string oldUrl = (sf == null ? selectedNode.Text : sf.BaseUri.OriginalString);
FormInput formIpt = new FormInput();
formIpt.Text = "Edit the source";
formIpt.StartPosition = FormStartPosition.CenterParent;
formIpt.SetUrl(oldUrl);
DialogResult dr = formIpt.ShowDialog();
string url = formIpt.GetUrl();
if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
if (dr == DialogResult.OK)
{
//MessageBox.Show("should do the editing");
//use the new url to load the RSS
this.Text = mainTitle + " (...Try to load the changed source '" + url + "'...)";
sf = await asyncLoadRSS(url);
this.Text = mainTitle;
notifyIcon.Visible = true;
//list the RSS into the tree and list view, but not content browser
if (sf != null)
{
if (treeViewRzz.SelectedNode == selectedNode)
listRSS(sf, null, true, selectedNode);
else
listRSS(sf, null, false, selectedNode);
}
else
{
notifyIcon.ShowBalloonTip(0, "Error",
"Something went wrong, maybe the source just entered is not available.",
ToolTipIcon.Error);
notifyIcon.Visible = true;
return;
}
//save it to the config file
string urls = getConfigValue("urls");
if (!String.IsNullOrEmpty(urls))
{
List<string> list = urls.Split(',').ToList();
int idx = list.IndexOf(oldUrl);
list.RemoveAt(idx);
urls = String.Join(",", list.ToArray());
setConfigValue("urls", urls + "," + url);
}
else
{
setConfigValue("urls", url);
}
}
} else
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(0, "Error", "Not a valid URL.", ToolTipIcon.Error);
notifyIcon.Visible = false;
}
formIpt.Close();
formIpt.Dispose();
}
private async void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
TreeNode selectedNode = treeViewRzz.SelectedNode;
SyndicationFeed sf = (SyndicationFeed)selectedNode.Tag;
string oldUrl = (sf == null ? selectedNode.Text : sf.BaseUri.OriginalString);
this.Text = mainTitle + " (...Refreshing...)";
sf = await asyncLoadRSS(oldUrl);
this.Text = mainTitle;
notifyIcon.Visible = true;
//list the RSS into the tree and list view, but not content browser
if (sf != null)
{
if (treeViewRzz.SelectedNode == selectedNode)
listRSS(sf, null, true, selectedNode);
else
listRSS(sf, null, false, selectedNode);
}
else
{
notifyIcon.ShowBalloonTip(
0, "Error",
"Refreshing failed, maybe the source is temply not available.",
ToolTipIcon.Error
);
notifyIcon.Visible = true;
return;
}
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
try2ShowRZZ();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
try2ShowRZZ();
}
private async void toolStripButtonCollectImgLinks_Click(object sender, EventArgs e)
{
object obj = await csWebBrowser.GetSourceAsync();
string htmlContent = obj.ToString();
List<string> links = getImageLinks(htmlContent);
if (links.Count > 0)
{
string ss = "";
foreach (string link in links)
{
ss += link + "\r\n";
}
toolStripTextBoxImgs.Text = ss;
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(0, "Info",
String.Format("{0} image link(s) collected.", links.Count()),
ToolTipIcon.Info);
notifyIcon.Visible = false;
} else
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(0, "Tips", "Seems that there are no images.", ToolTipIcon.Info);
notifyIcon.Visible = false;
}
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.R) //Ctrl + R
{
try
{
hideToNotifyIcon();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
private void FormMain_Load(object sender, EventArgs e)
{
listViewRzz.View = View.Details;
notifyIcon.Visible = false;
string urls = getConfigValue("urls");
if (!String.IsNullOrEmpty(urls))
{
var arrUrl = urls.Split(',');
formLoading = new FormLoading();
formLoading.Tag = notifyIcon;
formLoading.setBar(arrUrl.Length);
formLoading.setProgress(0);
formLoading.Show();
bgWorkerFeeds.RunWorkerAsync(arrUrl);
} else
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(0, "Tips", "No RSS subscribed, please add one.", ToolTipIcon.Info);
}
}
class RZZs
{
public int cur = 0;
public string[] urls = null;
public List<SyndicationFeed> feeds = new List<SyndicationFeed>();
public object tag = null;
}
private void bgWorkerFeeds_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
RZZs rZZs = new RZZs();
var arrUrl = (string[])e.Argument;
rZZs.urls = arrUrl;
foreach (var url in arrUrl)
{
SyndicationFeed sf = loadRSS(url);
rZZs.feeds.Add(sf);
rZZs.cur++;
//rZZs.tag = (sf == null) ? (object)url : null;
rZZs.tag = (object)url;
bw.ReportProgress(rZZs.cur, rZZs);
}
}
private void bgWorkerFeeds_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
RZZs rZZs = e.UserState as RZZs;
SyndicationFeed sf = rZZs.feeds[rZZs.cur - 1];
if (sf != null)
{
formLoading.setProgress(rZZs.cur);
if (rZZs.cur != rZZs.urls.Length)
{
listRSS(sf, null, false);
}
else
{
listRSS(sf);
formLoading.Close();
formLoading.Dispose();
}
}
else
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(
0, "Error",
String.Format(
"Something's wrong, maybe the source '{0}' is not available.",
//(sf == null ? (string)rZZs.tag : sf.BaseUri.OriginalString)),
(string)rZZs.tag
),
ToolTipIcon.Error
);
notifyIcon.Visible = true;
listRSS(sf, null, true, null, (string)rZZs.tag);
if (rZZs.cur == rZZs.urls.Length)
{
formLoading.Close();
formLoading.Dispose();
}
}
}
private void toolStripButtonSHTitles_Click(object sender, EventArgs e)
{
splitContainerFeed.Panel1Collapsed = !splitContainerFeed.Panel1Collapsed;
splitContainerFeeds.Panel1Collapsed = !splitContainerFeeds.Panel1Collapsed;
if (splitContainerFeed.Panel1Collapsed)
{
toolStripButtonSHTitles.Image = global::RZZReader.Properties.Resources._2right;
} else
{
toolStripButtonSHTitles.Image = global::RZZReader.Properties.Resources._2left;
}
}
}
internal class LifeSpanHandler : ILifeSpanHandler
{
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(
IntPtr hWnd, // the handle of message reciever window
int Msg, // message ID
int wParam, // w parameter
int lParam // l parameter
);
private const int WM_SETICON = 0x80;
public LifeSpanHandler() { }
public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
//throw new NotImplementedException();
return false;
}
public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
//throw new NotImplementedException();
if (!browser.IsDisposed && browser.IsPopup)
{
var handle = browser.GetHost().GetWindowHandle();
Bitmap bmp = Properties.Resources.feed;
SendMessage(handle, WM_SETICON, 0, (int)bmp.GetHicon());
}
}
public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
//throw new NotImplementedException();
}
public bool OnBeforePopup(IWebBrowser chromiumWebBrowser,
IBrowser browser, IFrame frame,
string targetUrl, string targetFrameName,
WindowOpenDisposition targetDisposition,
bool userGesture,
IPopupFeatures popupFeatures, IWindowInfo windowInfo,
IBrowserSettings browserSettings,
ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
//throw new NotImplementedException();
newBrowser = new ChromiumWebBrowser();
newBrowser.Load(targetUrl);
return false;
}
}
public class FileEncryption
{
private static readonly byte[] Key = Encoding.UTF8.GetBytes("__your own key__");/* your encryption key, 16 bytes*/
private static readonly byte[] IV = Encoding.UTF8.GetBytes("__your own I.V__");/* your initialization vector, 16 bytes */
public static void EncryptFile(string inputFile, string outputFile)
{
using (var aes = new AesCryptoServiceProvider())
{
aes.Key = Key;
aes.IV = IV;
using (var inputFileStream = new FileStream(inputFile, FileMode.Open))
using (var outputFileStream = new FileStream(outputFile, FileMode.Create))
using (var cryptoStream = new CryptoStream(outputFileStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
inputFileStream.CopyTo(cryptoStream);
}
}
}
public static void DecryptFile(string inputFile, string outputFile)
{
using (var aes = new AesCryptoServiceProvider())
{
aes.Key = Key;
aes.IV = IV;
using (var inputFileStream = new FileStream(inputFile, FileMode.Open))
using (var outputFileStream = new FileStream(outputFile, FileMode.Create))
using (var cryptoStream = new CryptoStream(inputFileStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
{
cryptoStream.CopyTo(outputFileStream);
}
}
}
}
}