/
FormBrowser.cs
1193 lines (898 loc) · 35.5 KB
/
FormBrowser.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 BrowserLib;
using mshtml;
using Nekoxy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Browser {
/// <summary>
/// ブラウザを表示するフォームです。
/// </summary>
/// <remarks>thx KanColleViewer!</remarks>
[ServiceBehavior( InstanceContextMode = InstanceContextMode.Single )]
public partial class FormBrowser : Form, IBrowser {
private readonly Size KanColleSize = new Size( 800, 480 );
// FormBrowserHostの通信サーバ
private string ServerUri;
// FormBrowserの通信サーバ
private PipeCommunicator<IBrowserHost> BrowserHost;
private BrowserLib.BrowserConfiguration Configuration;
// 親プロセスが生きているか定期的に確認するためのタイマー
private Timer HeartbeatTimer = new Timer();
private IntPtr HostWindow;
private bool _styleSheetApplied;
/// <summary>
/// スタイルシートの変更が適用されているか
/// </summary>
private bool StyleSheetApplied {
get { return _styleSheetApplied; }
set {
if ( value ) {
//Browser.Anchor = AnchorStyles.None;
ApplyZoom();
SizeAdjuster_SizeChanged( null, new EventArgs() );
} else {
SizeAdjuster.SuspendLayout();
//Browser.Anchor = AnchorStyles.Top | AnchorStyles.Left;
Browser.Location = new Point( 0, 0 );
Browser.MinimumSize = new Size( 0, 0 );
Browser.Size = SizeAdjuster.Size;
SizeAdjuster.ResumeLayout();
}
_styleSheetApplied = value;
}
}
/// <summary>
/// 艦これが読み込まれているかどうか
/// </summary>
private bool IsKanColleLoaded { get; set; }
private VolumeManager _volumeManager;
private string _lastScreenShotPath;
private NumericUpDown ToolMenu_Other_Volume_VolumeControl {
get { return (NumericUpDown)( (ToolStripControlHost)ToolMenu_Other_Volume.DropDownItems["ToolMenu_Other_Volume_VolumeControlHost"] ).Control; }
}
private PictureBox ToolMenu_Other_LastScreenShot_Control {
get { return (PictureBox)( (ToolStripControlHost)ToolMenu_Other_LastScreenShot.DropDownItems["ToolMenu_Other_LastScreenShot_ImageHost"] ).Control; }
}
/// <summary>
/// </summary>
/// <param name="serverUri">ホストプロセスとの通信用URL</param>
public FormBrowser( string serverUri ) {
InitializeComponent();
ServerUri = serverUri;
StyleSheetApplied = false;
_volumeManager = new VolumeManager( (uint)System.Diagnostics.Process.GetCurrentProcess().Id );
Browser.ReplacedKeyDown += Browser_ReplacedKeyDown;
// 音量設定用コントロールの追加
{
var control = new NumericUpDown();
control.Name = "ToolMenu_Other_Volume_VolumeControl";
control.Maximum = 100;
control.TextAlign = HorizontalAlignment.Right;
control.Font = ToolMenu_Other_Volume.Font;
control.ValueChanged += ToolMenu_Other_Volume_ValueChanged;
control.Tag = false;
var host = new ToolStripControlHost( control, "ToolMenu_Other_Volume_VolumeControlHost" );
control.Size = new Size( host.Width - control.Margin.Horizontal, host.Height - control.Margin.Vertical );
control.Location = new Point( control.Margin.Left, control.Margin.Top );
ToolMenu_Other_Volume.DropDownItems.Add( host );
}
// スクリーンショットプレビューコントロールの追加
{
double zoomrate = 0.5;
var control = new PictureBox();
control.Name = "ToolMenu_Other_LastScreenShot_Image";
control.SizeMode = PictureBoxSizeMode.Zoom;
control.Size = new Size( (int)( KanColleSize.Width * zoomrate ), (int)( KanColleSize.Height * zoomrate ) );
control.Margin = new Padding();
control.Image = new Bitmap( (int)( KanColleSize.Width * zoomrate ), (int)( KanColleSize.Height * zoomrate ), PixelFormat.Format24bppRgb );
using ( var g = Graphics.FromImage( control.Image ) ) {
g.Clear( SystemColors.Control );
g.DrawString( "スクリーンショットをまだ撮影していません。\r\n", Font, Brushes.Black, new Point( 4, 4 ) );
}
var host = new ToolStripControlHost( control, "ToolMenu_Other_LastScreenShot_ImageHost" );
host.Size = new Size( control.Width + control.Margin.Horizontal, control.Height + control.Margin.Vertical );
host.AutoSize = false;
control.Location = new Point( control.Margin.Left, control.Margin.Top );
host.Click += ToolMenu_Other_LastScreenShot_ImageHost_Click;
ToolMenu_Other_LastScreenShot.DropDownItems.Insert( 0, host );
}
}
private void FormBrowser_Load( object sender, EventArgs e ) {
SetWindowLong( this.Handle, GWL_STYLE, WS_CHILD );
// ホストプロセスに接続
BrowserHost = new PipeCommunicator<IBrowserHost>(
this, typeof( IBrowser ), ServerUri + "Browser", "Browser" );
BrowserHost.Connect( ServerUri + "/BrowserHost" );
BrowserHost.Faulted += BrowserHostChannel_Faulted;
ConfigurationChanged( BrowserHost.Proxy.Configuration );
// ウィンドウの親子設定&ホストプロセスから接続してもらう
BrowserHost.Proxy.ConnectToBrowser( this.Handle );
// 親ウィンドウが生きているか確認
HeartbeatTimer.Tick += (EventHandler)( ( sender2, e2 ) => {
BrowserHost.AsyncRemoteRun( () => { HostWindow = BrowserHost.Proxy.HWND; } );
} );
HeartbeatTimer.Interval = 2000; // 2秒ごと
HeartbeatTimer.Start();
BrowserHost.AsyncRemoteRun( () => BrowserHost.Proxy.GetIconResource() );
}
void Exit() {
if ( !BrowserHost.Closed ) {
BrowserHost.Close();
HeartbeatTimer.Stop();
Application.Exit();
}
}
void BrowserHostChannel_Faulted( Exception e ) {
// 親と通信できなくなったら終了する
Exit();
}
public void CloseBrowser() {
HeartbeatTimer.Stop();
// リモートコールでClose()呼ぶのばヤバそうなので非同期にしておく
BeginInvoke( (Action)( () => Exit() ) );
}
public void ConfigurationChanged( BrowserLib.BrowserConfiguration conf ) {
Configuration = conf;
SizeAdjuster.AutoScroll = Configuration.IsScrollable;
ToolMenu_Other_Zoom_Fit.Checked = Configuration.ZoomFit;
ApplyZoom();
ToolMenu_Other_AppliesStyleSheet.Checked = Configuration.AppliesStyleSheet;
ToolMenu.Dock = (DockStyle)Configuration.ToolMenuDockStyle;
ToolMenu.Visible = Configuration.IsToolMenuVisible;
}
private void ConfigurationUpdated() {
BrowserHost.AsyncRemoteRun( () => BrowserHost.Proxy.ConfigurationUpdated( Configuration ) );
}
private void AddLog( int priority, string message ) {
BrowserHost.AsyncRemoteRun( () => BrowserHost.Proxy.AddLog( priority, message ) );
}
public void InitialAPIReceived() {
IsKanColleLoaded = true;
//ロード直後の適用ではレイアウトがなぜか崩れるのでこのタイミングでも適用
ApplyStyleSheet();
ApplyZoom();
//起動直後はまだ音声が鳴っていないのでミュートできないため、この時点で有効化
SetVolumeState();
}
private void SizeAdjuster_SizeChanged( object sender, EventArgs e ) {
if ( !StyleSheetApplied ) {
Browser.Location = new Point( 0, 0 );
Browser.Size = SizeAdjuster.Size;
return;
}
/*/
Utility.Logger.Add( 1, string.Format( "SizeChanged: BR ({0},{1}) {2}x{3}, PA {4}x{5}, CL {6}x{7}",
Browser.Location.X, Browser.Location.Y, Browser.Width, Browser.Height, SizeAdjuster.Width, SizeAdjuster.Height, ClientSize.Width, ClientSize.Height ) );
//*/
ApplyZoom();
}
private void CenteringBrowser() {
int x = Browser.Location.X, y = Browser.Location.Y;
bool isScrollable = Configuration.IsScrollable;
if ( !isScrollable || Browser.Width <= SizeAdjuster.Width ) {
x = ( SizeAdjuster.Width - Browser.Width ) / 2;
}
if ( !isScrollable || Browser.Height <= SizeAdjuster.Height ) {
y = ( SizeAdjuster.Height - Browser.Height ) / 2;
}
//if ( x != Browser.Location.X || y != Browser.Location.Y )
Browser.Location = new Point( x, y );
}
private void Browser_Navigating( object sender, WebBrowserNavigatingEventArgs e ) {
// note: ここを有効にすると別ページに切り替えた際にきちんとセーフティが働くが、代わりにまれに誤検知して撮影できなくなる時がある
// 無効にするとセーフティは働かなくなるが誤検知がなくなる
// セーフティを切ってでも誤検知しなくしたほうがいいので無効化
//IsKanColleLoaded = false;
}
private void Browser_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e ) {
StyleSheetApplied = false;
ApplyStyleSheet();
ApplyZoom();
}
/// <summary>
/// スタイルシートを適用します。
/// </summary>
public void ApplyStyleSheet() {
if ( !Configuration.AppliesStyleSheet )
return;
try {
var document = Browser.Document;
if ( document == null ) return;
if ( document.Url.ToString().Contains( ".swf?" ) ) {
document.Body.SetAttribute( "width", "100%" );
document.Body.SetAttribute( "height", "100%" );
} else {
var swf = getFrameElementById( document, "externalswf" );
if ( swf == null ) return;
// InvokeScriptは関数しか呼べないようなので、スクリプトをevalで渡す
document.InvokeScript( "eval", new object[] { Properties.Resources.PageScript } );
swf.Document.InvokeScript( "eval", new object[] { Properties.Resources.FrameScript } );
}
StyleSheetApplied = true;
} catch ( Exception ex ) {
BrowserHost.AsyncRemoteRun( () =>
BrowserHost.Proxy.SendErrorReport( ex.ToString(), "スタイルシートの適用に失敗しました。" ) );
}
}
/// <summary>
/// 指定した URL のページを開きます。
/// </summary>
public void Navigate( string url ) {
StyleSheetApplied = false;
Browser.Navigate( url );
}
/// <summary>
/// ブラウザを再読み込みします。
/// </summary>
public void RefreshBrowser() {
Browser.Refresh( WebBrowserRefreshOption.Completely );
}
/// <summary>
/// ズームを適用します。
/// </summary>
public void ApplyZoom() {
int zoomRate = Configuration.ZoomRate;
bool fit = Configuration.ZoomFit && StyleSheetApplied;
try {
var wb = Browser.ActiveXInstance as SHDocVw.IWebBrowser2;
if ( wb == null || wb.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_UNINITIALIZED || wb.Busy ) return;
double zoomFactor;
object pin;
if ( fit ) {
pin = 100;
double rateX = (double)SizeAdjuster.Width / KanColleSize.Width;
double rateY = (double)SizeAdjuster.Height / KanColleSize.Height;
zoomFactor = Math.Min( rateX, rateY );
} else {
if ( zoomRate < 10 )
zoomRate = 10;
if ( zoomRate > 1000 )
zoomRate = 1000;
pin = zoomRate;
zoomFactor = zoomRate / 100.0;
}
object pout = null;
wb.ExecWB( SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref pin, ref pout );
if ( StyleSheetApplied ) {
Browser.Size = Browser.MinimumSize = new Size(
(int)( KanColleSize.Width * zoomFactor ),
(int)( KanColleSize.Height * zoomFactor )
);
CenteringBrowser();
}
if ( fit ) {
ToolMenu_Other_Zoom_Current.Text = string.Format( "現在: ぴったり" );
} else {
ToolMenu_Other_Zoom_Current.Text = string.Format( "現在: {0}%", zoomRate );
}
} catch ( Exception ex ) {
AddLog( 3, "ズームの適用に失敗しました。" + ex.Message );
}
}
/// <summary>
/// スクリーンショットを保存します。
/// </summary>
/// <param name="folderPath">保存するフォルダへのパス。</param>
/// <param name="screenShotFormat">スクリーンショットのフォーマット。1=jpg, 2=png</param>
public void SaveScreenShot( string folderPath, int screenShotFormat ) {
if ( !System.IO.Directory.Exists( folderPath ) ) {
System.IO.Directory.CreateDirectory( folderPath );
}
string ext;
System.Drawing.Imaging.ImageFormat format;
switch ( screenShotFormat ) {
case 1:
ext = "jpg";
format = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case 2:
default:
ext = "png";
format = System.Drawing.Imaging.ImageFormat.Png;
break;
}
SaveScreenShot( string.Format(
"{0}\\{1:yyyyMMdd_HHmmssff}.{2}",
folderPath,
DateTime.Now,
ext ), format );
}
// ラッパークラスに戻す
private static HtmlDocument WrapHTMLDocument( IHTMLDocument2 document ) {
ConstructorInfo[] constructor = typeof( HtmlDocument ).GetConstructors(
BindingFlags.NonPublic | BindingFlags.Instance );
return (HtmlDocument)constructor[0].Invoke( new object[] { null, document } );
}
// 中のフレームからidにマッチする要素を返す
private static HtmlElement getFrameElementById( HtmlDocument document, String id ) {
foreach ( HtmlWindow frame in document.Window.Frames ) {
// frameが別ドメインだとセキュリティ上の問題(クロスフレームスクリプティング)
// からアクセスができないのでアクセスできるドキュメントに変換する
IServiceProvider provider = (IServiceProvider)frame.DomWindow;
object ppvobj;
provider.QueryService( typeof( SHDocVw.IWebBrowserApp ).GUID, typeof( SHDocVw.IWebBrowser2 ).GUID, out ppvobj );
var htmlDocument = WrapHTMLDocument( (IHTMLDocument2)( (SHDocVw.IWebBrowser2)ppvobj ).Document );
var htmlElement = htmlDocument.GetElementById( id );
if ( htmlElement == null )
continue;
return htmlElement;
}
return null;
}
/// <summary>
/// スクリーンショットを保存します。
/// </summary>
/// <param name="path">保存先。</param>
/// <param name="format">画像のフォーマット。</param>
private void SaveScreenShot( string path, System.Drawing.Imaging.ImageFormat format ) {
var wb = Browser;
if ( !IsKanColleLoaded ) {
AddLog( 3, string.Format( "艦これが読み込まれていないため、スクリーンショットを撮ることはできません。" ) );
System.Media.SystemSounds.Beep.Play();
return;
}
try {
IViewObject viewobj = null;
//int width = 0, height = 0;
if ( wb.Document.Url.ToString().Contains( ".swf?" ) ) {
viewobj = wb.Document.GetElementsByTagName( "embed" )[0].DomElement as IViewObject;
if ( viewobj == null ) {
throw new InvalidOperationException( "embed 要素の取得に失敗しました。" );
}
//width = ( (HTMLEmbed)viewobj ).clientWidth;
//height = ( (HTMLEmbed)viewobj ).clientHeight;
} else {
var swf = getFrameElementById( wb.Document, "externalswf" );
if ( swf == null ) {
throw new InvalidOperationException( "対象の swf が見つかりませんでした。" );
}
Func<dynamic, bool> isvalid = target => {
if ( target == null ) return false;
viewobj = target as IViewObject;
if ( viewobj == null ) return false;
//if ( !int.TryParse( target.width, out width ) ) return false;
//if ( !int.TryParse( target.height, out height ) ) return false;
return true;
};
if ( !isvalid( swf.DomElement as HTMLEmbed ) && !isvalid( swf.DomElement as HTMLObjectElement ) ) {
throw new InvalidOperationException( "対象の swf が見つかりませんでした。" );
}
}
if ( viewobj != null ) {
var rect = new RECT { left = 0, top = 0, width = KanColleSize.Width, height = KanColleSize.Height };
using ( var image = new Bitmap( rect.width, rect.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb ) ) {
var device = new DVTARGETDEVICE { tdSize = 0 };
using ( var g = Graphics.FromImage( image ) ) {
var hdc = g.GetHdc();
viewobj.Draw( 1, 0, IntPtr.Zero, device, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero );
g.ReleaseHdc( hdc );
}
image.Save( path, format );
}
}
_lastScreenShotPath = path;
AddLog( 2, string.Format( "スクリーンショットを {0} に保存しました。", path ) );
} catch ( Exception ex ) {
BrowserHost.AsyncRemoteRun( () =>
BrowserHost.Proxy.SendErrorReport( ex.ToString(), "スクリーンショットの保存時にエラーが発生しました。" ) );
System.Media.SystemSounds.Beep.Play();
}
}
public void SetProxy( string proxy ) {
ushort port;
if ( ushort.TryParse( proxy, out port ) ) {
WinInetUtil.SetProxyInProcessForNekoxy( port );
} else {
WinInetUtil.SetProxyInProcess( proxy, "local" );
}
//AddLog( 1, "setproxy:" + proxy );
}
/// <summary>
/// キャッシュを削除します。
/// </summary>
private void ClearCache() {
const int CACHEGROUP_SEARCH_ALL = 0x0;
const int ERROR_NO_MORE_ITEMS = 259;
const uint CACHEENTRYTYPE_COOKIE = 1048577;
const uint CACHEENTRYTYPE_HISTORY = 2097153;
long groupId = 0;
int cacheEntryInfoBufferSizeInitial = 0;
int cacheEntryInfoBufferSize = 0;
IntPtr cacheEntryInfoBuffer = IntPtr.Zero;
IntPtr enumHandle = IntPtr.Zero;
enumHandle = FindFirstUrlCacheGroup( 0, CACHEGROUP_SEARCH_ALL, IntPtr.Zero, 0, ref groupId, IntPtr.Zero );
if ( enumHandle != IntPtr.Zero && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error() )
return;
/*/
while ( true ) {
bool returnValue = DeleteUrlCacheGroup( groupId, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, IntPtr.Zero );
if ( !returnValue && ERROR_FILE_NOT_FOUND == Marshal.GetLastWin32Error() ) {
returnValue = FindNextUrlCacheGroup( enumHandle, ref groupId, IntPtr.Zero );
}
if ( !returnValue && ( ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error() || ERROR_FILE_NOT_FOUND == Marshal.GetLastWin32Error() ) )
break;
}
//*/
enumHandle = FindFirstUrlCacheEntry( null, IntPtr.Zero, ref cacheEntryInfoBufferSizeInitial );
if ( enumHandle != IntPtr.Zero && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error() )
return;
cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
cacheEntryInfoBuffer = Marshal.AllocHGlobal( cacheEntryInfoBufferSize );
enumHandle = FindFirstUrlCacheEntry( null, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial );
while ( true ) {
var internetCacheEntry = (INTERNET_CACHE_ENTRY_INFOA)Marshal.PtrToStructure( cacheEntryInfoBuffer, typeof( INTERNET_CACHE_ENTRY_INFOA ) );
cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize;
var type = internetCacheEntry.CacheEntryType;
bool returnValue = false;
if ( type != CACHEENTRYTYPE_COOKIE && type != CACHEENTRYTYPE_HISTORY )
returnValue = DeleteUrlCacheEntry( internetCacheEntry.lpszSourceUrlName );
if ( !returnValue ) {
returnValue = FindNextUrlCacheEntry( enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial );
}
if ( !returnValue && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error() ) {
break;
}
if ( !returnValue && cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize ) {
cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
cacheEntryInfoBuffer = Marshal.ReAllocHGlobal( cacheEntryInfoBuffer, (IntPtr)cacheEntryInfoBufferSize );
returnValue = FindNextUrlCacheEntry( enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial );
}
}
Marshal.FreeHGlobal( cacheEntryInfoBuffer );
}
public void SetIconResource( byte[] canvas ) {
string[] keys = new string[] {
"Browser_ScreenShot",
"Browser_Zoom",
"Browser_ZoomIn",
"Browser_ZoomOut",
"Browser_Unmute",
"Browser_Mute",
"Browser_Refresh",
"Browser_Navigate",
"Browser_Other",
};
int unitsize = 16 * 16 * 4;
for ( int i = 0; i < keys.Length; i++ ) {
Bitmap bmp = new Bitmap( 16, 16, PixelFormat.Format32bppArgb );
if ( canvas != null ) {
BitmapData bmpdata = bmp.LockBits( new Rectangle( 0, 0, bmp.Width, bmp.Height ), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb );
Marshal.Copy( canvas, unitsize * i, bmpdata.Scan0, unitsize );
bmp.UnlockBits( bmpdata );
}
Icons.Images.Add( keys[i], bmp );
}
ToolMenu_ScreenShot.Image = ToolMenu_Other_ScreenShot.Image =
Icons.Images["Browser_ScreenShot"];
ToolMenu_Zoom.Image = ToolMenu_Other_Zoom.Image =
Icons.Images["Browser_Zoom"];
ToolMenu_Other_Zoom_Increment.Image =
Icons.Images["Browser_ZoomIn"];
ToolMenu_Other_Zoom_Decrement.Image =
Icons.Images["Browser_ZoomOut"];
ToolMenu_Refresh.Image = ToolMenu_Other_Refresh.Image =
Icons.Images["Browser_Refresh"];
ToolMenu_NavigateToLogInPage.Image = ToolMenu_Other_NavigateToLogInPage.Image =
Icons.Images["Browser_Navigate"];
ToolMenu_Other.Image =
Icons.Images["Browser_Other"];
SetVolumeState();
}
private void SetVolumeState() {
bool mute;
float volume;
try {
mute = _volumeManager.IsMute;
volume = _volumeManager.Volume * 100;
} catch ( Exception ) {
// 音量データ取得不能時
mute = false;
volume = 100;
}
ToolMenu_Mute.Image = ToolMenu_Other_Mute.Image =
Icons.Images[mute ? "Browser_Mute" : "Browser_Unmute"];
{
var control = ToolMenu_Other_Volume_VolumeControl;
control.Tag = false;
control.Value = (decimal)volume;
control.Tag = true;
}
Configuration.Volume = volume;
Configuration.IsMute = mute;
ConfigurationUpdated();
}
private void ToolMenu_Other_ScreenShot_Click( object sender, EventArgs e ) {
SaveScreenShot( Configuration.ScreenShotPath, Configuration.ScreenShotFormat );
}
private void ToolMenu_Other_Zoom_Decrement_Click( object sender, EventArgs e ) {
Configuration.ZoomRate = Math.Max( Configuration.ZoomRate - 20, 10 );
Configuration.ZoomFit = ToolMenu_Other_Zoom_Fit.Checked = false;
ApplyZoom();
ConfigurationUpdated();
}
private void ToolMenu_Other_Zoom_Increment_Click( object sender, EventArgs e ) {
Configuration.ZoomRate = Math.Min( Configuration.ZoomRate + 20, 1000 );
Configuration.ZoomFit = ToolMenu_Other_Zoom_Fit.Checked = false;
ApplyZoom();
ConfigurationUpdated();
}
private void ToolMenu_Other_Zoom_Click( object sender, EventArgs e ) {
int zoom;
if ( sender == ToolMenu_Other_Zoom_25 )
zoom = 25;
else if ( sender == ToolMenu_Other_Zoom_50 )
zoom = 50;
else if ( sender == ToolMenu_Other_Zoom_75 )
zoom = 75;
else if ( sender == ToolMenu_Other_Zoom_100 )
zoom = 100;
else if ( sender == ToolMenu_Other_Zoom_150 )
zoom = 150;
else if ( sender == ToolMenu_Other_Zoom_200 )
zoom = 200;
else if ( sender == ToolMenu_Other_Zoom_250 )
zoom = 250;
else if ( sender == ToolMenu_Other_Zoom_300 )
zoom = 300;
else if ( sender == ToolMenu_Other_Zoom_400 )
zoom = 400;
else
zoom = 100;
Configuration.ZoomRate = zoom;
Configuration.ZoomFit = ToolMenu_Other_Zoom_Fit.Checked = false;
ApplyZoom();
ConfigurationUpdated();
}
private void ToolMenu_Other_Zoom_Fit_Click( object sender, EventArgs e ) {
Configuration.ZoomFit = ToolMenu_Other_Zoom_Fit.Checked;
ApplyZoom();
ConfigurationUpdated();
}
//ズームUIの使いまわし
private void ToolMenu_Other_DropDownOpening( object sender, EventArgs e ) {
var list = ToolMenu_Zoom.DropDownItems.Cast<ToolStripItem>().ToArray();
ToolMenu_Other_Zoom.DropDownItems.AddRange( list );
}
private void ToolMenu_Zoom_DropDownOpening( object sender, EventArgs e ) {
var list = ToolMenu_Other_Zoom.DropDownItems.Cast<ToolStripItem>().ToArray();
ToolMenu_Zoom.DropDownItems.AddRange( list );
}
private void ToolMenu_Other_Mute_Click( object sender, EventArgs e ) {
try {
_volumeManager.ToggleMute();
} catch ( Exception ) {
System.Media.SystemSounds.Beep.Play();
}
SetVolumeState();
}
void ToolMenu_Other_Volume_ValueChanged( object sender, EventArgs e ) {
var control = ToolMenu_Other_Volume_VolumeControl;
try {
if ( (bool)control.Tag )
_volumeManager.Volume = (float)( control.Value / 100 );
control.BackColor = SystemColors.Window;
} catch ( Exception ) {
control.BackColor = Color.MistyRose;
}
}
private void ToolMenu_Other_Refresh_Click( object sender, EventArgs e ) {
if ( !Configuration.ConfirmAtRefresh ||
MessageBox.Show( "再読み込みします。\r\nよろしいですか?", "確認",
MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2 )
== System.Windows.Forms.DialogResult.OK ) {
RefreshBrowser();
}
}
private void ToolMenu_Other_NavigateToLogInPage_Click( object sender, EventArgs e ) {
if ( MessageBox.Show( "ログインページへ移動します。\r\nよろしいですか?", "確認",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2 )
== System.Windows.Forms.DialogResult.OK ) {
Navigate( Configuration.LogInPageURL );
}
}
private void ToolMenu_Other_Navigate_Click( object sender, EventArgs e ) {
BrowserHost.AsyncRemoteRun( () => BrowserHost.Proxy.RequestNavigation( Browser.Url == null ? null : Browser.Url.ToString() ) );
}
private void ToolMenu_Other_AppliesStyleSheet_Click( object sender, EventArgs e ) {
Configuration.AppliesStyleSheet = ToolMenu_Other_AppliesStyleSheet.Checked;
if ( Configuration.AppliesStyleSheet ) {
ApplyStyleSheet();
}
ConfigurationUpdated();
}
private void ToolMenu_Other_Alignment_Click( object sender, EventArgs e ) {
if ( sender == ToolMenu_Other_Alignment_Top )
ToolMenu.Dock = DockStyle.Top;
else if ( sender == ToolMenu_Other_Alignment_Bottom )
ToolMenu.Dock = DockStyle.Bottom;
else if ( sender == ToolMenu_Other_Alignment_Left )
ToolMenu.Dock = DockStyle.Left;
else
ToolMenu.Dock = DockStyle.Right;
Configuration.ToolMenuDockStyle = (int)ToolMenu.Dock;
ConfigurationUpdated();
}
private void ToolMenu_Other_Alignment_Invisible_Click( object sender, EventArgs e ) {
ToolMenu.Visible =
Configuration.IsToolMenuVisible = false;
ConfigurationUpdated();
}
private void ToolMenu_Other_ClearCache_Click( object sender, EventArgs e ) {
if ( MessageBox.Show( "ブラウザのキャッシュを削除します。\nよろしいですか?", "キャッシュの削除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question )
== System.Windows.Forms.DialogResult.OK ) {
BeginInvoke( (MethodInvoker)( () => {
ClearCache();
MessageBox.Show( "キャッシュの削除が完了しました。", "削除完了", MessageBoxButtons.OK, MessageBoxIcon.Information );
} ) );
}
}
private void SizeAdjuster_DoubleClick( object sender, EventArgs e ) {
ToolMenu.Visible =
Configuration.IsToolMenuVisible = true;
ConfigurationUpdated();
}
private void ContextMenuTool_ShowToolMenu_Click( object sender, EventArgs e ) {
ToolMenu.Visible =
Configuration.IsToolMenuVisible = true;
ConfigurationUpdated();
}
private void ContextMenuTool_Opening( object sender, CancelEventArgs e ) {
if ( IsKanColleLoaded || ToolMenu.Visible )
e.Cancel = true;
}
private void ToolMenu_ScreenShot_Click( object sender, EventArgs e ) {
ToolMenu_Other_ScreenShot_Click( sender, e );
}
private void ToolMenu_Mute_Click( object sender, EventArgs e ) {
ToolMenu_Other_Mute_Click( sender, e );
}
private void ToolMenu_Refresh_Click( object sender, EventArgs e ) {
ToolMenu_Other_Refresh_Click( sender, e );
}
private void ToolMenu_NavigateToLogInPage_Click( object sender, EventArgs e ) {
ToolMenu_Other_NavigateToLogInPage_Click( sender, e );
}
// ショートカットキーが反映されない問題の対策
void Browser_ReplacedKeyDown( object sender, KeyEventArgs e ) {
foreach ( var item in ToolMenu_Other.DropDownItems ) {
ToolStripMenuItem menu = item as ToolStripMenuItem;
if ( menu != null ) {
if ( e.KeyData == menu.ShortcutKeys ) {
menu.PerformClick();
e.Handled = true;
}
}
}
/*// 有効にするとショートカットが完全に無効化できる代わりに提督コメント・艦隊名が入力不能に
if ( IsKanColleLoaded )
e.Handled = true;
//*/
}
private void FormBrowser_Activated( object sender, EventArgs e ) {
//System.Media.SystemSounds.Asterisk.Play();
Browser.Focus();
}
private void ToolMenu_Other_Alignment_DropDownOpening( object sender, EventArgs e ) {
foreach ( var item in ToolMenu_Other_Alignment.DropDownItems ) {
var menu = item as ToolStripMenuItem;
if ( menu != null ) {
menu.Checked = false;
}
}
switch ( (DockStyle)Configuration.ToolMenuDockStyle ) {
case DockStyle.Top:
ToolMenu_Other_Alignment_Top.Checked = true;
break;
case DockStyle.Bottom:
ToolMenu_Other_Alignment_Bottom.Checked = true;
break;
case DockStyle.Left:
ToolMenu_Other_Alignment_Left.Checked = true;
break;
case DockStyle.Right:
ToolMenu_Other_Alignment_Right.Checked = true;
break;
}
ToolMenu_Other_Alignment_Invisible.Checked = !Configuration.IsToolMenuVisible;
}
private void ToolMenu_Other_LastScreenShot_DropDownOpening( object sender, EventArgs e ) {
try {
using ( var fs = new FileStream( _lastScreenShotPath, FileMode.Open, FileAccess.Read ) ) {
if ( ToolMenu_Other_LastScreenShot_Control.Image != null )
ToolMenu_Other_LastScreenShot_Control.Image.Dispose();
ToolMenu_Other_LastScreenShot_Control.Image = Image.FromStream( fs );
}
} catch ( Exception ) {
// *ぷちっ*
}
}
void ToolMenu_Other_LastScreenShot_ImageHost_Click( object sender, EventArgs e ) {
if ( _lastScreenShotPath != null && System.IO.File.Exists( _lastScreenShotPath ) )
System.Diagnostics.Process.Start( _lastScreenShotPath );
}
private void ToolMenu_Other_LastScreenShot_OpenScreenShotFolder_Click( object sender, EventArgs e ) {
if ( System.IO.Directory.Exists( Configuration.ScreenShotPath ) )
System.Diagnostics.Process.Start( Configuration.ScreenShotPath );
}
protected override void WndProc( ref Message m ) {
if ( m.Msg == WM_ERASEBKGND )
// ignore this message
return;
base.WndProc( ref m );
}
#region 呪文