Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
andanteyk committed Apr 28, 2015
2 parents a49e934 + cd4d876 commit 680c4ad
Show file tree
Hide file tree
Showing 21 changed files with 380 additions and 302 deletions.
3 changes: 1 addition & 2 deletions Browser/Browser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Dpi.cs" />
<Compile Include="FormBrowser.cs">
<SubType>Form</SubType>
</Compile>
Expand All @@ -81,7 +80,6 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScreenHelper.cs" />
<Compile Include="VolumeManager.cs" />
<EmbeddedResource Include="FormBrowser.resx">
<DependentUpon>FormBrowser.cs</DependentUpon>
Expand All @@ -94,6 +92,7 @@
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
Expand Down
27 changes: 0 additions & 27 deletions Browser/Dpi.cs

This file was deleted.

19 changes: 19 additions & 0 deletions Browser/FormBrowser.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

180 changes: 93 additions & 87 deletions Browser/FormBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ServiceModel;
using System.Text;
Expand Down Expand Up @@ -136,6 +137,7 @@ 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;
Expand Down Expand Up @@ -178,8 +180,7 @@ private void SizeAdjuster_SizeChanged( object sender, EventArgs e ) {
Browser.Location.X, Browser.Location.Y, Browser.Width, Browser.Height, SizeAdjuster.Width, SizeAdjuster.Height, ClientSize.Width, ClientSize.Height ) );
//*/

//スタイルシート適用時はセンタリング
CenteringBrowser();
ApplyZoom();
}

private void CenteringBrowser() {
Expand Down Expand Up @@ -225,27 +226,21 @@ public void ApplyStyleSheet() {
var document = Browser.Document;
if ( document == null ) return;

var gameframe = document.GetElementById( "game_frame" );
if ( gameframe == null ) {
if ( document.Url.AbsolutePath.Contains( ".swf?" ) )
gameframe = document.Body;
}

if ( gameframe == null ) return;

if ( document.Url.AbsolutePath.Contains( ".swf?" ) ) {

var target = gameframe.Document;

if ( target != null ) {
mshtml.IHTMLStyleSheet ss = ( (mshtml.IHTMLDocument2)target.DomDocument ).createStyleSheet( "", 0 );

ss.cssText = Configuration.StyleSheet;
document.Body.SetAttribute( "width", "100%" );
document.Body.SetAttribute( "height", "100%" );

} else {
var swf = getFrameElementById( document, "externalswf" );
if ( swf == null ) return;

StyleSheetApplied = true;
// InvokeScriptは関数しか呼べないようなので、スクリプトをevalで渡す
document.InvokeScript( "eval", new object[] { Properties.Resources.PageScript } );
swf.Document.InvokeScript( "eval", new object[] { Properties.Resources.FrameScript } );
}


StyleSheetApplied = true;

} catch ( Exception ex ) {

Expand Down Expand Up @@ -274,43 +269,48 @@ public void RefreshBrowser() {
/// ズームを適用します。
/// </summary>
public void ApplyZoom() {
ApplyZoom( Configuration.ZoomRate );
}

/// <summary>
/// ズームを適用します。
/// </summary>
/// <param name="zoomRate">拡大率。%指定で 10-1000</param>
public void ApplyZoom( int zoomRate ) {
int zoomRate = Configuration.ZoomRate;
bool fit = Configuration.ZoomFit && StyleSheetApplied;

try {

if ( zoomRate < 10 )
zoomRate = 10;
if ( zoomRate > 1000 )
zoomRate = 1000;

var wb = Browser.ActiveXInstance as SHDocVw.IWebBrowser2;
if ( wb == null || wb.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_UNINITIALIZED || wb.Busy ) return;

var dpi = ScreenHelper.GetSystemDpi();
var zoomFactor = dpi.ScaleX + ( zoomRate / 100.0 - 1.0 );
var percentage = (int)( zoomFactor * 100 );
double zoomFactor;
object pin;

object pin = percentage;
object pout = null;
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 / dpi.ScaleX ) ),
(int)( KanColleSize.Height * ( zoomFactor / dpi.ScaleY ) )
(int)( KanColleSize.Width * zoomFactor ),
(int)( KanColleSize.Height * zoomFactor )
);
CenteringBrowser();
}

ToolMenu_Other_Zoom_Current.Text = string.Format( "現在: {0}%", zoomRate );
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 );
Expand Down Expand Up @@ -354,6 +354,33 @@ public void SaveScreenShot( string folderPath, int screenShotFormat ) {

}

// ラッパークラスに戻す
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>
/// スクリーンショットを保存します。
Expand All @@ -378,7 +405,7 @@ private void SaveScreenShot( string path, System.Drawing.Imaging.ImageFormat for


IViewObject viewobj = null;
int width = 0, height = 0;
//int width = 0, height = 0;


if ( document.url.Contains( ".swf?" ) ) {
Expand All @@ -388,67 +415,37 @@ private void SaveScreenShot( string path, System.Drawing.Imaging.ImageFormat for
throw new InvalidOperationException( "embed 要素の取得に失敗しました。" );
}

width = ( (HTMLEmbed)viewobj ).clientWidth;
height = ( (HTMLEmbed)viewobj ).clientHeight;
//width = ( (HTMLEmbed)viewobj ).clientWidth;
//height = ( (HTMLEmbed)viewobj ).clientHeight;

} else {

var gameFrame = document.getElementById( "game_frame" ).document as HTMLDocument;
if ( gameFrame == null ) {
throw new InvalidOperationException( "game_frame 要素の取得に失敗しました。" );
var swf = getFrameElementById( wb.Document, "externalswf" );
if ( swf == null ) {
throw new InvalidOperationException( "対象の swf が見つかりませんでした。" );
}

bool foundflag = false;

for ( int i = 0; i < document.frames.length; i++ ) {

var provider = document.frames.item( i ) as IServiceProvider;
if ( provider == null ) continue;

object ppvobj;
provider.QueryService( typeof( SHDocVw.IWebBrowserApp ).GUID, typeof( SHDocVw.IWebBrowser2 ).GUID, out ppvobj );

var _wb = ppvobj as SHDocVw.IWebBrowser2;
if ( _wb == null ) continue;

var iframe = _wb.Document as HTMLDocument;
if ( iframe == null ) continue;

Func<dynamic, bool> isvalid = target => {

var swf = iframe.getElementById( "externalswf" );
if ( swf == null ) continue;
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;
};

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 as HTMLEmbed ) && !isvalid( swf as HTMLObjectElement ) )
continue;

foundflag = true;

break;
}


if ( !foundflag ) {
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( width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb ) ) {

using ( var image = new Bitmap( rect.width, rect.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb ) ) {

var rect = new RECT { left = 0, top = 0, width = width, height = height };
var device = new DVTARGETDEVICE { tdSize = 0 };

using ( var g = Graphics.FromImage( image ) ) {
Expand Down Expand Up @@ -556,12 +553,14 @@ private void ToolMenu_Other_ScreenShot_Click( object sender, EventArgs e ) {

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();
}
Expand Down Expand Up @@ -592,6 +591,13 @@ private void ToolMenu_Other_Zoom_Click( object sender, EventArgs e ) {
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();
}
Expand Down
Loading

0 comments on commit 680c4ad

Please sign in to comment.